Difference between revisions of "Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project"

From OpenCircuits
Jump to navigation Jump to search
Line 49: Line 49:
 
|-valign="top"
 
|-valign="top"
 
|Power supply
 
|Power supply
|Not shown, you need two dc voltages, one for the PIC at 5 volts, and another with the right voltage for the motors.  There is a suitable power supply in the [[PIC based Stepper Motor Dancing Analog Clock]]
+
|Not shown, 5 volts, you can use a higher voltage with a voltage regulator.  There is a suitable power supply in the [[PIC based Stepper Motor Dancing Analog Clock]]
 
<!-------------------------------->
 
<!-------------------------------->
 
 
|-valign="top"
 
|-valign="top"
 
|PIC16F877A
 
|PIC16F877A
|My favorate 16 series part, relatively lots of memory and pins.  Bigger than you need, but only about 8 bucks.  Try with an 18 series part, should not be hard and will leave you more up to date.  Let me know.
+
|My favorate 16 series part, relatively lots of memory and pins.  Bigger than you need, but only about 8 bucks.  Try with an 18 series part, should not be hard and will leave you more up to date.  Let me know.  Other parts of the PIC circuit not discussed here.
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 65: Line 64:
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|Pull Up/Down Resistors
+
|Power supply splitting resistors, form a pseodo ground at about 2.5 volts.
|10k more or less
+
|100 more or less
 +
<!-------------------------------->
 +
|-valign="top"
 +
|RIN
 +
|about 5 to 10 K 
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
|CQ1, CQ2 = capicators for the crystal
+
|RFB
|about 20 pf seems to work, see the PIC16F877A manual
+
|feedback resistor should be about 4 times higher than the input resistor
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 76: Line 79:
 
|
 
|
 
<!--------------------------------
 
<!--------------------------------
|-valign="top"
 
|xx
 
|
 
<!-------------------------------->
 
 
|-valign="top"
 
|-valign="top"
 
|Q = crystal
 
|Q = crystal
Line 95: Line 94:
 
|RRA2, RRA3  = Pull up resistors
 
|RRA2, RRA3  = Pull up resistors
 
|10K more or less
 
|10K more or less
<!-------------------------------->
+
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
 
|C_BP  = By Pass Cap.
 
|C_BP  = By Pass Cap.
Line 107: Line 106:
 
|RMC  = Pull up resistor for master clear ( pull down with push button switch to reset ).
 
|RMC  = Pull up resistor for master clear ( pull down with push button switch to reset ).
 
|10k more or less
 
|10k more or less
<!-------------------------------->
+
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
 
|PUSH_BUTTON_SWITCH  = Reset
 
|PUSH_BUTTON_SWITCH  = Reset
Line 132: Line 131:
 
|
 
|
 
|
 
|
<!-------------------------------->
 
|-valign="top"
 
|VPLUS_VDD = Positive power
 
|To the 5 volt power. 
 
<!-------------------------------->
 
|-valign="top"
 
|G1  = Ground connection
 
|Not show, add one to connect the power supply.
 
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 148: Line 139:
 
|  =
 
|  =
 
|
 
|
<!-------------------------------->
 
|-valign="top"
 
|MOUNT_1...4
 
|Mounting holes
 
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"

Revision as of 16:49, 19 December 2008

Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project

EARLY DRAFT - NOT READY


  • Name: Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
  • Status: early draft -- do not read
  • Technology: PIC microcontroller with code in BoostC
  • Author: russ_hensel ( where you can find an email address to reach me )
  • Summary: A PIC16F877A project that operates a stepper motor as an encoder Runs with a PC running a terminal program.
  • Last revision Dec 19 2008 – Early Draft.
  • Archive zip file ( none ) includes: nothing Email me for file, will be posted eventually.

Rotary encoders are devices that allow electronic measurement of rotation. You can find a lot of information by Googling “rotary encoder”. Many are optical with a partly transparent wheel rotating and being read by 2 pairs of led and photo detectors. Most of the old ball based mice had 2 optical rotary encoders to measure motion in the x and y direction. You can take these old guys apart to get at the encoders. Stepper motors can also be used as rotary encoders. When rotated the motors act as generators and generate the two out of phase waves that are the standard output of encoders (at least after the waves are squared up). If turned very slowly the output signal drops to a very low voltage and you can miss steps; so these guys are not perfect. A nice feature of the motors is that they often have a very nice feel in your hands especially if you put a nice knob on them. Other multi phase motors, like disk drive motors, are reputed to work as well, and often have enough mechanism on them to use as a knob. The output of the encoder can be used as a volume control, tuning device or whatever.

This project describes just enough software and hardware to let you experiment with stepper motors as encoders to use as a jumping off point for a more complete project.


Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program, or as an alternative running a Java program developed especially to control the PIC ( still under development ). The PIC chip is supplemented with a dual op amp connected as two schmidt triggers, there is of course a stepper motor.



Hardware

The output of the stepper motor can be quite small if the motor is turned slowly, or quite high if turned fast. To deal with the high output we used 2 diodes one to the positive rail and one to the negative rail. To avoid two power supplies a voltage divider across a single 5 volt power supply is used and the center of the divider is considered as ground for the input. To deal with low voltage inputs the full gain of an op amp is used, this will take a small voltage and boost it to either +5 volts ( if the op amp can reach the positive voltage rail ) or 0 volts. Since the op amp is powered on 5 volts its output cannot get so high as to damage the PIC. My first circuit worked just like this and produced junk for output. Apparently a lot of noise crept in near the transition between the voltages, this despite that the signals did not look too bad on an oscilloscope. To avoid this I added some positive feedback to make the circuit a Schmidt trigger. The feedback makes the circuit less sensitive, but dramatically improved the success of the device. Decreasing the the feedback will make the circuit more sensitive but may bring the noise back, increasing the feedback resistor decreases the feedback.

In the schematic only 1 of the two Schmidt triggers is shown. Note that the stepper motor has 4 coils, only two are used. It does not matter which the first coil is, but then next one must be one of the three that is 90 degrees out of phase with the first. If you have scope with xy input, plot the two signals. When the motor is rotated the plot should rotate around the corners of a square.

The PIC part is the circuit is like that of PIC based Stepper Motor Dancing Analog Clock or Stepper Motor Tester Use the inputs as specified in the software.


Schematic

Schematic


Parts -- this is not up to date, working on it

Part Details
Power supply Not shown, 5 volts, you can use a higher voltage with a voltage regulator. There is a suitable power supply in the PIC based Stepper Motor Dancing Analog Clock
PIC16F877A My favorate 16 series part, relatively lots of memory and pins. Bigger than you need, but only about 8 bucks. Try with an 18 series part, should not be hard and will leave you more up to date. Let me know. Other parts of the PIC circuit not discussed here.
Power supply splitting resistors, form a pseodo ground at about 2.5 volts. 100 more or less
RIN about 5 to 10 K
RFB feedback resistor should be about 4 times higher than the input resistor

Command Interface

All commands ( except stop should be terminated with a carriage return ) Note that the command interface is not very smart, giving parameters that are out of range my blow the whole program up. If so reboot the PIC. Do not send a new command ( except stop ) until earlier commands have been completed ( actually you can get ahead some if you are careful )


Command Code Notes, PIC Response
Report version v<cr> Version of the PIC software something like:

Serial Stepper Test ver July 4 2008

Report on all parameters r Delimited by commas something like:
Stop ! Should almost immediately stop long running commands of which there are currently none. Response Stopped.
Other, not understood commands xxx Responds with "!Bad Command = xxx" if the command is not understood.


Notes on terminal program set up.

  • Baud rate should be 19.2K 8N1
  • Most terminal programs can be set to treat a carriage return as a carriage return line feed. Do it.

Some terminal programs will not transmit in lower case ( all our commands are lower case ) unless specially set to do so. Set it to allow lower case.

Microcontroller Program Design

I no longer have the patience for assembly language. I have moved on to C in particularly BoostC, see link below. I like this compiler it has both a free version with some restrictions and a very reasonably priced full version. Writing in C should make the program fairly easy to read. Most of the design should be evident by reading the program, however a few notes here may help.

The idea is to put each activation sequence in a table and then step through that table and activate the corresponding port bits and thus stepper coils. The four wires can be activated in a total of 6 different ways, one table corresponds to each permutation. The particular table to use is set using the permutation command ( p ).

The series of wires to energize is specified in the arrays StepperStepsN where N is the number of the wire permutation. Each step just increments its way through the array wrapping around the the beginning and the end.

Commands are received via an interrupt driven routine, the main loop checks each time around to see if a complete command has been received. Because commands are only interpreted in the main loop all commands are ignored until the program returns to the main loop. The exception to this is the stop command which will terminate a g or x command and return to the main loop quickly. RS232 transmission is not driven by an interrupt and so during transmission from the pic no stepping takes place. Commands which do not result in motion execute very quickly, most of the time is for communications.

Currently drive to the motor is half step drive. This gives twice as many steps per revolution as is labeled on the motor. I plan later to let you select half step, full step or wave drive. See the links below for more information.

Compiling

The zip file contains the entire source bootst project. Unzip into a directory and open in source boost. Set the target to 16F877A. See the comments in the program header for other setup before compiling. After compiling my compiler reports something like:

Memory Usage Report

  • RAM available:368 bytes, used:139 bytes (37.8%), free:229 bytes (62.2%),
  • Heap size:229 bytes, Heap max single alloc:95 bytes
  • ROM available:8192 words, used:2118 words (25.9%), free:6074 words (74.1%)

I think you could contract the program a bit to get under the 2K free compiler limit.



Other Things to Try

Additions/Changes/Modification

  • The 877A is way overkill for this project unless you make it do a bit more. Note that only 2 input pins plus the serial lines are really used.
  • There is nothing magic about the dual op amp I had around, lots of others should work, there are also IC schmidt triggers. Some PIC input ports have schmidt triggers on their inputs. Op amps are good for many other things so nice to have around. You can tweak the parameters of a op amp schmidt trigger in a way you cannot with other circuits. Basic_Circuits_and_Circuit_Building_Blocks#Schmitt_Trigger
  • Optimize the code especially in countEncoder()

Possibly useful links

This program uses my: Serial Communications Library -- BoostC and 16F877A

More information on serial communications with microcontrollers: Microcontroller Serial Communications Articles

A project using a stepper motor as a rotary encoder: Rotary Encoder http://www.webx.dk/oz2cpu/20m/encoder.htm

Using a disk drive motor as encoder: Inexpensive rotary encoder http://users.tkk.fi/~jwagner/electr/rotary-enc/

Info on steppers: Encoder Implementation http://www.seattlerobotics.org/encoder/jun97/encoding.html

Info on steppers: Stepper motors http://www.allaboutcircuits.com/vol_2/chpt_13/5.html

More info on steppers: Basic Stepping Motor Control Circuits http://www.cs.uiowa.edu/~jones/step/circuits.html

A free terminal program, I like this much better than hyperterminal: Welcome to our Free Download/New Products Page! http://www.rs485.com/psoftware.html

BoostC – I think the free version is enough to compile the program: SourceBoost Technologies http://www.sourceboost.com/


Download

pending, not at: Version 1 zip file: StepperTest_v1.zip

Comment, Questions, Contributions?

Email me russ_hensel, or use the talk page for this topic. All feedback is welcome.