本帖最后由 卿九金 于 2023-5-18 13:24 编辑
大佬们好,分享一下我用鲁班猫做ROS主控,stm32f407做底层驱动做的一个ros小车。 目的是识别烟雾并净化:净化是用的负离子发生器,外加扇叶将其扩散出去。同时也具有环境气体浓度(质量)检测的功能。 这是我去年12月底开始做的,入坑鲁班猫算是比较早了。在读大三学生。正奥里给考研中。。。
最底下还塞了一块vet6和一块esp32.
项目在电子发烧友的链接:https://bbs.elecfans.com/jishu_2344500_1_1.html
加了个散热风扇
以下是功能层的32部分,鲁班猫是用于ros建图和导航。 先介绍功能部分即功能层的stm32与串口屏、esp32通信部分。功能层的主要目的是获取传感器数据和通过继电器控制小车前端的负离子发生器和两个加快负离子扩散的风扇。 注:这里的stm32相当于一个中转,用的是rt—thread实时操作系统,版本是4.0.2(写的比较早,当时rtt有bug。) 因为用的是rtt,移植性很高,故只写了应用层的main.c函数。如下:气体传感器如下(所用的是串口协议)
- #include <rtthread.h>
- /*串口1用来调试*/
- #define DBG_TAG "main"
- #define DBG_LVL DBG_LOG
- #include <rtdbg.h>
- #include <string.h>
- #include <serial.h> //此处有坑,要改头文件路径为rt-thread/components/drivers/include/drivers
- #include <stdio.h>
- #include "stdlib.h"
- #define key1_open rt_pin_write(51,PIN_LOW );//d3
- #define key1_close rt_pin_write(51,PIN_HIGH );
- #define key2_open rt_pin_write(52,PIN_LOW );//d4
- #define key2_close rt_pin_write(52,PIN_HIGH );
- #define key3_open rt_pin_write(53,PIN_LOW );//d5
- #define key3_close rt_pin_write(53,PIN_HIGH );
- rt_thread_t majoy_th;
- /*micropython esp32与rtt串口DMA传输数据时有坑,
- * 需在drv_usart.c找到HAL_UART_RxCpltCallback和HAL_UART_RxHalfCpltCallback将dma_isr(&uart->serial)注释掉,
- * 能降低数据错误率*/
- /*串口2的变量 115200*/
- struct serial_configure uar2_configs = RT_SERIAL_CONFIG_DEFAULT;
- rt_sem_t sem2;
- rt_device_t uar2_dev;
- rt_thread_t uar_2_th;
- rt_thread_t uar_2_deal;
- char buffer[256] = {0};
- rt_size_t rxlen2 = 0;
- /*串口3的变量 9600*/
- struct serial_configure uar3_configs = MY_SERIAL_CONFIG_DEFAULT;
- rt_sem_t sem3;
- rt_device_t uar3_dev;
- rt_thread_t uar_3_th;
- uint8_t buffer3[17] = {0};
- rt_size_t rxlen3 = 0;
- /*串口4的变量 115200*/
- struct serial_configure uar4_configs = RT_SERIAL_CONFIG_DEFAULT;
- rt_sem_t sem4;
- rt_device_t uar4_dev;
- rt_thread_t uar_4_th;
- rt_uint8_t buffer4[256] = {0xff};
- rt_size_t rxlen4 = 0;
- //char deal;
- rt_uint8_t deal ;
- char wheater[8];
- char humidity[4];
- char temperature[4];
- char wind_speed[6];
- char shi[3];
- char miao[3];
- char fen[3];
- char wheater_deal[21="main2.g3.txt="";
- char humidity_deal[17="main2.g1.txt="";
- char shi_deal[15] = "main.z1.val=";
- char miao_deal[15] = "main.z0.val=";
- char end[1=""";
- char xf_end[3];
- void uar2_thread_entry(void *parameter)//串口2DMA线程入口
- {
- rt_size_t len = 0;
- char temperature_deal[17="main2.g0.txt="";
- char fen_deal[16] = "main.z2.val=";
- while(1)
- {
- rt_sem_take(sem2, RT_WAITING_FOREVER);
- char wind_speed_deal[19="main2.g2.txt="";
- len = rt_device_read(uar2_dev, 0, buffer, rxlen2);
- buffer[len] ='\0';
- rt_kprintf(buffer);
- ....(部分代码)
复制代码
接下来是鲁班猫1s做ros主控的部分。1、移植轮趣大佬的ros源码:根据我现有的硬件:思岚a1雷达、一个usb rgb摄像头选择合适的功能包,然后开始移植。中途会出现很多错误。例如缺少部分功能包,sudo apt install ros-noetic-(包名)【我的ros版本是noetic】。2、移植完毕后发现大佬们并没有使用鲁班猫上的npu。所以我尝试了用npu跑yolov5在debain10的环境下用python接口效果如下: Python与c++接口将图片监测改成实时摄像头的代码:只需更改cv.Capture()函数的摄像头设备号即可。
链接:https://pan.baidu.com/s/1gauOezF-X8ZuvU4b0I4v4A?pwd=jhzs提取码:jhzs
Python接口的yolov7只需更改yolov5代码的锚点即可,以下只列出主函数主要部分,完整的在链接里。
- capture = cv2.VideoCapture(9)
- ref, frame = capture.read()
- if not ref:
- raise ValueError("error reading")
-
- fps = 0.0
- while(True):
- t1 = time.time()
- #
- ref, frame = capture.read()
- if not ref:
- break
- # BGRtoRGB
- frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
-
- #############
- #
- img = frame
- img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
-
- # Inference
- print('--> Running model')
- outputs = rknn.inference(inputs=[img])
-
-
- input0_data = outputs[0]
- input1_data = outputs[1]
- input2_data = outputs[2]
- input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))
- input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))
- input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))
- input_data = list()
- input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
- input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
- input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
- boxes, classes, scores = yolov5_post_process(input_data)
-
- img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
- #img_1 = img_1[:,:,::-1]
- if boxes is not None:
- draw(img_1, boxes, scores, classes)
- fps = ( fps + (1./(time.time()-t1)) ) / 2
- print("fps= %.2f"%(fps))
- #img_1 = cv2.putText(frame, "fps= %.2f"%(fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
- cv2.imshow("video",img_1[:,:,::-1])
- c= cv2.waitKey(1) & 0xff
- if c==27:
- capture.release()
- break
- print("Video Detection Done!")
- capture.release()
- cv2.destroyAllWindows()
复制代码
但还没用接到ros中,为此我去翻rknn的github找到了接入ros的方法。
Ros功能包如下:Launch文件: yolov5.launch
- <launch>
- <arg name="display_output" default="true"/>
- <arg name="camera_topic" default="/usb_cam/image_raw"/>
- <arg name="chip_type" default="RK3566"/>
- <node name="rknn_yolov5_node" pkg="rknn_ros" type="rknn_yolov5_node" output="screen">
- <param name="model_file" value="yolov5s-640-640.rknn"/>
- <param name="display_output" value="$(arg display_output)"/>
- <param name="prob_threshold" value="0.35"/>
- <param name="chip_type" value="$(arg chip_type)"/>
- <remap from="/camera/image_raw" to="$(arg camera_topic)"/>
- </node>
- </launch>
复制代码Camrea.Launch - <launch>
- <arg name="device" default="video9"/>
- <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" >
- <param name="video_device" value="/dev/$(arg device)" />
- <param name="image_width" value="640" />
- <param name="image_height" value="480" />
- <param name="framerate" value="30" />
- <param name="pixel_format" value="yuyv" />
- <param name="camera_frame_id" value="usn_cam" />
- <param name="io_method" value="mmap"/>
- <param name="camera_name" value="usn_cam"/>
- </node>
- </launch>
复制代码
启动摄像头默认的摄像头设备号为video0 鲁班猫为video9
1、roslaunch rknn_ros camera.launch
2、roslaunch rknn_ros camera.launch device:=video9(可传参或者改launch)
3、roslaunch rknn_ros yolov5.launch chip_type:=RK3566
(ros包链接)
yolov5-master\models下的yolo.py找到def forward(self, x):函数,更改为:
- def forward(self, x):
- z = [] # inference output
- for i in range(self.nl):
- if os.getenv('RKNN_model_hack', '0') != '0':
- z.append(torch.sigmoid(self.m[i](x[i])))
- continue
-
- x[i] = self.m[i](x[i]) # conv
- '''
- bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
- x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
-
- if not self.training: # inference
- if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
- self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
-
- y = x[i].sigmoid()
- if self.inplace:
- y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy
- y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
- else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
- xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
- xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
- wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
- y = torch.cat((xy, wh, conf), 4)
- z.append(y.view(bs, -1, self.no))
-
- if os.getenv('RKNN_model_hack', '0') != '0':
- return z
-
- return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
- '''
- return x[0],x[1],x[2]
复制代码
这样就可以在pt权重转onnx时去掉最后一个Detect层。 pt转onnx指令python export.py --weights yolov5s.pt --img 640 --batch 1 --opset 11 --include onnx
红色字体部分换成要转换的权重文件例如我的就是:python export.py --weights weights/best.pt --img 640 --batch 1 --opset 11 --include onnxopset选择11。 这样出来的模型是有三个节点的模型,才是可用的。
有三个节点。且要记好三个节点的名字。 效果如图:
整车的sw模型链接:soildwork2020及以上版本可直接打开
链接:https://pan.baidu.com/s/1KqB1SOD418dCvyDaZFMgpg?pwd=jhzs
提取码:jhzs
当时还理想化的撸了个履带,可后来发现打印出来根本用不了,故放弃,换成了轮子。放链接是希望能够帮到像我一样步步踩坑的菜鸟级选手。我是老踩坑怪了。
有不当的地方,还望大佬们海涵。
|