当前位置:   article > 正文

【软硬件通信】ESP32 Arduino 服务端 控制舵机

【软硬件通信】ESP32 Arduino 服务端 控制舵机

1、安装esp32开发环境

搭建ESP32开发环境

 

2、编写舵机驱动程序

  1. #include <WiFi.h>
  2. #include <Arduino.h>
  3. int freq = 50; // 频率(20ms周期)
  4. int channel = 8; // 通道(高速通道(0 ~ 7)由80MHz时钟驱动,低速通道(8 ~ 15)由 1MHz 时钟驱动。)
  5. int resolution = 8; // 分辨率
  6. const int led = 16;
  7. const char *ssid = "wifi-name"; //wifi名
  8. const char *password = "wifi-pwd";//wifi密码
  9. const IPAddress serverIP(192,168,0,101); //欲访问的服务端IP地址
  10. uint16_t serverPort = 9010; //服务端口号
  11. WiFiClient client; //声明一个ESP32客户端对象,用于与服务器进行连接
  12. int calculatePWM(int degree)
  13. { //0-180
  14. //20ms周期,高电平0.5-2.5ms,对应0-180度角度
  15. const float deadZone = 6.4;//对应0.5ms(0.5ms/(20ms/256))
  16. const float max = 32;//对应2.5ms
  17. if (degree < 0)
  18. degree = 0;
  19. if (degree > 180)
  20. degree = 180;
  21. return (int)(((max - deadZone) / 180) * degree + deadZone);
  22. }
  23. void setup()
  24. {
  25. pinMode(2, OUTPUT);
  26. ledcSetup(channel, freq, resolution); // 设置通道
  27. ledcAttachPin(led, channel); // 将通道与对应的引脚连接
  28. Serial.begin(115200);
  29. Serial.println();
  30. WiFi.mode(WIFI_STA);
  31. WiFi.setSleep(false); //关闭STA模式下wifi休眠,提高响应速度
  32. WiFi.begin(ssid, password);
  33. while (WiFi.status() != WL_CONNECTED)
  34. {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38. Serial.println("Connected");
  39. Serial.print("IP Address:");
  40. Serial.println(WiFi.localIP());
  41. Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
  42. }
  43. void loop()
  44. {
  45. Serial.println("尝试访问服务器");
  46. if (client.connect(serverIP, serverPort)) //尝试访问目标地址
  47. {
  48. Serial.println("访问成功");
  49. client.print("Hello world!"); //向服务器发送数据
  50. while (client.connected() || client.available()) //如果已连接或有收到的未读取的数据
  51. {
  52. if (client.available()) //如果有数据可读取
  53. {
  54. String line = client.readStringUntil('\n'); //读取数据到换行符
  55. Serial.print("读取到数据:");
  56. Serial.println(line);
  57. client.write(line.c_str()); //将收到的数据回发
  58. if(line == "1")
  59. {
  60. Serial.print("打开灯");
  61. digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
  62. for (int d = 0; d <= 180; d += 10)
  63. {
  64. ledcWrite(channel, calculatePWM(d)); // 输出PWM
  65. Serial.printf("value=%d,calcu=%d\n", d, calculatePWM(d));
  66. delay(500);
  67. }
  68. }
  69. else
  70. {
  71. Serial.print("关闭灯");
  72. digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
  73. }
  74. }
  75. }
  76. Serial.println("关闭当前连接");
  77. client.stop(); //关闭客户端
  78. }
  79. else
  80. {
  81. Serial.println("访问失败");
  82. client.stop(); //关闭客户端
  83. }
  84. delay(5000);
  85. }

 

3、服务端通讯

  1. while(1)
  2. port = 9010;
  3. % 构造服务器端tcpip对象
  4. tcpipServer = tcpip('192,168,0,101',port,'NetWorkRole','Server');
  5. set(tcpipServer,'Timeout',10);
  6. N = 1024;
  7. set(tcpipServer,'InputBufferSize',8*N);
  8. set(tcpipServer,'OutputBufferSize',1024);
  9. % 打开连接对象
  10. fopen(tcpipServer);
  11. % 发送指令
  12. instruction = 'Please send back a signal.';
  13. fwrite(tcpipServer,instruction,'int8');
  14. disp('Instruction sending succeeds.');
  15. numSent = get(tcpipServer,'valuesSent');
  16. disp(strcat('Bytes of instruction is :',num2str(numSent)));
  17. % 等待接收数据
  18. while(1)
  19. nBytes = get(tcpipServer,'BytesAvailable');
  20. if nBytes > 0
  21. break;
  22. end
  23. end
  24. % 接收数据
  25. receivedInstruction = fread(tcpipServer,nBytes,'int8');
  26. disp(strcat('received instruction is: ',char(receivedInstruction')));
  27. % 关闭和删除连接对象
  28. fclose(tcpipServer);
  29. delete(tcpipServer);
  30. end

 

换其他的任何高级编程语言均可,实现tcp通讯即可。

 

引用:

驱动舵机

matlab tcp server 持续通信

 

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

闽ICP备14008679号