11_motorControl.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. #version : 2023.08.18
  3. #language : en
  4. #hardware : pi
  5. from maix import display
  6. from maix import image
  7. from maix import camera
  8. import smbus2
  9. import time
  10. import sys
  11. sys.path.append("/root/")
  12. from CocoPi import stm8s
  13. iic_slaver=stm8s()
  14. iic_slaver.clear()
  15. del iic_slaver
  16. from CocoPi import dcMotor
  17. from CocoPi import BUTTON
  18. key_D = BUTTON(7)
  19. key_B = BUTTON(8)
  20. key_C = BUTTON(13)
  21. D1=dcMotor(1)
  22. D2=dcMotor(2)
  23. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  24. camera.camera.config(size=(320,240))
  25. ScreenOrientation = False
  26. speed=0
  27. rotation=1
  28. addValue=1
  29. angle=0
  30. canvas = image.new(size = (320, 240))
  31. while True:
  32. if (key_B.is_pressed()):
  33. while key_B.is_pressed():
  34. time.sleep(0.001)
  35. rotation=(-1)*rotation
  36. if(key_C.is_pressed()):
  37. if speed<255:
  38. addValue=1
  39. else:
  40. addValue=0
  41. elif key_D.is_pressed():
  42. if speed>0:
  43. addValue=-1
  44. else:
  45. addValue=0
  46. else:
  47. addValue=0
  48. canvas = image.new(color=(0,0,0),size = (320, 240))
  49. speed=speed+addValue
  50. canvas.draw_string(80, 8, "speed:"+str(speed), 1, color=(0, 255, 0))
  51. if rotation==1:
  52. canvas.draw_image((image.open("/root/preset/img/clockwise_bfbfbf_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  53. #canvas.draw_string(40, 24, "rotation:clockwise", 1, color=(0, 255, 0))
  54. else:
  55. canvas.draw_image((image.open("/root/preset/img/anticlockwise_bfbfbf_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  56. #canvas.draw_string(40, 24, "rotation:anticlockwise", 1, color=(0, 255, 0))
  57. angle=angle-int(rotation*speed/3)
  58. if angle>3600:
  59. angle=angle-3600
  60. elif angle<-3600:
  61. angle=angle+3600
  62. else:
  63. angle=angle
  64. #canvas.draw_string(4, 180, "C:change fan's direction of rotation", 1, color=(0, 255, 0))
  65. #canvas.draw_string(4, 200, "D:increase fan's speed of rotation", 1, color=(0, 255, 0))
  66. #canvas.draw_string(4, 220, "B:decrease fan's speed of rotation", 1, color=(0, 255, 0))
  67. canvas.draw_image((image.open("/root/preset/img/ttMotor_200_64.png")).rotate(0, adjust=1),80,90,alpha=1)
  68. canvas.draw_image((image.open("/root/preset/img/fan_140_140.png")).rotate(angle, adjust=0),60,52,alpha=1)
  69. canvas.draw_image((image.open("/root/preset/img/increase_bfbfbf_24x24.png")).rotate(0, adjust=0),288,8,alpha=1)
  70. canvas.draw_image((image.open("/root/preset/img/decrease_bfbfbf_24x24.png")).rotate(0, adjust=0),8,8,alpha=1)
  71. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  72. if(speed<80):
  73. D1.dcMotorCtrl(rotation,speed)
  74. D2.dcMotorCtrl(rotation,speed)
  75. else:
  76. if rotation==1:
  77. D1.dcMotorCtrl(1,speed)
  78. D2.dcMotorCtrl(1,speed)
  79. else:
  80. D1.dcMotorCtrl(0,speed)
  81. D2.dcMotorCtrl(0,speed)
  82. if ScreenOrientation:
  83. canvasVER = canvas.crop(0,0,240,320)
  84. canvasVER = canvasVER.rotate(-90, adjust=1)
  85. display.show(canvasVER)
  86. else:
  87. display.show(canvas)