Hardware Reference
In-Depth Information
Entering F starts the motor running, in the forward (clockwise) direction. To speed
it up, press > while it is running, or prior to starting it. Pressing the same direction
command F toggles the motor off again. Alternatively, S is available to stop the motor
if that seems more intuitive. The R command starts the motor in the reverse direction.
Pressing R again stops it. Direction can be changed while the motor is running. This tests
how well it recovers when operated at higher speeds.
Program Internals
This program requires the use of a thread to run the motor in free-running mode. This
design allows the main program to continue to read user commands from the keyboard
while supplying the motor with stepping commands. The user can change the stepping
speed, reverse the motor, or stop the motor.
The main user input dispatch loop is in the main program (lines 230 to 305). The
threaded code resides in lines 125 to 140. Unless the free-running F or R commands are in
effect, the thread blocks in line 131, waiting for a command. Once a command is received,
the loop in lines 133 to 136 keeps the motor stepping, until the main loop sets the stop flag.
The mutex and cond variables (lines 36 and 37) provide a simple arrangement
to implement a queue from the main thread to the free-running thread. The queue
get function is implemented in lines 105 to 119. The code must first successfully lock
the mutex in line 109. Once that is accomplished, the while loop in lines 111 and 112
is executed. If the cmd variable is still zero, this indicates that no command has been
queued. When that happens, pthread_cond_wait() in line 112 is executed. This unlocks
the mutex and blocks the execution of the program. Control blocks until the cond variable
is signaled in line 158. When control returns from pthread_cond_wait(3) , the kernel has
relocked the mutex .
Queuing the command occurs in the routine queue_cmd() (lines 146 to 159). After
locking the mutex (line 149), the while loop checks whether the cmd variable is nonzero.
If it is, this indicates that the motor thread has not received the last command yet, and
control blocks in the pthread_cond_wait() call (line 152). Again, when control blocks,
the kernel releases the mutex . The cond variable is signaled from line 117, when the
command is taken off the one-element queue.
The stepping functions are performed by the routine step() in lines 73 to 91. The
motor drive is disabled in line 86 so the GPIO signals can be changed (line 87). Once the
new GPIO output settings are established, the drive to the motor is enabled in line 88.
If you choose to use different GPIO pins for this project, change the constant
declarations in lines 23 to 27.
1 /*********************************************************************
2 * bipolar.c : Drive a bipolar stepper motor
3 *********************************************************************/
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
 
Search WWH ::




Custom Search