View: 416|Reply: 6

Rasptank control issues, I suspect move.py settings

[Copy link]

1

Threads

1

Posts

5

Credits

Newbie

Rank: 1

Credits
5
Post time 2021-6-15 21:28:22 | Show all posts |Read mode
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:

  1. pi@raspberrypi:~/adeept_rasptank/server $ cat move.py
  2. #!/usr/bin/env python3
  3. # File name   : move.py
  4. # Description : Control Motor
  5. # Product     : GWR
  6. # Website     : www.gewbot.com
  7. # Author      : William
  8. # Date        : 2019/07/24
  9. import time
  10. import RPi.GPIO as GPIO

  11. # motor_EN_A: Pin7  |  motor_EN_B: Pin11
  12. # motor_A:  Pin8,Pin10    |  motor_B: Pin13,Pin12

  13. Motor_A_EN    = 4
  14. Motor_B_EN    = 17

  15. Motor_A_Pin1  = 14
  16. Motor_A_Pin2  = 15
  17. Motor_B_Pin1  = 27
  18. Motor_B_Pin2  = 18

  19. Dir_forward   = 0
  20. Dir_backward  = 1

  21. left_forward  = 0
  22. left_backward = 1

  23. right_forward = 0
  24. right_backward= 1

  25. pwm_A = 0
  26. pwm_B = 0

  27. def motorStop():#Motor stops
  28.         GPIO.output(Motor_A_Pin1, GPIO.LOW)
  29.         GPIO.output(Motor_A_Pin2, GPIO.LOW)
  30.         GPIO.output(Motor_B_Pin1, GPIO.LOW)
  31.         GPIO.output(Motor_B_Pin2, GPIO.LOW)
  32.         GPIO.output(Motor_A_EN, GPIO.LOW)
  33.         GPIO.output(Motor_B_EN, GPIO.LOW)


  34. def setup():#Motor initialization
  35.         global pwm_A, pwm_B
  36.         GPIO.setwarnings(False)
  37.         GPIO.setmode(GPIO.BCM)
  38.         GPIO.setup(Motor_A_EN, GPIO.OUT)
  39.         GPIO.setup(Motor_B_EN, GPIO.OUT)
  40.         GPIO.setup(Motor_A_Pin1, GPIO.OUT)
  41.         GPIO.setup(Motor_A_Pin2, GPIO.OUT)
  42.         GPIO.setup(Motor_B_Pin1, GPIO.OUT)
  43.         GPIO.setup(Motor_B_Pin2, GPIO.OUT)

  44.         motorStop()
  45.         try:
  46.                 pwm_A = GPIO.PWM(Motor_A_EN, 1000)
  47.                 pwm_B = GPIO.PWM(Motor_B_EN, 1000)
  48.         except:
  49.                 pass


  50. def motor_left(status, direction, speed):#Motor 2 positive and negative rotation
  51.         if status == 0: # stop
  52.                 GPIO.output(Motor_B_Pin1, GPIO.LOW)
  53.                 GPIO.output(Motor_B_Pin2, GPIO.LOW)
  54.                 GPIO.output(Motor_B_EN, GPIO.LOW)
  55.         else:
  56.                 if direction == Dir_backward:
  57.                         GPIO.output(Motor_B_Pin1, GPIO.HIGH)
  58.                         GPIO.output(Motor_B_Pin2, GPIO.LOW)
  59.                         pwm_B.start(100)
  60.                         pwm_B.ChangeDutyCycle(speed)
  61.                 elif direction == Dir_forward:
  62.                         GPIO.output(Motor_B_Pin1, GPIO.LOW)
  63.                         GPIO.output(Motor_B_Pin2, GPIO.HIGH)
  64.                         pwm_B.start(0)
  65.                         pwm_B.ChangeDutyCycle(speed)


  66. def motor_right(status, direction, speed):#Motor 1 positive and negative rotation
  67.         if status == 0: # stop
  68.                 GPIO.output(Motor_A_Pin1, GPIO.LOW)
  69.                 GPIO.output(Motor_A_Pin2, GPIO.LOW)
  70.                 GPIO.output(Motor_A_EN, GPIO.LOW)
  71.         else:
  72.                 if direction == Dir_forward:#
  73.                         GPIO.output(Motor_A_Pin1, GPIO.HIGH)
  74.                         GPIO.output(Motor_A_Pin2, GPIO.LOW)
  75.                         pwm_A.start(100)
  76.                         pwm_A.ChangeDutyCycle(speed)
  77.                 elif direction == Dir_backward:
  78.                         GPIO.output(Motor_A_Pin1, GPIO.LOW)
  79.                         GPIO.output(Motor_A_Pin2, GPIO.HIGH)
  80.                         pwm_A.start(0)
  81.                         pwm_A.ChangeDutyCycle(speed)
  82.         return direction


  83. def move(speed, direction, turn, radius=0.6):   # 0 < radius <= 1  
  84.         #speed = 100
  85.         if direction == 'forward':
  86.                 if turn == 'right':
  87.                         motor_left(0, left_backward, int(speed*radius))
  88.                         motor_right(1, right_forward, speed)
  89.                 elif turn == 'left':
  90.                         motor_left(1, left_forward, speed)
  91.                         motor_right(0, right_backward, int(speed*radius))
  92.                 else:
  93.                         motor_left(1, left_forward, speed)
  94.                         motor_right(1, right_forward, speed)
  95.         elif direction == 'backward':
  96.                 if turn == 'right':
  97.                         motor_left(0, left_forward, int(speed*radius))
  98.                         motor_right(1, right_backward, speed)
  99.                 elif turn == 'left':
  100.                         motor_left(1, left_backward, speed)
  101.                         motor_right(0, right_forward, int(speed*radius))
  102.                 else:
  103.                         motor_left(1, left_backward, speed)
  104.                         motor_right(1, right_backward, speed)
  105.         elif direction == 'no':
  106.                 if turn == 'right':
  107.                         motor_left(1, left_backward, speed)
  108.                         motor_right(1, right_forward, speed)
  109.                 elif turn == 'left':
  110.                         motor_left(1, left_forward, speed)
  111.                         motor_right(1, right_backward, speed)
  112.                 else:
  113.                         motorStop()
  114.         else:
  115.                 pass




  116. def destroy():
  117.         motorStop()
  118.         GPIO.cleanup()             # Release resource


  119. if __name__ == '__main__':
  120.         try:
  121.                 speed_set = 60
  122.                 setup()
  123.                 move(speed_set, 'forward', 'no', 0.8)
  124.                 time.sleep(1.3)
  125.                 motorStop()
  126.                 destroy()
  127.         except KeyboardInterrupt:
  128.                 destroy()

  129. pi@raspberrypi:~/adeept_rasptank/server $ ifconfig
Copy the Code
Reply

Use magic Report

2

Threads

6

Posts

36

Credits

Newbie

Rank: 1

Credits
36
Post time 2021-10-21 02:01:55 | Show all posts
have you tried to swap Dir_forward =0 to =1 and same for Dir_backward =1 to =0 ?
Reply

Use magic Report

0

Threads

3

Posts

11

Credits

Newbie

Rank: 1

Credits
11
Post time 2021-12-13 12:39:43 | Show all posts
I had a similar problem.  If the forward/backward were correct, left and right and if I fixed left and right, forward/backward were wrong.  I swapped the pin numbers to fix it.

Motor_A_EN    = 4
Motor_B_EN    = 17

Motor_A_Pin1  = 14
Motor_A_Pin2  = 15
Motor_B_Pin1  = 27
Motor_B_Pin2  = 18
Reply

Use magic Report

0

Threads

6

Posts

26

Credits

Newbie

Rank: 1

Credits
26
Post time 2022-1-23 01:54:06 | Show all posts
They got it wrong in this block:

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)

If you want to turn right, the right side needs to move backward and the left side needs to move forward. They mixed it up. Change the four lines and everything will be alright.

elif direction == 'no':
                if turn == 'right':
                        motor_left(1, left_forward, speed)
                        motor_right(1, right_backward, speed)
                elif turn == 'left':
                        motor_left(1, left_backward, speed)
                        motor_right(1, right_forward, speed)
Reply

Use magic Report

0

Threads

4

Posts

14

Credits

Newbie

Rank: 1

Credits
14
Post time 2022-4-11 15:52:32 | Show all posts
best post
Reply

Use magic Report

0

Threads

4

Posts

14

Credits

Newbie

Rank: 1

Credits
14
Post time 2022-4-11 15:56:40 | Show all posts
This is very informative coding, I can use this coding in my website
Expert Book Writers
Reply

Use magic Report

0

Threads

16

Posts

39

Credits

Administrator

Rank: 9Rank: 9Rank: 9

Credits
39
Post time 2022-5-5 17:21:58 | Show all posts
You can modify lines 22-23 of adeept_rasptank/server/move.py.
Changeļ¼š
Dir_forward = 0
Dir_backward = 1
To:
Dir_forward = 1
Dir_backward = 0

Please see the Q&A in the link: https://www.adeept.com/learn/tutorial-389.html
Reply

Use magic Report

You have to log in before you can reply Login | Sign Up

Points Rules