User Manual
31
repetitions is just one step up or down in motor speed: 400 repetitions at 2000 Hertz represents just
0.2 seconds.
Then we define the run_loop() function
run_loop(startloop, endloop, step, port_num, printchar)
The arguments startloop and endloop are the % time for the switched port to be ON at the start
and end of the loop. step is the size of the increment for each successive loop. port_num is the
port we‟re switching (17 or 18). printchar is the acceleration/deceleration indication character
('+' or '-'). The run_loop() function handles the repeated calls to the run_motor() function
causing it to run its 400 cycles for each set of values defined.
After the functions are defined, wiring instructions are printed out and the computer waits for a key
press of enter to confirm the user is ready. Then the run_loop() function is called four times to
drive the motor speed from 5% to 95% and back again in each direction. After this, both ports are set
to OFF (False) and then reset.
Suggested safe tweaks to experiment with. Try changing these one at a time and see what they do...
run_loop(5, 95, 1, 18,'+') – change + to something else
run_loop(5, 95, 1, 18,'+') – change 5 to a higher number < 95
run_loop(5, 95, 1, 18,'+') – change 95 to a lower number (but still greater than
what you changed the 5 to)
motor-wp.py (hardware PWM)
The Raspberry Pi has one available hardware PWM port (GPIO18). We will use port 17 to control
motor direction and port 18 to handle the pulsing.
The first part of the program imports the required modules, including the Python 3 print() function
(explained above in the section on motor-rg.py). After initialising WiringPi, port 18 is set to
PWM mode with wiringpi.pinMode(18,2) and port 17 is set up for normal output. Then both
ports are set to 0 (OFF). Wiring instructions are then printed out and the computer waits for user
confirmation. When the user is ready, the program defines three functions that are used repeatedly.
display(printchar) handles the correct display of the motor acceleration/deceleration
indicators using the imported Python 3 print() function.
reset_ports() handles the resetting of the ports on program exit. This is not built into WiringPi
for Python so it needs to be defined here. This is particularly important in the motor program as it
avoids an uncontrollable “motor running” situation on program exit. This is an important safety
consideration if you‟re going to run things with propellers etc.
loop(start_pwm, stop_pwm, step, printchar) handles the main PWM control loop.
As before, this is called in four different ways (increasing speed then decreasing speed with motor
going one way, then increasing and decreasing speed with motor going the other way). start_pwm
is the 0-1024 PWM value to start the loop with. stop_pwm is the 0-1024 PWM value to end the loop