This 3rd part is dedicated to the sw .
I use an approach object-oriented, so I prefer to create for each real component a equivalent sw class object. It means that I try to include in the code all the parameters necessary to describe the item and its behavior.
The advantage of this approach is that if I need 20 motors, I just initialize 20 istances of my DCMotor Class.
The translation of this concept is very simple:Which parameters can describe can describe my motor?
The fundamental information is: which is its speed? and than which are the limits of this speed?
Again, How phisically can control the motor, so which pins are used to set the speed?
All of this information are included in the init() of my class:
def __init__(self, name, MBack, MForw, channel1, channel2, WMin=-100, WMax=100, Wstall=30, debug=True,simulation=False):
MBack is the GPIO pin used to move the motor Backward, channel1 is the DMA channel used for it.
MForw is the pin for motor Forward. channel2 is the DMA channel used for it.
Wmin and Wmax is intuitive…
WStall is the minimun speed that cause the motor not to move.
The actions that can be done onmy motor are:
1)Start the motor – in reality this just initialize the GPIO channels and pins.
def start(self):
2)Stop the motor – stop the GPIO channel.
def stop(self):
3)Set the speed W – check the speed is inside the limits,check if the motor is requested to move backward or forward , reset the unsued pin and set the pulse width of the correct pin.
def setW(self, W)
Finally by calling in a main routine those functions you can move your motor:
myDCmotor = DCmotor('myMotor', 18, 23, 11, 12) myDCmotor.start() myDCmotor.set(30) #accelerate myDCmotor.set(80) #deelerate myDCmotor.set(50) #move backward myDCmotor.set(-40) #brake myDCmotor.set(0) #stop #myDCmotor.stop()
You can download the code from Github-solenerotech/myRover
NOTES:During the programming , also when I added other devices like picmaera and ultrasound sensor I notice some bad behaviour on the GPIO system.For this reason you can find in the code some choices that are explained by this problems.
1)I notice that it is necessary to use one channel for each pin. Otherwise the pulse width is inverded ( if I set W=90% it runs 10% ans so on). Thats expalin channel1 and channel2.
2)the DMA channels are used to create the PWM. But the DMA channels in rpi are used also for other activity. So you need to avoid some of them, in particular DMA channel 0 is used for the system and DMA channel 2 is used for the sdcard. So avoid those 2 channels.
3)In order to generate a PWM you can use hardware clock or sw clock. The hw clock is used also by Picamera, so this can generate an interference to the picam itself. To avoid this I’m using the sw clock on the PWM. This is obtained by using in the PWM setuput the option delay_hw=PWM.DELAY_VIA_PCM