当前位置:网站首页>esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
2022-08-04 01:32:00 【zhangrelay】

Sample program test portad数值:
#include "Arduino.h"
#include <ESP32AnalogRead.h>
ESP32AnalogRead adc;
void setup()
{
adc.attach(34);
Serial.begin(115200);
}
void loop()
{
delay(50);
Serial.println("Voltage = "+String(adc.readVoltage()));
}This is just a simple testadport voltage program,没有滤波,No numerical transformation is performed according to the actual situation.
34is the measurement voltage port.
初始化,adand serial port baud rate:
void setup()
{
adc.attach(34);
Serial.begin(115200);
}Cycle the voltage and output the serial port,时延50ms:
void loop()
{
delay(50);
Serial.println("Voltage = "+String(adc.readVoltage()));
}This is a simple voltage measurement program for a single-chip microcomputer.
Supplemental filtering and scaling are required.留作思考题.
效果如下图:

micro-rosAn example of publishing a message is as follows:
#include <micro_ros_arduino.h>
#include <stdio.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <std_msgs/msg/int32.h>
#if !defined(ESP32) && !defined(TARGET_PORTENTA_H7_M7) && !defined(ARDUINO_NANO_RP2040_CONNECT)
#error This example is only avaible for Arduino Portenta, Arduino Nano RP2040 Connect and ESP32 Dev module
#endif
rcl_publisher_t publisher;
std_msgs__msg__Int32 msg;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;
#define LED_PIN 13
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
void error_loop(){
while(1){
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(100);
}
}
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
RCLC_UNUSED(last_call_time);
if (timer != NULL) {
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
msg.data++;
}
}
void setup() {
set_microros_wifi_transports("***", "***", "***", 8888);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
delay(2000);
allocator = rcl_get_default_allocator();
//create init_options
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
// create node
RCCHECK(rclc_node_init_default(&node, "micro_ros_arduino_wifi_node", "", &support));
// create publisher
RCCHECK(rclc_publisher_init_best_effort(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"topic_name"));
msg.data = 0;
}
void loop() {
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
msg.data++;
}What code needs to be modified?
ROS1/2The data types in are as follows:
ROS Message Types
Bool
Byte
ByteMultiArray
Char
ColorRGBA
Duration
Empty
Float32
Float32MultiArray
Float64
Float64MultiArray
Header
Int16
Int16MultiArray
Int32
Int32MultiArray
Int64
Int64MultiArray
Int8
Int8MultiArray
MultiArrayDimension
MultiArrayLayout
String
Time
UInt16
UInt16MultiArray
UInt32
UInt32MultiArray
UInt64
UInt64MultiArray
UInt8
UInt8MultiArray
battery电池,Voltage situation:

高>4.1v为插入usb口,Low for battery power.

需要启动agent:

参考程序如下:
#include <micro_ros_arduino.h>
#include <stdio.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <std_msgs/msg/float32.h>
#if !defined(ESP32) && !defined(TARGET_PORTENTA_H7_M7) && !defined(ARDUINO_NANO_RP2040_CONNECT)
#error This example is only avaible for Arduino Portenta, Arduino Nano RP2040 Connect and ESP32 Dev module
#endif
#define BAT_DET 34
rcl_publisher_t publisher;
std_msgs__msg__Float32 msg;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;
#define LED_PIN 13
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
void error_loop(){
while(1){
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(100);
}
}
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
RCLC_UNUSED(last_call_time);
if (timer != NULL) {
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
msg.data++;
}
}
void setup() {
set_microros_wifi_transports("***", "***", "***", 8888);
pinMode(BAT_DET, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
delay(2000);
allocator = rcl_get_default_allocator();
//create init_options
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
// create node
RCCHECK(rclc_node_init_default(&node, "robot_battery_wifi_node", "", &support));
// create publisher
RCCHECK(rclc_publisher_init_best_effort(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32),
"robot_battery"));
msg.data = 0.66;
}
void loop() {
float battery = 4.21 * analogRead(BAT_DET) / 2435;
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
msg.data=battery;
delay(1000);
}边栏推荐
- 5.scrapy中间件&分布式爬虫
- 【正则表达式】笔记
- Google Earth Engine ——利用公开的河流数据计算河流的有效宽度
- 虚拟机CentOS7中无图形界面安装Oracle
- [store mall project 01] environment preparation and testing
- typescript56 - generic interface
- nodejs+npm的安装与配置
- Demand analysis of MES management system in electronic assembly industry
- 网络带宽监控,带宽监控工具哪个好
- Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
猜你喜欢

哎,又跟HR在小群吵了一架!

阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践

js中常用的几种遍历处理数据的方法梳理

持续投入商品研发,叮咚买菜赢在了供应链投入上

nodejs+express realizes the access to the database mysql and displays the data on the page

initramfs详解----添加硬盘驱动并访问磁盘

特征值与特征向量

网络带宽监控,带宽监控工具哪个好

redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)

Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
随机推荐
一个注解替换synchronized关键字:分布式场景下实现方法加锁
在Activity中获取另一个XML文件的控件
Demand analysis of MES management system in electronic assembly industry
typescript53 - generic constraints
LeetCode third topic (the Longest Substring Without Repeating Characters) trilogy # 3: two optimization
[store mall project 01] environment preparation and testing
ThreadLocal
pygame 中的transform模块
多渠道打包
【虚拟化生态平台】虚拟化平台搭建
2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
Slipper —— 虚点,最短路
typescript51-泛型的基本使用
OpenCV如何实现Sobel边缘检测
Analysis: What makes the Nomad Bridge hack unique
企业虚拟偶像产生了实质性的价值效益
Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
GNSS文章汇总
- heavy OpenCV 】 【 mapping
互斥锁、读写锁、自旋锁,以及原子操作指令xaddl、cmpxchg的使用场景剖析