I intend to build a robot which will take my trash bin to the curb and back. I have several sensors in play. It will use three ultrasonic sensors on the front and sides to avoid collisions. It will also have a GPS module to navigate from start to curb and back. There are some other details, but not important at this point. I have several existing parts I intend to use for this project. I have a RPi 0w. I also have two good size motors and a Sabertooth 2x25 motor controller. I'm working on this in steps. I already have bread-boarded and tested the ultrasonic sensors. Now I'm working on the motion. I've used the code below to test the use of PWM to drive the motors through the Sabertooth.
(https://www.dimensionengineering.com/da ... th2x25.pdf) The code starts the motors at full forward and steps down to a stop, and then back up to full reverse. At least that is what it is supposed to do. When it gets to a duty cycle of 50% it is supposed to be at full stop, but it isn't. It jerks forward and reverse.
What am I missing in the use of PWM?
I've already considered switching to serial, but will need those pins for the GPS. Besides, I'd really like to understand how to make this work.
Thank you in advance for responses.
Code: Select all
#!/usr/bin/env python import RPi.GPIO as GPIO import time GPIO.cleanup() LedPin = 12 LedPin2 = 35 GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output GPIO.output(LedPin, GPIO.LOW) # Set pin to low(0V) GPIO.setup(LedPin2, GPIO.OUT) # Set pin mode as output GPIO.output(LedPin2, GPIO.LOW) # Set pin to low(0V) p = GPIO.PWM(LedPin, 1000) # set Frequece to 1KHz p2 = GPIO.PWM(LedPin2, 1000) # set Frequece to 1KHz p.start(50) # Start PWM output, Duty Cycle = 50 p2.start(50) try: print('start') while True: for dc in range(100, -1, -10): # Decrease duty cycle: 100~0 p.ChangeDutyCycle(dc) # Change duty cycle p2.ChangeDutyCycle(dc) print('Duty Cycle = ', dc) time.sleep(5) p.stop() GPIO.output(LedPin, GPIO.HIGH) # turn off all leds except KeyboardInterrupt: p.stop() GPIO.output(LedPin, GPIO.HIGH) # turn off all leds p2.stop() GPIO.output(LedPin2, GPIO.HIGH) # turn off all leds GPIO.cleanup()