Tank.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from Wheel import Wheel
  2. import wiringpi
  3. class Tank:
  4. # 初始速度为0
  5. __speed=0
  6. # 记录小车状态
  7. __status='stop'
  8. # 转向PWM增益
  9. __turningPWMIncrease=1.4
  10. def __init__(self):
  11. wiringpi.wiringPiSetup()
  12. self.__left=Wheel(pin1=23,pin2=22,pwm=25)
  13. self.__right=Wheel(pin1=28,pin2=29,pwm=26)
  14. def forward(self):
  15. self.__status='forward'
  16. self.setSpeed(self.__speed)
  17. self.__left.forward()
  18. self.__right.forward()
  19. def backward(self):
  20. self.__status='backward'
  21. self.setSpeed(self.__speed)
  22. self.__left.backward()
  23. self.__right.backward()
  24. def left(self):
  25. self.__status='left'
  26. self.setSpeed(self.__speed)
  27. self.__left.backward()
  28. self.__right.forward()
  29. def right(self):
  30. self.__status='right'
  31. self.setSpeed(self.__speed)
  32. self.__left.forward()
  33. self.__right.backward()
  34. def stop(self):
  35. self.__status='stop'
  36. self.__left.stop()
  37. self.__right.stop()
  38. def speedUp(self):
  39. self.__speed=self.__speed+5
  40. if self.__speed>50:
  41. self.__speed=50
  42. self.setSpeed(self.__speed)
  43. def speedDown(self):
  44. self.__speed=self.__speed-5
  45. if self.__speed<15:
  46. self.__speed=15
  47. self.__left.setSpeed(self.__speed)
  48. def setSpeed(self,speed):
  49. self.__speed=speed
  50. self.__setRunSpeed(self.__speed)
  51. def __setRunSpeed(self,speed):
  52. if self.__status=='left' or self.__status=='right':
  53. # 如果是转弯,要给加大速度,才能用同样的速度转动
  54. turnSpeed = int(self.__speed*self.__turningPWMIncrease)
  55. print("转向速度:"+str(turnSpeed))
  56. self.__left.setSpeed(turnSpeed)
  57. self.__right.setSpeed(turnSpeed)
  58. else:
  59. self.__left.setSpeed(self.__speed)
  60. self.__right.setSpeed(self.__speed)