|
I have a Rasptank that turns left when commanded to turn left and turns right when commanded to turn right, but, moves forwards when commanded to move backwards and moves backwards when commanded to move forwards. Do you know what the issue is and how to resolve it. I have attached the move.py file for reference:
- pi@raspberrypi:~/adeept_rasptank/server $ cat move.py
- #!/usr/bin/env python3
- # File name : move.py
- # Description : Control Motor
- # Product : GWR
- # Website : www.gewbot.com
- # Author : William
- # Date : 2019/07/24
- import time
- import RPi.GPIO as GPIO
- # motor_EN_A: Pin7 | motor_EN_B: Pin11
- # motor_A: Pin8,Pin10 | motor_B: Pin13,Pin12
- Motor_A_EN = 4
- Motor_B_EN = 17
- Motor_A_Pin1 = 14
- Motor_A_Pin2 = 15
- Motor_B_Pin1 = 27
- Motor_B_Pin2 = 18
- Dir_forward = 0
- Dir_backward = 1
- left_forward = 0
- left_backward = 1
- right_forward = 0
- right_backward= 1
- pwm_A = 0
- pwm_B = 0
- def motorStop():#Motor stops
- GPIO.output(Motor_A_Pin1, GPIO.LOW)
- GPIO.output(Motor_A_Pin2, GPIO.LOW)
- GPIO.output(Motor_B_Pin1, GPIO.LOW)
- GPIO.output(Motor_B_Pin2, GPIO.LOW)
- GPIO.output(Motor_A_EN, GPIO.LOW)
- GPIO.output(Motor_B_EN, GPIO.LOW)
- def setup():#Motor initialization
- global pwm_A, pwm_B
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(Motor_A_EN, GPIO.OUT)
- GPIO.setup(Motor_B_EN, GPIO.OUT)
- GPIO.setup(Motor_A_Pin1, GPIO.OUT)
- GPIO.setup(Motor_A_Pin2, GPIO.OUT)
- GPIO.setup(Motor_B_Pin1, GPIO.OUT)
- GPIO.setup(Motor_B_Pin2, GPIO.OUT)
- motorStop()
- try:
- pwm_A = GPIO.PWM(Motor_A_EN, 1000)
- pwm_B = GPIO.PWM(Motor_B_EN, 1000)
- except:
- pass
- def motor_left(status, direction, speed):#Motor 2 positive and negative rotation
- if status == 0: # stop
- GPIO.output(Motor_B_Pin1, GPIO.LOW)
- GPIO.output(Motor_B_Pin2, GPIO.LOW)
- GPIO.output(Motor_B_EN, GPIO.LOW)
- else:
- if direction == Dir_backward:
- GPIO.output(Motor_B_Pin1, GPIO.HIGH)
- GPIO.output(Motor_B_Pin2, GPIO.LOW)
- pwm_B.start(100)
- pwm_B.ChangeDutyCycle(speed)
- elif direction == Dir_forward:
- GPIO.output(Motor_B_Pin1, GPIO.LOW)
- GPIO.output(Motor_B_Pin2, GPIO.HIGH)
- pwm_B.start(0)
- pwm_B.ChangeDutyCycle(speed)
- def motor_right(status, direction, speed):#Motor 1 positive and negative rotation
- if status == 0: # stop
- GPIO.output(Motor_A_Pin1, GPIO.LOW)
- GPIO.output(Motor_A_Pin2, GPIO.LOW)
- GPIO.output(Motor_A_EN, GPIO.LOW)
- else:
- if direction == Dir_forward:#
- GPIO.output(Motor_A_Pin1, GPIO.HIGH)
- GPIO.output(Motor_A_Pin2, GPIO.LOW)
- pwm_A.start(100)
- pwm_A.ChangeDutyCycle(speed)
- elif direction == Dir_backward:
- GPIO.output(Motor_A_Pin1, GPIO.LOW)
- GPIO.output(Motor_A_Pin2, GPIO.HIGH)
- pwm_A.start(0)
- pwm_A.ChangeDutyCycle(speed)
- return direction
- def move(speed, direction, turn, radius=0.6): # 0 < radius <= 1
- #speed = 100
- if direction == 'forward':
- if turn == 'right':
- motor_left(0, left_backward, int(speed*radius))
- motor_right(1, right_forward, speed)
- elif turn == 'left':
- motor_left(1, left_forward, speed)
- motor_right(0, right_backward, int(speed*radius))
- else:
- motor_left(1, left_forward, speed)
- motor_right(1, right_forward, speed)
- elif direction == 'backward':
- if turn == 'right':
- motor_left(0, left_forward, int(speed*radius))
- motor_right(1, right_backward, speed)
- elif turn == 'left':
- motor_left(1, left_backward, speed)
- motor_right(0, right_forward, int(speed*radius))
- else:
- motor_left(1, left_backward, speed)
- motor_right(1, right_backward, speed)
- elif direction == 'no':
- if turn == 'right':
- motor_left(1, left_backward, speed)
- motor_right(1, right_forward, speed)
- elif turn == 'left':
- motor_left(1, left_forward, speed)
- motor_right(1, right_backward, speed)
- else:
- motorStop()
- else:
- pass
- def destroy():
- motorStop()
- GPIO.cleanup() # Release resource
- if __name__ == '__main__':
- try:
- speed_set = 60
- setup()
- move(speed_set, 'forward', 'no', 0.8)
- time.sleep(1.3)
- motorStop()
- destroy()
- except KeyboardInterrupt:
- destroy()
- pi@raspberrypi:~/adeept_rasptank/server $ ifconfig
Copy the Code |
|