当前位置:   article > 正文

学习笔记:基于ESP32-CAM控制wifi监控避障小车_esp32cam小车

esp32cam小车

文章:

一、器材清单

  1. 主控ESP32-CAM(带摄像头)
  2. USB 转 TTL(CH340)串口烧入器
  3. HC-SR04超声波测距模块
  4. HC-SR04超声波测距支架(结合舵机云台固定)
  5. SG90舵机 (360度)
  6. L298n电机驱动模块
  7. 四轮小车套件(包含小车底板和四轮电机和电源开关按键)
  8. 胶枪 胶棒3根
  9. 18650电池*2
  10. 18650电池充电器

 二、闭门造车

对小车零件进行组装,成品如下:

 线路连接:

 三、头秃码字

1、Arduino的基本语法;

  1. void Myservo(int angle)
  2. {
  3. for (int i = 0; i < 5; i++) {
  4. int pulsewidth = (angle * 11) + 500; //将角度转化为500-2480的脉宽值
  5. digitalWrite(ServoPin, HIGH); //将舵机接口电平至高
  6. delayMicroseconds(pulsewidth); //延时脉宽值的微秒数
  7. digitalWrite(ServoPin, LOW); //将舵机接口电平至低
  8. delayMicroseconds(20000 - pulsewidth); //延时脉宽值的微秒数
  9. }
  10. delay(10);
  11. }

(1)采用IO口TRIG触发测距,给至少10us的高电平信号;

(2)模块自动发送8个40khz的方波,自动检测是否有信号返回;

(3)有信号返回,通过IO口ECHO输出一个高电平,高电平持续的时间   就是超声波从发射到返回的时间。

(4)测试距离=(高电平时间*声速(340M/S))/2;  

(5)pulseIn() :用于读取引脚脉冲的时间长度,脉冲可以是HIGH或LOW。   如果是HIGH,函数将先等引脚变为高电平,然后开始计时,一直到变为低   电平为止。返回脉冲持续的时间长短, 单位为ms。如果超时还没有读到的话,   将返回0。声音在干燥、摄氏 20度的空气中的传播速度大约为343米/秒,合34,300厘米/秒。 或者,我们作一下单位换算,34,300除以1,000,000厘米/微秒。即为:0.0343厘米/微秒 再换一个角度,1/(0.0343 厘米/微秒)即:29.15 微秒/厘米。 这就意味着,每291.5微秒表示10CM的距离。1厘米就是29.15微秒。 但是发送后到接收到回波,声音走过的是2倍的距离呀。所以实际距离就是1厘米,对应58.3微秒。 实际上整个测距过程是测的发出声波到收到回波的时间,你的程序里的dis距离实际上是时间us。 所以换成距离的cm,要除以58。当然除以58.3可能更精确

2、云平台控制闪光灯 

  1. // 新建组件对象
  2. BlinkerButton Button1("btn-abc");
  3. // 按下按键即会执行该函数
  4. void button1_callback(const String & state) {
  5. BLINKER_LOG("get button state: ", state);
  6. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  7. }
  8. void setup() {
  9. // 初始化串口
  10. Serial.begin(115200);
  11. // 初始化有LED的IO
  12. pinMode(LED_BUILTIN, OUTPUT);
  13. digitalWrite(LED_BUILTIN, HIGH);
  14. // 初始化blinker
  15. Blinker.begin(auth, ssid, pswd);
  16. Button1.attach(button1_callback);
  17. }

3、控制舵机转动

  1. // 新建组件对象
  2. BlinkerSlider Slider0("huakuai-kai0"); //位置0-3 滑块 数据键名 舵机范围0-180
  3. //滑块任务函数
  4. void slider0_callback(int32_t value) //滑块0
  5. {
  6. int servo=value;
  7. Myservo(servo);//转动角度}
  8. }
  9. void setup() {
  10. // 初始化串口
  11. Serial.begin(115200);
  12. // 初始化blinker
  13. Blinker.begin(auth, ssid, pswd);
  14. Slider0.attach(slider0_callback);//关联滑动开关
  15. }

4、ESP32-CAM控制电机

  1. BlinkerButton Button1("GoForward");
  2. BlinkerButton Button2("GoBack");
  3. BlinkerButton Button3("Left");
  4. BlinkerButton Button4("Right");
  5. BlinkerButton Button5("Stop");
  6. void button1_callback(const String & state) //定义前进按钮
  7. {
  8. BLINKER_LOG("get button state: ", state);
  9. if (state == "press" || state == "tap") { //判断按键是否被按下
  10. Go_Forward();
  11. }
  12. else if (state == "pressup") { //判断按键是否松开
  13. Stop_Car( );
  14. }
  15. }
  16. Button1.attach(button1_callback);

4、代码总结:

  1. #ifndef ESP_CAM_SERVER_H
  2. #define ESP_CAM_SERVER_H
  3. //#include "esp_camera.h"
  4. #include <WiFi.h>
  5. #include "esp_timer.h"
  6. #include "img_converters.h"
  7. #include "Arduino.h"
  8. #include "fb_gfx.h"
  9. #include "soc/soc.h" //disable brownout problems
  10. #include "soc/rtc_cntl_reg.h" //disable brownout problems
  11. #include "esp_http_server.h"
  12. #define PART_BOUNDARY "123456789000000000000987654321"
  13. // This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
  14. #define CAMERA_MODEL_AI_THINKER
  15. //#define CAMERA_MODEL_M5STACK_PSRAM
  16. //#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM
  17. // Not tested with this model
  18. //#define CAMERA_MODEL_WROVER_KIT
  19. #if defined(CAMERA_MODEL_WROVER_KIT)
  20. #define PWDN_GPIO_NUM -1
  21. #define RESET_GPIO_NUM -1
  22. #define XCLK_GPIO_NUM 21
  23. #define SIOD_GPIO_NUM 26
  24. #define SIOC_GPIO_NUM 27
  25. #define Y9_GPIO_NUM 35
  26. #define Y8_GPIO_NUM 34
  27. #define Y7_GPIO_NUM 39
  28. #define Y6_GPIO_NUM 36
  29. #define Y5_GPIO_NUM 19
  30. #define Y4_GPIO_NUM 18
  31. #define Y3_GPIO_NUM 5
  32. #define Y2_GPIO_NUM 4
  33. #define VSYNC_GPIO_NUM 25
  34. #define HREF_GPIO_NUM 23
  35. #define PCLK_GPIO_NUM 22
  36. #elif defined(CAMERA_MODEL_M5STACK_PSRAM)
  37. #define PWDN_GPIO_NUM -1
  38. #define RESET_GPIO_NUM 15
  39. #define XCLK_GPIO_NUM 27
  40. #define SIOD_GPIO_NUM 25
  41. #define SIOC_GPIO_NUM 23
  42. #define Y9_GPIO_NUM 19
  43. #define Y8_GPIO_NUM 36
  44. #define Y7_GPIO_NUM 18
  45. #define Y6_GPIO_NUM 39
  46. #define Y5_GPIO_NUM 5
  47. #define Y4_GPIO_NUM 34
  48. #define Y3_GPIO_NUM 35
  49. #define Y2_GPIO_NUM 32
  50. #define VSYNC_GPIO_NUM 22
  51. #define HREF_GPIO_NUM 26
  52. #define PCLK_GPIO_NUM 21
  53. #elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
  54. #define PWDN_GPIO_NUM -1
  55. #define RESET_GPIO_NUM 15
  56. #define XCLK_GPIO_NUM 27
  57. #define SIOD_GPIO_NUM 25
  58. #define SIOC_GPIO_NUM 23
  59. #define Y9_GPIO_NUM 19
  60. #define Y8_GPIO_NUM 36
  61. #define Y7_GPIO_NUM 18
  62. #define Y6_GPIO_NUM 39
  63. #define Y5_GPIO_NUM 5
  64. #define Y4_GPIO_NUM 34
  65. #define Y3_GPIO_NUM 35
  66. #define Y2_GPIO_NUM 17
  67. #define VSYNC_GPIO_NUM 22
  68. #define HREF_GPIO_NUM 26
  69. #define PCLK_GPIO_NUM 21
  70. #elif defined(CAMERA_MODEL_AI_THINKER)
  71. #define PWDN_GPIO_NUM 32
  72. #define RESET_GPIO_NUM -1
  73. #define XCLK_GPIO_NUM 0
  74. #define SIOD_GPIO_NUM 26
  75. #define SIOC_GPIO_NUM 27
  76. #define Y9_GPIO_NUM 35
  77. #define Y8_GPIO_NUM 34
  78. #define Y7_GPIO_NUM 39
  79. #define Y6_GPIO_NUM 36
  80. #define Y5_GPIO_NUM 21
  81. #define Y4_GPIO_NUM 19
  82. #define Y3_GPIO_NUM 18
  83. #define Y2_GPIO_NUM 5
  84. #define VSYNC_GPIO_NUM 25
  85. #define HREF_GPIO_NUM 23
  86. #define PCLK_GPIO_NUM 22
  87. #else
  88. #error "Camera model not selected"
  89. #endif
  90. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  91. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  92. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  93. httpd_handle_t stream_httpd = NULL;
  94. static esp_err_t stream_handler(httpd_req_t *req){
  95. camera_fb_t * fb = NULL;
  96. esp_err_t res = ESP_OK;
  97. size_t _jpg_buf_len = 0;
  98. uint8_t * _jpg_buf = NULL;
  99. char * part_buf[64];
  100. res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  101. if(res != ESP_OK){
  102. return res;
  103. }
  104. while(true){
  105. fb = esp_camera_fb_get();
  106. if (!fb) {
  107. Serial.println("Camera capture failed");
  108. res = ESP_FAIL;
  109. } else {
  110. if(fb->width > 400){
  111. if(fb->format != PIXFORMAT_JPEG){
  112. bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  113. esp_camera_fb_return(fb);
  114. fb = NULL;
  115. if(!jpeg_converted){
  116. Serial.println("JPEG compression failed");
  117. res = ESP_FAIL;
  118. }
  119. } else {
  120. _jpg_buf_len = fb->len;
  121. _jpg_buf = fb->buf;
  122. }
  123. }
  124. else {
  125. if (fb->format != PIXFORMAT_JPEG){
  126. esp_camera_fb_return(fb);
  127. fb = NULL;
  128. } else {
  129. _jpg_buf = fb->buf;
  130. _jpg_buf_len = fb->len;
  131. }
  132. }
  133. }
  134. if(res == ESP_OK){
  135. size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  136. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  137. }
  138. if(res == ESP_OK){
  139. res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  140. }
  141. if(res == ESP_OK){
  142. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  143. }
  144. if(fb){
  145. esp_camera_fb_return(fb);
  146. fb = NULL;
  147. _jpg_buf = NULL;
  148. } else if(_jpg_buf){
  149. free(_jpg_buf);
  150. _jpg_buf = NULL;
  151. }
  152. if(res != ESP_OK){
  153. break;
  154. }
  155. //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  156. }
  157. return res;
  158. }
  159. void startCameraServer(){
  160. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  161. config.server_port = 80;
  162. httpd_uri_t index_uri = {
  163. .uri = "/",
  164. .method = HTTP_GET,
  165. .handler = stream_handler,
  166. .user_ctx = NULL
  167. };
  168. //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  169. if (httpd_start(&stream_httpd, &config) == ESP_OK) {
  170. httpd_register_uri_handler(stream_httpd, &index_uri);
  171. }
  172. }
  173. void setupCamera()
  174. {
  175. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  176. // Serial.begin(115200);
  177. // Serial.setDebugOutput(false);
  178. camera_config_t config;
  179. config.ledc_channel = LEDC_CHANNEL_0;
  180. config.ledc_timer = LEDC_TIMER_0;
  181. config.pin_d0 = Y2_GPIO_NUM;
  182. config.pin_d1 = Y3_GPIO_NUM;
  183. config.pin_d2 = Y4_GPIO_NUM;
  184. config.pin_d3 = Y5_GPIO_NUM;
  185. config.pin_d4 = Y6_GPIO_NUM;
  186. config.pin_d5 = Y7_GPIO_NUM;
  187. config.pin_d6 = Y8_GPIO_NUM;
  188. config.pin_d7 = Y9_GPIO_NUM;
  189. config.pin_xclk = XCLK_GPIO_NUM;
  190. config.pin_pclk = PCLK_GPIO_NUM;
  191. config.pin_vsync = VSYNC_GPIO_NUM;
  192. config.pin_href = HREF_GPIO_NUM;
  193. config.pin_sscb_sda = SIOD_GPIO_NUM;
  194. config.pin_sscb_scl = SIOC_GPIO_NUM;
  195. config.pin_pwdn = PWDN_GPIO_NUM;
  196. config.pin_reset = RESET_GPIO_NUM;
  197. config.xclk_freq_hz = 20000000;
  198. config.pixel_format = PIXFORMAT_JPEG;
  199. if(psramFound()){
  200. config.frame_size = FRAMESIZE_QVGA;
  201. config.jpeg_quality = 10;
  202. config.fb_count = 2;
  203. } else {
  204. config.frame_size = FRAMESIZE_QVGA;
  205. config.jpeg_quality = 10;
  206. config.fb_count = 2;
  207. }
  208. // Camera init
  209. esp_err_t err = esp_camera_init(&config);
  210. if (err != ESP_OK) {
  211. Serial.printf("Camera init failed with error 0x%x", err);
  212. return;
  213. }
  214. // Wi-Fi connection
  215. // WiFi.begin(ssid, password);
  216. // while (WiFi.status() != WL_CONNECTED) {
  217. // delay(500);
  218. // Serial.print(".");
  219. // }
  220. // Serial.println("");
  221. // Serial.println("WiFi connected");
  222. // Serial.print("Camera Stream Ready! Go to: http://");
  223. // Serial.println(WiFi.localIP());
  224. // Start streaming web server
  225. startCameraServer();
  226. }
  227. #endif

学习笔记,仅供参考,按照制作,不会成功!!!

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

闽ICP备14008679号