main_rtt.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import PikaStdLib
  2. import pikaRTThread
  3. import pikaRTDevice
  4. import PikaDebug
  5. thread = pikaRTThread.Thread()
  6. mem = PikaStdLib.MemChecker()
  7. pdb = PikaDebug.Debuger()
  8. print('mem use max:')
  9. mem.max()
  10. # A2
  11. Ain1 = pikaRTDevice.GPIO()
  12. Ain1.setId(16)
  13. Ain1.setMode('out')
  14. Ain1.enable()
  15. # A4
  16. Ain2 = pikaRTDevice.GPIO()
  17. Ain2.setId(20)
  18. Ain2.setMode('out')
  19. Ain2.enable()
  20. # A6
  21. Apwm = pikaRTDevice.PWM()
  22. Apwm.setName('tim3pwm1')
  23. Apwm.setChannel(1)
  24. Apwm.setFreq(1000)
  25. Apwm.setDuty(0)
  26. Apwm.enable()
  27. # A3
  28. Bin1 = pikaRTDevice.GPIO()
  29. Bin1.setId(17)
  30. Bin1.setMode('out')
  31. Bin1.enable()
  32. # A5
  33. Bin2 = pikaRTDevice.GPIO()
  34. Bin2.setId(21)
  35. Bin2.setMode('out')
  36. Bin2.enable()
  37. # A7
  38. Bpwm = pikaRTDevice.PWM()
  39. Bpwm.setName('tim3pwm2')
  40. Bpwm.setChannel(2)
  41. Bpwm.setFreq(1000)
  42. Bpwm.setDuty(0)
  43. Bpwm.enable()
  44. #PC4
  45. sensor1 = pikaRTDevice.GPIO()
  46. sensor1.setId(24)
  47. sensor1.setMode('in')
  48. sensor1.setPull('up')
  49. sensor1.enable()
  50. #PC5
  51. sensor2 = pikaRTDevice.GPIO()
  52. sensor2.setId(25)
  53. sensor2.setMode('in')
  54. sensor2.setPull('up')
  55. sensor2.enable()
  56. def motor1(speed: float):
  57. if speed > 0:
  58. Ain1.high()
  59. Ain2.low()
  60. Apwm.setDuty(speed)
  61. else:
  62. Ain1.low()
  63. Ain2.high()
  64. Apwm.setDuty(-speed)
  65. def motor2(speed: float):
  66. if speed > 0:
  67. Bin1.high()
  68. Bin2.low()
  69. Bpwm.setDuty(speed)
  70. else:
  71. Bin1.low()
  72. Bin2.high()
  73. Bpwm.setDuty(-speed)
  74. def go():
  75. while True:
  76. if not sensor2.read():
  77. print('turn right...')
  78. motor1(0.6)
  79. motor2(-0.6)
  80. elif not sensor1.read():
  81. print('turn left...')
  82. motor1(-0.6)
  83. motor2(0.6)
  84. else:
  85. print('forword...')
  86. motor1(0.6)
  87. motor2(0.6)
  88. go()