首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从rosserial arduino发布Int16MultiArray

问如何从rosserial arduino发布Int16MultiArray
EN

Stack Overflow用户
提问于 2021-10-20 16:29:19
回答 1查看 223关注 0票数 1

我正在尝试发布ros包mecanum_drive:https://github.com/dudasdavid/mecanum_drive的Int16MultiArray。

我的问题是,我似乎不能从我的arduino发布数组。(我使用的是Teensy 4.1)

代码语言:javascript
复制
#include <std_msgs/Int16MultiArray.h>

ros::NodeHandle nh;
std_msgs::Int16MultiArray wheel_ticks;
ros::Publisher wheel_ticks_pub("wheel_ticks", &wheel_ticks);
void setup() {
  nh.getHardware()->setBaud(115200); //was 115200
  nh.initNode();              // init ROS
  nh.advertise(wheel_ticks_pub);
}

void loop() {
  // put your main code here, to run repeatedly:
 //I have tried the code below which uploads to the arduino, but rostopic then says that it dosnt contain any data
 /*
 short value[4] = {0,100,0,0};
 wheel_ticks.data = value;
 */

 //I also tryed the code below which uploads, but then the teensy looses its serial port (arduino port says"[no_device] Serial(Teensy4.1)": 
 /*
 wheel_ticks.data[0] = 10;
 wheel_ticks.data[1] = 5;
 */

 //below gives this error: cannot convert '<brace-enclosed initializer list>' to 'std_msgs::Int16MultiArray::_data_type* {aka short int*}' in assignment
 /*
 wheel_ticks.data = {0,0,0,1};
 */
 wheel_ticks_pub.publish(&wheel_ticks);

nh.spinOnce();      
}

我试过的所有东西要么没有上传,要么上传了,但是序列被搞乱了,或者上传了,rostopic echo说它是空的。

谢谢你看这篇文章,希望你能帮上忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-20 16:41:47

rosserial的一个非常特殊的限制是数组有一个专门用于数据长度的额外字段。这是必需的,因为数据字段是作为指针实现的,因此没有真正好的方法来获取数据长度。消息类型实际上如下所示

代码语言:javascript
复制
class Int16MultiArray{
    Header header;
    int data_length;
    int16_t * data;
};

因此,您所要做的就是在发送消息之前设置数据字段

代码语言:javascript
复制
#include <std_msgs/Int16MultiArray.h>

ros::NodeHandle nh;
std_msgs::Int16MultiArray wheel_ticks;
ros::Publisher wheel_ticks_pub("wheel_ticks", &wheel_ticks);
void setup() {
  nh.getHardware()->setBaud(115200); //was 115200
  nh.initNode();              // init ROS
  nh.advertise(wheel_ticks_pub);
}
void loop() {
 
  short value[4] = {0,100,0,0};
  wheel_ticks.data = value;
  wheel_ticks.data_length = 4;
 
  wheel_ticks_pub.publish(&wheel_ticks);

  nh.spinOnce();      
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69649792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档