当前位置:   article > 正文

ESP32(MicroPython) 两轮差速五自由度机械臂小车_micropython pca9685驱动报错

micropython pca9685驱动报错

这次的项目在软件上没多少调整,但本人希望分享一下硬件上的经验。

小车使用两轮差速底盘,驱动轮在小车中间,前后都要万向轮。这种形式可以实现0转弯半径,但受万向轮及用于加高的铜柱的规格限制,两个万向轮难以调到相同高度。考虑铜柱规格,本人万向轮调到比后面的万向轮略高,这样在向前加速时也更稳定。底盘的底板和盖板均使用4mm亚克力板,强度足够。由于重量较轻,在机械臂未指向正前方时可能出现一边车轮抓地效果不好的情况,建议先调整机械臂再行进。

 

底盘采用闭环控制,由于对实时性要求高,不适合用同一块主控运行网页,因此使用另一块主控提供网页,并通过串口转发指令,此外还开启热点以便在户外连接。由于要给两块主控和两个编码器供电,对12v电池降压有较大压降,AMS1117不能满足需要,就改用性能更高的LM7805降压模块。由于专门的编码器电机驱动尚无标准化设计,性价比较低,本人使用杜邦线连接编码器电机的接口,电机驱动使用经典的L298N。由于搭载机械臂增大了负载,底盘的PID参数调到P=20、I=2,实测相应速度和稳定性良好。

 机械臂使用PCA9685进行驱动,舵机为MG996R,由于希望达到6v电压以充分发挥舵机性能,本人尝试了6v电池和使用LM7806对7.4v电池降压的方案,均不能正常运行。之后使用LM7805对7.4v电池降压输出到主控,并由主控输出5v电压到PCA9685,也不能正常运行。最后改用充电宝给主控供电并由主控输出5v电压到PCA9685,可以正常运行。出现这种情况可能是因为PCA9685对电压稳定性要求较高。由于MG996R舵机的线不够长,有3个舵机使用了舵机延长线。为了方便操作,控制机械臂的主控也连接到底盘上的主控发出的热点。本人测试过把这两部分程序合并,但不能正常加载网页,就分两块主控。

 相应程序

控制电机的ESP32

  1. from machine import *
  2. import time
  3. from motos import *
  4. import _thread
  5. '''
  6. 底盘参数计算(最终结果保留3位有效数字)
  7. (speed为编码器脉冲计数)
  8. 轮直径:0.065
  9. 轮周长:0.2041
  10. 每圈脉冲数:110
  11. 底盘周长:0.6908
  12. 底盘直径/每rad长:0.22
  13. 每次脉冲长度:0.001855
  14. 线速度:speed*10*0.001855=speed*0.0186
  15. 角速度:(speed1-speed2)*10*0.001855/0.22=(speed1-speed2)*0.0843
  16. 考虑丢步情况,大约只有63%的脉冲能被统计,因此公式改为:
  17. 线速度=speed/0.63*0.0186=speed*0.0295
  18. 角速度=(speed1-speed2)/0.63*0.0843=(speed1-speed2)*0.134
  19. 每旋转1度(一边轮静止,另一边旋转)所需脉冲数=0.22*2*3.14/360/0.00186*0.63=1.30
  20. 每行驶1米所需脉冲数:1/0.001855*0.63=340
  21. '''
  22. # 编码器初始化
  23. pin17 = Pin(17, Pin.IN)
  24. pin5 = Pin(5, Pin.IN)
  25. encoder1 = encoder1(pin5, pin17, 0) # 参数(编码器A相引脚,编码器B相引脚,定时器序号)
  26. pin19 = Pin(19, Pin.IN)
  27. pin18 = Pin(18, Pin.IN)
  28. encoder2 = encoder2(pin18, pin19, 2)
  29. # 电机初始化
  30. motor1=PWM(Pin(15),freq=1000,duty=0)
  31. motor2=PWM(Pin(2),freq=1000,duty=0)
  32. motor3=PWM(Pin(4),freq=1000,duty=0)
  33. motor4=PWM(Pin(16),freq=1000,duty=0)
  34. duty1=0
  35. duty2=0
  36. linear_velocity=0
  37. angular_velocity=0
  38. target1=0
  39. target2=0
  40. offset1=0
  41. offset2=0
  42. '''
  43. distance=0
  44. angle=0
  45. target_distance=0
  46. target_angle=0
  47. flag=0
  48. '''
  49. def set_target(duty1,duty2):
  50. global linear_velocity
  51. global angular_velocity
  52. global target1
  53. global target2
  54. global target_distance
  55. global target_angle
  56. global flag
  57. while True:
  58. try:
  59. target=int(input("input"))
  60. ''' #控制底盘旋转一定角度并前进一定距离,实测不能正常运行,已弃用
  61. if target//1000>600: #前3位组成的数大于600时控制行驶距离和旋转角度
  62. target_distance=target%1000 #后三位为距离*100
  63. target_angle=target//1000-800 #前三位为旋转角度+800,角度为正时右转
  64. target_angle=round(target_angle*1.3)
  65. target_distance=round(target_distance*0.34)
  66. print(target_angle,target_distance)
  67. flag=1
  68. target=0
  69. elif target>0: #前3位组成的数小于600时控制线速度和角速度
  70. '''
  71. linear_velocity=target%1000-400 #前三位为角速度*100+400,角速度为正时右转,后三位为线速度*100+400
  72. angular_velocity=target//1000-400 #换算时数字放大1000倍,除法运算后结果不用缩小
  73. target_speed=linear_velocity/2.95 #计算每周期目标脉冲数
  74. target_offset=angular_velocity/26.8 #计算每周期目标脉冲数差并换算为每边车轮速度与平均速度的差
  75. target1=round(target_speed+target_offset) #左轮目标每周期脉冲数
  76. target2=round(target_speed-target_offset) #右轮目标每周期脉冲数
  77. target=0
  78. except:
  79. pass
  80. _thread.start_new_thread(set_target, (duty1, duty2))
  81. while True:
  82. speed1 = encoder1.read() #编码器读数
  83. speed2 = encoder2.read()
  84. Offset1=offset1 #记录上一次偏差
  85. Offset2=offset2
  86. offset1=target1-speed1
  87. offset2=target2-speed2
  88. adujstment1=offset1*20-Offset1*18 #PID控制:P=20,I=2
  89. adujstment2=offset2*20-Offset2*18
  90. duty1+=adujstment1
  91. duty2+=adujstment2
  92. ''' #控制底盘旋转一定角度并前进一定距离,实测不能正常运行,已弃用
  93. if flag==1: #指定角度转向
  94. print(flag,angle)
  95. if 0<target_angle-angle<10 or 0>target_angle-angle>-10:
  96. target1=0
  97. target2=0
  98. flag=2
  99. angle=0
  100. target_angle=0
  101. if target_angle>0: #右转
  102. target1=20
  103. target2=0
  104. angle+=speed1
  105. if target_angle<0: #左转
  106. target1=0
  107. target2=20
  108. angle+=speed2
  109. if flag==2: #指定距离直行
  110. print(flag,distance)
  111. if target_distance-distance>400:
  112. target1=100
  113. target2=100
  114. elif target_distance-distance>100:
  115. target1=25
  116. target2=25
  117. elif target_distance-distance>40:
  118. target1=10
  119. target2=10
  120. elif target_distance-distance<15:
  121. target1=0
  122. target2=0
  123. flag=0
  124. distance=0
  125. target_distance=0
  126. distance+=speed1
  127. '''
  128. if target1<0: #由于实测发现电机反向时不能正常测速,这部分改为开环控制
  129. duty1=10*target1
  130. if target2<0:
  131. duty2=10*target2
  132. if duty1<-1023:
  133. duty1=-1023
  134. if duty1>1023:
  135. duty1=1023
  136. if duty2<-1023:
  137. duty2=-1023
  138. if duty2>1023:
  139. duty2=1023
  140. if duty1>0:
  141. motor1.duty(duty1)
  142. motor2.duty(0)
  143. if duty1<0:
  144. motor1.duty(0)
  145. motor2.duty(-duty1)
  146. if duty2>0:
  147. motor3.duty(duty2)
  148. motor4.duty(0)
  149. if duty2<0:
  150. motor3.duty(0)
  151. motor4.duty(-duty2)
  152. time.sleep(0.1)

发出热点的ESP32C3

  1. #导入Pin模块
  2. from machine import Pin
  3. import time
  4. from machine import PWM
  5. import network
  6. import socket
  7. #定义LED控制对象
  8. led1=Pin(6,Pin.OUT,Pin.PULL_DOWN)
  9. duty=0
  10. speed=0
  11. turn=0
  12. angular=400
  13. linear=400
  14. #WIFI连接
  15. def wifi_connect():
  16. ap = network.WLAN(network.AP_IF) # 指定用ap模式
  17. ap.active(True) # 启用wifi前需要先激活接口
  18. ap.config(essid="ESP32_Motor_Control") # 设置热点名称
  19. ap.config(authmode=0) # 设置认证模式
  20. return True
  21. #网页数据
  22. def web_page():
  23. global a1
  24. global a2
  25. global a3
  26. global a4
  27. html = """<html>
  28. <head>
  29. <meta name="viewport" content="width=device-width, initial-scale=1">
  30. <style>
  31. .button{display: inline-block; background-color: #8080f0; border: none;
  32. border-radius: 4px; color: white; padding: 8px 15px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
  33. a:link {text-decoration:none;}a:visited {text-decoration:none;}a:hover {text-decoration:none;}
  34. a:active {text-decoration:none;}
  35. html {
  36. font-family: Arial;
  37. display: inline-block;
  38. margin: 0px auto;
  39. text-align: center;
  40. }
  41. h2 { font-size: 1.5rem; }
  42. p { font-size: 1.5rem; }
  43. .units { font-size: 1rem; }
  44. .dht-labels{
  45. font-size: 1rem;
  46. vertical-align:middle;
  47. padding-bottom: 7px;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <h2>ESP32 Motor Control</h2> <p><a href="/?d1">linear: <strong>""" + str(speed) + """</strong></a></p>
  53. <p><a href="/?w10"><button class="button">-3.00</button></a>
  54. <a href="/?w11"><button class="button">-2.75</button></a>
  55. <a href="/?w12"><button class="button">-2.50</button></a>
  56. <a href="/?w13"><button class="button">-2.25</button></a>
  57. <a href="/?w14"><button class="button">-2.00</button></a>
  58. <a href="/?w15"><button class="button">-1.75</button></a>
  59. <a href="/?w16"><button class="button">-1.50</button></a>
  60. <a href="/?w17"><button class="button">-1.25</button></a>
  61. <a href="/?w18"><button class="button">-1.00</button></a>
  62. <a href="/?w19"><button class="button">-0.75</button></a>
  63. <a href="/?w1a"><button class="button">-0.50</button></a>
  64. <a href="/?w1b"><button class="button">-0.25</button></a>
  65. <a href="/?w1c"><button class="button">0.00</button></a>
  66. <a href="/?w1d"><button class="button">0.25</button></a>
  67. <a href="/?w1e"><button class="button">0.50</button></a>
  68. <a href="/?w1f"><button class="button">0.75</button></a>
  69. <a href="/?w1g"><button class="button">1.00</button></a>
  70. <a href="/?w1h"><button class="button">1.25</button></a>
  71. <a href="/?w1i"><button class="button">1.50</button></a>
  72. <a href="/?w1j"><button class="button">1.75</button></a>
  73. <a href="/?w1k"><button class="button">2.00</button></a>
  74. <a href="/?w1l"><button class="button">2.25</button></a>
  75. <a href="/?w1m"><button class="button">2.50</button></a>
  76. <a href="/?w1n"><button class="button">2.75</button></a>
  77. <a href="/?w1o"><button class="button">3.00</button></a></p>
  78. <p><a href="/?d2">angular: <strong>""" + str(turn) + """</strong></a></p>
  79. <p><a href="/?w20"><button class="button">-3.00</button></a>
  80. <a href="/?w21"><button class="button">-2.75</button></a>
  81. <a href="/?w22"><button class="button">-2.50</button></a>
  82. <a href="/?w23"><button class="button">-2.25</button></a>
  83. <a href="/?w24"><button class="button">-2.00</button></a>
  84. <a href="/?w25"><button class="button">-1.75</button></a>
  85. <a href="/?w26"><button class="button">-1.50</button></a>
  86. <a href="/?w27"><button class="button">-1.25</button></a>
  87. <a href="/?w28"><button class="button">-1.00</button></a>
  88. <a href="/?w29"><button class="button">-0.75</button></a>
  89. <a href="/?w2a"><button class="button">-0.50</button></a>
  90. <a href="/?w2b"><button class="button">-0.25</button></a>
  91. <a href="/?w2c"><button class="button">0.00</button></a>
  92. <a href="/?w2d"><button class="button">0.25</button></a>
  93. <a href="/?w2e"><button class="button">0.50</button></a>
  94. <a href="/?w2f"><button class="button">0.75</button></a>
  95. <a href="/?w2g"><button class="button">1.00</button></a>
  96. <a href="/?w2h"><button class="button">1.25</button></a>
  97. <a href="/?w2i"><button class="button">1.50</button></a>
  98. <a href="/?w2j"><button class="button">1.75</button></a>
  99. <a href="/?w2k"><button class="button">2.00</button></a>
  100. <a href="/?w2l"><button class="button">2.25</button></a>
  101. <a href="/?w2m"><button class="button">2.50</button></a>
  102. <a href="/?w2n"><button class="button">2.75</button></a>
  103. <a href="/?w2o"><button class="button">3.00</button></a></p>
  104. </body>
  105. </html>"""
  106. return html
  107. #程序入口
  108. if __name__=="__main__":
  109. wifi_connect()
  110. #SOCK_STREAM表示的是TCP协议,SOCK_DGRAM表示的是UDP协议
  111. my_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #创建socket连接
  112. # 将socket对象绑定ip地址和端口号
  113. my_socket.bind(('', 80))
  114. # 相当于电话的开机 括号里的参数表示可以同时接收5个请求
  115. my_socket.listen(5)
  116. while True:
  117. try:
  118. # 进入监听状态,等待别人链接过来,有两个返回值,
  119. #一个是对方的socket对象,一个是对方的ip以及端口
  120. client, addr = my_socket.accept()
  121. # recv表示接收,括号里是最大接收字节
  122. request = client.recv(1024)
  123. request = str(request)
  124. w10 = request.find('/?w10')
  125. w11 = request.find('/?w11')
  126. w12 = request.find('/?w12')
  127. w13 = request.find('/?w13')
  128. w14 = request.find('/?w14')
  129. w15 = request.find('/?w15')
  130. w16 = request.find('/?w16')
  131. w17 = request.find('/?w17')
  132. w18 = request.find('/?w18')
  133. w19 = request.find('/?w19')
  134. w1a = request.find('/?w1a')
  135. w1b = request.find('/?w1b')
  136. w1c = request.find('/?w1c')
  137. w1d = request.find('/?w1d')
  138. w1e = request.find('/?w1e')
  139. w1f = request.find('/?w1f')
  140. w1g = request.find('/?w1g')
  141. w1h = request.find('/?w1h')
  142. w1i = request.find('/?w1i')
  143. w1j = request.find('/?w1j')
  144. w1k = request.find('/?w1k')
  145. w1l = request.find('/?w1l')
  146. w1m = request.find('/?w1m')
  147. w1n = request.find('/?w1n')
  148. w1o = request.find('/?w1o')
  149. w20 = request.find('/?w20')
  150. w21 = request.find('/?w21')
  151. w22 = request.find('/?w22')
  152. w23 = request.find('/?w23')
  153. w24 = request.find('/?w24')
  154. w25 = request.find('/?w25')
  155. w26 = request.find('/?w26')
  156. w27 = request.find('/?w27')
  157. w28 = request.find('/?w28')
  158. w29 = request.find('/?w29')
  159. w2a = request.find('/?w2a')
  160. w2b = request.find('/?w2b')
  161. w2c = request.find('/?w2c')
  162. w2d = request.find('/?w2d')
  163. w2e = request.find('/?w2e')
  164. w2f = request.find('/?w2f')
  165. w2g = request.find('/?w2g')
  166. w2h = request.find('/?w2h')
  167. w2i = request.find('/?w2i')
  168. w2j = request.find('/?w2j')
  169. w2k = request.find('/?w2k')
  170. w2l = request.find('/?w2l')
  171. w2m = request.find('/?w2m')
  172. w2n = request.find('/?w2n')
  173. w2o = request.find('/?w2o')
  174. d1 = request.find('/?d1')
  175. d2 = request.find('/?d2')
  176. if d1 == 6:
  177. speed=0.00
  178. linear=400
  179. print(str(angular)+str(linear))
  180. if d2 == 6:
  181. turn=0.00
  182. angular=400
  183. print(str(angular)+str(linear))
  184. if w10 == 6:
  185. speed=-3.00
  186. linear=100
  187. print(str(angular)+str(linear))
  188. if w11 == 6:
  189. speed=-2.75
  190. linear=125
  191. print(str(angular)+str(linear))
  192. if w12 == 6:
  193. speed=-2.50
  194. linear=150
  195. print(str(angular)+str(linear))
  196. if w13 == 6:
  197. speed=-2.25
  198. linear=175
  199. print(str(angular)+str(linear))
  200. if w14 == 6:
  201. speed=-2.00
  202. linear=200
  203. print(str(angular)+str(linear))
  204. if w15 == 6:
  205. speed=-1.75
  206. linear=225
  207. print(str(angular)+str(linear))
  208. if w16 == 6:
  209. speed=-1.50
  210. linear=250
  211. print(str(angular)+str(linear))
  212. if w17 == 6:
  213. speed=-1.25
  214. linear=275
  215. print(str(angular)+str(linear))
  216. if w18 == 6:
  217. speed=-1.00
  218. linear=300
  219. print(str(angular)+str(linear))
  220. if w19 == 6:
  221. speed=-0.75
  222. linear=325
  223. print(str(angular)+str(linear))
  224. if w1a == 6:
  225. speed=-0.50
  226. linear=350
  227. print(str(angular)+str(linear))
  228. if w1b == 6:
  229. speed=-0.25
  230. linear=375
  231. print(str(angular)+str(linear))
  232. if w1c == 6:
  233. speed=0.00
  234. linear=400
  235. print(str(angular)+str(linear))
  236. if w1d == 6:
  237. speed=0.25
  238. linear=425
  239. print(str(angular)+str(linear))
  240. if w1e == 6:
  241. speed=0.50
  242. linear=450
  243. print(str(angular)+str(linear))
  244. if w1f == 6:
  245. speed=0.75
  246. linear=475
  247. print(str(angular)+str(linear))
  248. if w1g == 6:
  249. speed=1.00
  250. linear=500
  251. print(str(angular)+str(linear))
  252. if w1h == 6:
  253. speed=1.25
  254. linear=525
  255. print(str(angular)+str(linear))
  256. if w1i == 6:
  257. speed=1.50
  258. linear=550
  259. print(str(angular)+str(linear))
  260. if w1j == 6:
  261. speed=1.75
  262. linear=575
  263. print(str(angular)+str(linear))
  264. if w1k == 6:
  265. speed=2.00
  266. linear=600
  267. print(str(angular)+str(linear))
  268. if w1l == 6:
  269. speed=2.25
  270. linear=625
  271. print(str(angular)+str(linear))
  272. if w1m == 6:
  273. speed=2.50
  274. linear=650
  275. print(str(angular)+str(linear))
  276. if w1n == 6:
  277. speed=2.75
  278. linear=675
  279. print(str(angular)+str(linear))
  280. if w1o == 6:
  281. speed=3.00
  282. linear=700
  283. print(str(angular)+str(linear))
  284. if w20 == 6:
  285. turn=-3.00
  286. angular=100
  287. print(str(angular)+str(linear))
  288. if w21 == 6:
  289. turn=-2.75
  290. angular=125
  291. print(str(angular)+str(linear))
  292. if w22 == 6:
  293. turn=-2.50
  294. angular=150
  295. print(str(angular)+str(linear))
  296. if w23 == 6:
  297. turn=-2.25
  298. angular=175
  299. print(str(angular)+str(linear))
  300. if w24 == 6:
  301. turn=-2.00
  302. angular=200
  303. print(str(angular)+str(linear))
  304. if w25 == 6:
  305. turn=-1.75
  306. angular=225
  307. print(str(angular)+str(linear))
  308. if w26 == 6:
  309. turn=-1.50
  310. angular=250
  311. print(str(angular)+str(linear))
  312. if w27 == 6:
  313. turn=-1.25
  314. angular=275
  315. print(str(angular)+str(linear))
  316. if w28 == 6:
  317. turn=-1.00
  318. angular=300
  319. print(str(angular)+str(linear))
  320. if w29 == 6:
  321. turn=-0.75
  322. angular=325
  323. print(str(angular)+str(linear))
  324. if w2a == 6:
  325. turn=-0.50
  326. angular=350
  327. print(str(angular)+str(linear))
  328. if w2b == 6:
  329. turn=-0.25
  330. angular=375
  331. print(str(angular)+str(linear))
  332. if w2c == 6:
  333. turn=0.00
  334. angular=400
  335. print(str(angular)+str(linear))
  336. if w2d == 6:
  337. turn=0.25
  338. angular=425
  339. print(str(angular)+str(linear))
  340. if w2e == 6:
  341. turn=0.50
  342. angular=450
  343. print(str(angular)+str(linear))
  344. if w2f == 6:
  345. turn=0.75
  346. angular=475
  347. print(str(angular)+str(linear))
  348. if w2g == 6:
  349. turn=1.00
  350. angular=500
  351. print(str(angular)+str(linear))
  352. if w2h == 6:
  353. turn=1.25
  354. angular=525
  355. print(str(angular)+str(linear))
  356. if w2i == 6:
  357. turn=1.50
  358. angular=550
  359. print(str(angular)+str(linear))
  360. if w2j == 6:
  361. turn=1.75
  362. angular=575
  363. print(str(angular)+str(linear))
  364. if w2k == 6:
  365. turn=2.00
  366. angular=600
  367. print(str(angular)+str(linear))
  368. if w2l == 6:
  369. turn=2.25
  370. angular=625
  371. print(str(angular)+str(linear))
  372. if w2m == 6:
  373. turn=2.50
  374. angular=650
  375. print(str(angular)+str(linear))
  376. if w2n == 6:
  377. turn=2.75
  378. angular=675
  379. print(str(angular)+str(linear))
  380. if w2o == 6:
  381. turn=3.00
  382. angular=700
  383. print(str(angular)+str(linear))
  384. response = web_page()
  385. client.send('HTTP/1.1 200 OK\n')
  386. client.send('Content-Type: text/html\n')
  387. client.send('Connection: close\n\n')
  388. client.sendall(response)
  389. client.close()
  390. except:
  391. pass

控制机械臂的ESP32C3

  1. #导入Pin模块
  2. from machine import Pin
  3. import time
  4. from machine import SoftI2C
  5. from servo import Servos
  6. import network
  7. import socket
  8. #定义LED控制对象
  9. led1=Pin(4,Pin.OUT,Pin.PULL_DOWN)
  10. i2c=SoftI2C(sda=Pin(9),scl=Pin(8),freq=10000)
  11. servos=Servos(i2c,address=0x40)
  12. #连接的WIFI账号和密码
  13. ssid = "ESP32_Motor_Control"
  14. password=None
  15. #舵机默认角度
  16. servos.position(0,90)
  17. servos.position(1,90)
  18. servos.position(2,90)
  19. servos.position(3,90)
  20. servos.position(4,90)
  21. #WIFI连接
  22. def wifi_connect():
  23. wlan=network.WLAN(network.STA_IF) #STA模式
  24. wlan.active(True) #激活
  25. if not wlan.isconnected():
  26. print("conneting to network...")
  27. wlan.connect(ssid,password) #输入 WIFI 账号密码
  28. while not wlan.isconnected():
  29. led1.value(1)
  30. time.sleep_ms(300)
  31. led1.value(0)
  32. time.sleep_ms(300)
  33. led1.value(0)
  34. return False
  35. else:
  36. led1.value(0)
  37. print("network information:", wlan.ifconfig())
  38. return True
  39. a0=90
  40. a1=90
  41. a2=90
  42. a3=90
  43. a4=90
  44. #网页数据
  45. def web_page():
  46. global a0
  47. global a1
  48. global a2
  49. global a3
  50. global a4
  51. html = """<html>
  52. <head>
  53. <meta name="viewport" content="width=device-width, initial-scale=1">
  54. <style>
  55. .button{display: inline-block; background-color: #971080; border: none;
  56. border-radius: 4px; color: white; padding: 8px 15px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
  57. a:link {text-decoration:none;}a:visited {text-decoration:none;}a:hover {text-decoration:none;}
  58. a:active {text-decoration:none;}
  59. html {
  60. font-family: Arial;
  61. display: inline-block;
  62. margin: 0px auto;
  63. text-align: center;
  64. }
  65. h2 { font-size: 1.5rem; }
  66. p { font-size: 1.5rem; }
  67. .units { font-size: 1rem; }
  68. .dht-labels{
  69. font-size: 1rem;
  70. vertical-align:middle;
  71. padding-bottom: 7px;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <h2>ESP32 Servo Control</h2>
  77. <p><a href="/?d0">Servo0: <strong>""" + str(a0) + """</strong></a></p>
  78. <p><a href="/?b00"><button class="button">0</button></a>
  79. <a href="/?b01"><button class="button">10</button></a>
  80. <a href="/?b02"><button class="button">20</button></a>
  81. <a href="/?b03"><button class="button">30</button></a>
  82. <a href="/?b04"><button class="button">40</button></a>
  83. <a href="/?b05"><button class="button">50</button></a>
  84. <a href="/?b06"><button class="button">60</button></a>
  85. <a href="/?b07"><button class="button">70</button></a>
  86. <a href="/?b08"><button class="button">80</button></a>
  87. <a href="/?b09"><button class="button">90</button></a>
  88. <a href="/?b0a"><button class="button">100</button></a>
  89. <a href="/?b0b"><button class="button">110</button></a>
  90. <a href="/?b0c"><button class="button">120</button></a>
  91. <a href="/?b0d"><button class="button">130</button></a>
  92. <a href="/?b0e"><button class="button">140</button></a>
  93. <a href="/?b0f"><button class="button">150</button></a>
  94. <a href="/?b0g"><button class="button">160</button></a>
  95. <a href="/?b0h"><button class="button">170</button></a>
  96. <a href="/?b0i"><button class="button">180</button></a></p>
  97. <p><a href="/?d1">Servo1: <strong>""" + str(a1) + """</strong></a></p>
  98. <p><a href="/?b10"><button class="button">0</button></a>
  99. <a href="/?b11"><button class="button">10</button></a>
  100. <a href="/?b12"><button class="button">20</button></a>
  101. <a href="/?b13"><button class="button">30</button></a>
  102. <a href="/?b14"><button class="button">40</button></a>
  103. <a href="/?b15"><button class="button">50</button></a>
  104. <a href="/?b16"><button class="button">60</button></a>
  105. <a href="/?b17"><button class="button">70</button></a>
  106. <a href="/?b18"><button class="button">80</button></a>
  107. <a href="/?b19"><button class="button">90</button></a>
  108. <a href="/?b1a"><button class="button">100</button></a>
  109. <a href="/?b1b"><button class="button">110</button></a>
  110. <a href="/?b1c"><button class="button">120</button></a>
  111. <a href="/?b1d"><button class="button">130</button></a>
  112. <a href="/?b1e"><button class="button">140</button></a>
  113. <a href="/?b1f"><button class="button">150</button></a>
  114. <a href="/?b1g"><button class="button">160</button></a>
  115. <a href="/?b1h"><button class="button">170</button></a>
  116. <a href="/?b1i"><button class="button">180</button></a></p>
  117. <p><a href="/?d2">Servo2: <strong>""" + str(a2) + """</strong></a></p>
  118. <p><a href="/?b10"><button class="button">0</button></a>
  119. <a href="/?b21"><button class="button">10</button></a>
  120. <a href="/?b22"><button class="button">20</button></a>
  121. <a href="/?b23"><button class="button">30</button></a>
  122. <a href="/?b24"><button class="button">40</button></a>
  123. <a href="/?b25"><button class="button">50</button></a>
  124. <a href="/?b26"><button class="button">60</button></a>
  125. <a href="/?b27"><button class="button">70</button></a>
  126. <a href="/?b28"><button class="button">80</button></a>
  127. <a href="/?b29"><button class="button">90</button></a>
  128. <a href="/?b2a"><button class="button">100</button></a>
  129. <a href="/?b2b"><button class="button">110</button></a>
  130. <a href="/?b2c"><button class="button">120</button></a>
  131. <a href="/?b2d"><button class="button">130</button></a>
  132. <a href="/?b2e"><button class="button">140</button></a>
  133. <a href="/?b2f"><button class="button">150</button></a>
  134. <a href="/?b2g"><button class="button">160</button></a>
  135. <a href="/?b2h"><button class="button">170</button></a>
  136. <a href="/?b2i"><button class="button">180</button></a></p>
  137. <p><a href="/?d3">Servo3: <strong>""" + str(a3) + """</strong></a></p>
  138. <p><a href="/?b30"><button class="button">0</button></a>
  139. <a href="/?b31"><button class="button">10</button></a>
  140. <a href="/?b32"><button class="button">20</button></a>
  141. <a href="/?b33"><button class="button">30</button></a>
  142. <a href="/?b34"><button class="button">40</button></a>
  143. <a href="/?b35"><button class="button">50</button></a>
  144. <a href="/?b36"><button class="button">60</button></a>
  145. <a href="/?b37"><button class="button">70</button></a>
  146. <a href="/?b38"><button class="button">80</button></a>
  147. <a href="/?b39"><button class="button">90</button></a>
  148. <a href="/?b3a"><button class="button">100</button></a>
  149. <a href="/?b3b"><button class="button">110</button></a>
  150. <a href="/?b3c"><button class="button">120</button></a>
  151. <a href="/?b3d"><button class="button">130</button></a>
  152. <a href="/?b3e"><button class="button">140</button></a>
  153. <a href="/?b3f"><button class="button">150</button></a>
  154. <a href="/?b3g"><button class="button">160</button></a>
  155. <a href="/?b3h"><button class="button">170</button></a>
  156. <a href="/?b3i"><button class="button">180</button></a></p>
  157. <p><a href="/?d4">Servo4: <strong>""" + str(a4) + """</strong></a></p>
  158. <p><a href="/?b30"><button class="button">0</button></a>
  159. <a href="/?b41"><button class="button">10</button></a>
  160. <a href="/?b42"><button class="button">20</button></a>
  161. <a href="/?b43"><button class="button">30</button></a>
  162. <a href="/?b44"><button class="button">40</button></a>
  163. <a href="/?b45"><button class="button">50</button></a>
  164. <a href="/?b46"><button class="button">60</button></a>
  165. <a href="/?b47"><button class="button">70</button></a>
  166. <a href="/?b48"><button class="button">80</button></a>
  167. <a href="/?b49"><button class="button">90</button></a>
  168. <a href="/?b4a"><button class="button">100</button></a>
  169. <a href="/?b4b"><button class="button">110</button></a>
  170. <a href="/?b4c"><button class="button">120</button></a>
  171. <a href="/?b4d"><button class="button">130</button></a>
  172. <a href="/?b4e"><button class="button">140</button></a>
  173. <a href="/?b4f"><button class="button">150</button></a>
  174. <a href="/?b4g"><button class="button">160</button></a>
  175. <a href="/?b4h"><button class="button">170</button></a>
  176. <a href="/?b4i"><button class="button">180</button></a></p>
  177. </body>
  178. </html>"""
  179. return html
  180. #程序入口
  181. if __name__=="__main__":
  182. wifi_connect()
  183. #SOCK_STREAM表示的是TCP协议,SOCK_DGRAM表示的是UDP协议
  184. my_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #创建socket连接
  185. # 将socket对象绑定ip地址和端口号
  186. my_socket.bind(('', 80))
  187. # 相当于电话的开机 括号里的参数表示可以同时接收5个请求
  188. my_socket.listen(5)
  189. while True:
  190. try:
  191. # 进入监听状态,等待别人链接过来,有两个返回值,
  192. #一个是对方的socket对象,一个是对方的ip以及端口
  193. client, addr = my_socket.accept()
  194. print('Got a connection from %s' % str(addr))
  195. # recv表示接收,括号里是最大接收字节
  196. request = client.recv(1024)
  197. request = str(request)
  198. print('Content = %s' % request)
  199. b00 = request.find('/?b00')
  200. b01 = request.find('/?b01')
  201. b02 = request.find('/?b02')
  202. b03 = request.find('/?b03')
  203. b04 = request.find('/?b04')
  204. b05 = request.find('/?b05')
  205. b06 = request.find('/?b06')
  206. b07 = request.find('/?b07')
  207. b08 = request.find('/?b08')
  208. b09 = request.find('/?b09')
  209. b0a = request.find('/?b0a')
  210. b0b = request.find('/?b0b')
  211. b0c = request.find('/?b0c')
  212. b0d = request.find('/?b0d')
  213. b0e = request.find('/?b0e')
  214. b0f = request.find('/?b0f')
  215. b0g = request.find('/?b0g')
  216. b0h = request.find('/?b0h')
  217. b0i = request.find('/?b0i')
  218. b10 = request.find('/?b10')
  219. b11 = request.find('/?b11')
  220. b12 = request.find('/?b12')
  221. b13 = request.find('/?b13')
  222. b14 = request.find('/?b14')
  223. b15 = request.find('/?b15')
  224. b16 = request.find('/?b16')
  225. b17 = request.find('/?b17')
  226. b18 = request.find('/?b18')
  227. b19 = request.find('/?b19')
  228. b1a = request.find('/?b1a')
  229. b1b = request.find('/?b1b')
  230. b1c = request.find('/?b1c')
  231. b1d = request.find('/?b1d')
  232. b1e = request.find('/?b1e')
  233. b1f = request.find('/?b1f')
  234. b1g = request.find('/?b1g')
  235. b1h = request.find('/?b1h')
  236. b1i = request.find('/?b1i')
  237. b20 = request.find('/?b10')
  238. b21 = request.find('/?b21')
  239. b22 = request.find('/?b22')
  240. b23 = request.find('/?b23')
  241. b24 = request.find('/?b24')
  242. b25 = request.find('/?b25')
  243. b26 = request.find('/?b26')
  244. b27 = request.find('/?b27')
  245. b28 = request.find('/?b28')
  246. b29 = request.find('/?b29')
  247. b2a = request.find('/?b2a')
  248. b2b = request.find('/?b2b')
  249. b2c = request.find('/?b2c')
  250. b2d = request.find('/?b2d')
  251. b2e = request.find('/?b2e')
  252. b2f = request.find('/?b2f')
  253. b2g = request.find('/?b2g')
  254. b2h = request.find('/?b2h')
  255. b2i = request.find('/?b2i')
  256. b30 = request.find('/?b30')
  257. b31 = request.find('/?b31')
  258. b32 = request.find('/?b32')
  259. b33 = request.find('/?b33')
  260. b34 = request.find('/?b34')
  261. b35 = request.find('/?b35')
  262. b36 = request.find('/?b36')
  263. b37 = request.find('/?b37')
  264. b38 = request.find('/?b38')
  265. b39 = request.find('/?b39')
  266. b3a = request.find('/?b3a')
  267. b3b = request.find('/?b3b')
  268. b3c = request.find('/?b3c')
  269. b3d = request.find('/?b3d')
  270. b3e = request.find('/?b3e')
  271. b3f = request.find('/?b3f')
  272. b3g = request.find('/?b3g')
  273. b3h = request.find('/?b3h')
  274. b3i = request.find('/?b3i')
  275. b40 = request.find('/?b40')
  276. b41 = request.find('/?b41')
  277. b42 = request.find('/?b42')
  278. b43 = request.find('/?b43')
  279. b44 = request.find('/?b44')
  280. b45 = request.find('/?b45')
  281. b46 = request.find('/?b46')
  282. b47 = request.find('/?b47')
  283. b48 = request.find('/?b48')
  284. b49 = request.find('/?b49')
  285. b4a = request.find('/?b4a')
  286. b4b = request.find('/?b4b')
  287. b4c = request.find('/?b4c')
  288. b4d = request.find('/?b4d')
  289. b4e = request.find('/?b4e')
  290. b4f = request.find('/?b4f')
  291. b4g = request.find('/?b4g')
  292. b4h = request.find('/?b4h')
  293. b4i = request.find('/?b4i')
  294. if b00 == 6:
  295. servos.position(0,0)
  296. a0=0
  297. if b11 == 6:
  298. servos.position(0,10)
  299. a0=10
  300. if b02 == 6:
  301. servos.position(0,20)
  302. a0=20
  303. if b03 == 6:
  304. servos.position(0,30)
  305. a0=30
  306. if b04 == 6:
  307. servos.position(0,40)
  308. a0=40
  309. if b05 == 6:
  310. servos.position(0,50)
  311. a0=50
  312. if b06 == 6:
  313. servos.position(0,60)
  314. a0=60
  315. if b07 == 6:
  316. servos.position(0,70)
  317. a0=70
  318. if b08 == 6:
  319. servos.position(0,80)
  320. a0=80
  321. if b09 == 6:
  322. servos.position(0,90)
  323. a0=90
  324. if b0a == 6:
  325. servos.position(0,100)
  326. a0=100
  327. if b0b == 6:
  328. servos.position(0,110)
  329. a0=110
  330. if b0c == 6:
  331. servos.position(0,120)
  332. a0=120
  333. if b0d == 6:
  334. servos.position(0,130)
  335. a0=130
  336. if b0e == 6:
  337. servos.position(0,140)
  338. a0=140
  339. if b0f == 6:
  340. servos.position(0,150)
  341. a0=150
  342. if b0g == 6:
  343. servos.position(0,160)
  344. a0=160
  345. if b0h == 6:
  346. servos.position(0,170)
  347. a0=170
  348. if b0i == 6:
  349. servos.position(0,180)
  350. a0=180
  351. if b10 == 6:
  352. servos.position(1,0)
  353. a1=0
  354. if b11 == 6:
  355. servos.position(1,10)
  356. a1=10
  357. if b12 == 6:
  358. servos.position(1,20)
  359. a1=20
  360. if b13 == 6:
  361. servos.position(1,30)
  362. a1=30
  363. if b14 == 6:
  364. servos.position(1,40)
  365. a1=40
  366. if b15 == 6:
  367. servos.position(1,50)
  368. a1=50
  369. if b16 == 6:
  370. servos.position(1,60)
  371. a1=60
  372. if b17 == 6:
  373. servos.position(1,70)
  374. a1=70
  375. if b18 == 6:
  376. servos.position(1,80)
  377. a1=80
  378. if b19 == 6:
  379. servos.position(1,90)
  380. a1=90
  381. if b1a == 6:
  382. servos.position(1,100)
  383. a1=100
  384. if b1b == 6:
  385. servos.position(1,110)
  386. a1=110
  387. if b1c == 6:
  388. servos.position(1,120)
  389. a1=120
  390. if b1d == 6:
  391. servos.position(1,130)
  392. a1=130
  393. if b1e == 6:
  394. servos.position(1,140)
  395. a1=140
  396. if b1f == 6:
  397. servos.position(1,150)
  398. a1=150
  399. if b1g == 6:
  400. servos.position(1,160)
  401. a1=160
  402. if b1h == 6:
  403. servos.position(1,170)
  404. a1=170
  405. if b1i == 6:
  406. servos.position(1,180)
  407. a1=180
  408. if b20 == 6:
  409. servos.position(2,0)
  410. a2=0
  411. if b21 == 6:
  412. servos.position(2,10)
  413. a2=10
  414. if b22 == 6:
  415. servos.position(2,20)
  416. a2=20
  417. if b23 == 6:
  418. servos.position(2,30)
  419. a2=30
  420. if b24 == 6:
  421. servos.position(2,40)
  422. a2=40
  423. if b25 == 6:
  424. servos.position(2,50)
  425. a2=50
  426. if b26 == 6:
  427. servos.position(2,60)
  428. a2=60
  429. if b27 == 6:
  430. servos.position(2,70)
  431. a2=70
  432. if b28 == 6:
  433. servos.position(2,80)
  434. a2=80
  435. if b29 == 6:
  436. servos.position(2,90)
  437. a2=90
  438. if b2a == 6:
  439. servos.position(2,100)
  440. a2=100
  441. if b2b == 6:
  442. servos.position(2,110)
  443. a2=110
  444. if b2c == 6:
  445. servos.position(2,120)
  446. a2=120
  447. if b2d == 6:
  448. servos.position(2,130)
  449. a2=130
  450. if b2e == 6:
  451. servos.position(2,140)
  452. a2=140
  453. if b2f == 6:
  454. servos.position(2,150)
  455. a2=150
  456. if b2g == 6:
  457. servos.position(2,160)
  458. a2=160
  459. if b2h == 6:
  460. servos.position(2,170)
  461. a2=170
  462. if b2i == 6:
  463. servos.position(2,180)
  464. a2=180
  465. if b30 == 6:
  466. servos.position(3,0)
  467. a3=0
  468. if b31 == 6:
  469. servos.position(3,10)
  470. a3=10
  471. if b32 == 6:
  472. servos.position(3,20)
  473. a3=20
  474. if b33 == 6:
  475. servos.position(3,30)
  476. a3=30
  477. if b34 == 6:
  478. servos.position(3,40)
  479. a3=40
  480. if b35 == 6:
  481. servos.position(3,50)
  482. a3=50
  483. if b36 == 6:
  484. servos.position(3,60)
  485. a3=60
  486. if b37 == 6:
  487. servos.position(3,70)
  488. a3=70
  489. if b38 == 6:
  490. servos.position(3,80)
  491. a3=80
  492. if b39 == 6:
  493. servos.position(3,90)
  494. a3=90
  495. if b3a == 6:
  496. servos.position(3,100)
  497. a3=100
  498. if b3b == 6:
  499. servos.position(3,110)
  500. a3=110
  501. if b3c == 6:
  502. servos.position(3,120)
  503. a3=120
  504. if b3d == 6:
  505. servos.position(3,130)
  506. a3=130
  507. if b3e == 6:
  508. servos.position(3,140)
  509. a3=140
  510. if b3f == 6:
  511. servos.position(3,150)
  512. a3=150
  513. if b3g == 6:
  514. servos.position(3,160)
  515. a3=160
  516. if b3h == 6:
  517. servos.position(3,170)
  518. a3=170
  519. if b3i == 6:
  520. servos.position(3,180)
  521. a3=180
  522. if b40 == 6:
  523. servos.position(4,0)
  524. a4=0
  525. if b41 == 6:
  526. servos.position(4,10)
  527. a4=10
  528. if b42 == 6:
  529. servos.position(4,20)
  530. a4=20
  531. if b43 == 6:
  532. servos.position(4,30)
  533. a4=30
  534. if b44 == 6:
  535. servos.position(4,40)
  536. a4=40
  537. if b45 == 6:
  538. servos.position(4,50)
  539. a4=50
  540. if b46 == 6:
  541. servos.position(4,60)
  542. a4=60
  543. if b47 == 6:
  544. servos.position(4,70)
  545. a4=70
  546. if b48 == 6:
  547. servos.position(4,80)
  548. a4=80
  549. if b49 == 6:
  550. servos.position(4,90)
  551. a4=90
  552. if b4a == 6:
  553. servos.position(4,100)
  554. a4=100
  555. if b4b == 6:
  556. servos.position(4,110)
  557. a4=110
  558. if b4c == 6:
  559. servos.position(4,120)
  560. a4=120
  561. if b4d == 6:
  562. servos.position(4,130)
  563. a4=130
  564. if b4e == 6:
  565. servos.position(4,140)
  566. a4=140
  567. if b4f == 6:
  568. servos.position(4,150)
  569. a4=150
  570. if b4g == 6:
  571. servos.position(4,160)
  572. a4=160
  573. if b4h == 6:
  574. servos.position(4,170)
  575. a4=170
  576. if b4i == 6:
  577. servos.position(4,180)
  578. a4=180
  579. response = web_page()
  580. client.send('HTTP/1.1 200 OK\n')
  581. client.send('Content-Type: text/html\n')
  582. client.send('Connection: close\n\n')
  583. client.sendall(response)
  584. client.close()
  585. except:
  586. pass

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/588269
推荐阅读
相关标签
  

闽ICP备14008679号