Introduction: Mimicking PWM for the Raspberry Pi 2 Sumobot With LabVIEW
While building my Raspberry Pi 2 Sumobot one of the issues I ran into was that the Raspberry Pi 2 does not have built-in PWM. Since PWM is required to control the Su mobots motors I was forced to try and mimic PWM with my LabVIEW program. This is the only difference between the BeagleBone Black, for the full Sumobot code go to this link and for the Sumobot setup code go to this l ink. I have the Raspberry Pi 2 Sumobot code posted at the end of the instructable. LabVIEW 2014 and Linx 3.0 are required to run this code.
Step 1: What Is PWM
Pulse width modulation (PWM) is a term for describing a type of digital signal. The term duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time; a low duty cycle corresponds to low power because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
Step 2: Setting the Duty Cycle
When the signal is high, we call this “on time”. To describe the amount of “on time” , we use the concept of duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform.
If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. Here is a graph that illustrates these three scenarios:
100% duty cycle would be the same as setting the voltage to 5 Volts (high). 0% duty cycle would be the same as grounding the signal.
Step 3: Raspberry Pi
To simulate PWM I set up a while loop with 2 digital writes, the first digital write turns the digital output on and the next output turns it back off. A time delay is placed after the first digital write and another is placed after the second. This allows us to control the duty cycle.
For this situation I chose 5 milliseconds(.005 seconds) as my period, I tried several different periods and this one worked best. The "right speed" local variable in the picture above is a number between 0-1, when the right joystick is neutral it will read 0 but it will increase when it is moved with 1 being fully forward or backwards. It set up so the right speed value will come out and multiply the period, and the result will set the first time delay. I then subtract the right speed value from 1 and then multiply it by the same period and have this value set the second time delay.