ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

how to change an arduino code into a publisher

asked 2021-01-31 00:53:29 -0600

ar89 gravatar image

updated 2021-01-31 01:08:48 -0600

enter code here #include <ros.h> #include <ros time.h=""> #include <sensor_msgs range.h=""> #include <std_msgs int32.h=""> #include <encoder.h>

ros::NodeHandle nh;
sensor_msgs::Range range_msg;
ros::Publisher pub_range( "range_data", &range_msg);

// Change these pin numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder encoder1(44, 45);
Encoder encoder2(46, 47);
Encoder encoder3(48, 49);
Encoder encoder4(50, 51);
//   avoid using pins with LEDs attached

void setup() {
  nh.initNode();
  nh.advertise(pub_range);

  Serial.begin(9600);
  Serial.println("Four Encoder Test:");
  for(int i=44;i<=51;i++)digitalWrite(i,1);   
}

long positionencoder1 = -999;
long positionencoder2 = -999;
long positionencoder3 = -999;
long positionencoder4 = -999;

void loop() {
  char buffer[50];

  long newencoder1, newencoder2, newencoder3, newencoder4;
  newencoder1 = encoder1.read();
  newencoder2 = encoder2.read();
  newencoder3 = encoder3.read();
  newencoder4 = encoder4.read();
  if (newencoder1 != positionencoder1 || newencoder2 != positionencoder2 || newencoder3 != positionencoder3 || newencoder4 != positionencoder4) {
 {
   sprintf(buffer, "E1 = %ld", encoder1.read());
   nh.loginfo(buffer);
   sprintf(buffer, "E2 = %ld", encoder2.read());
   nh.loginfo(buffer);
   sprintf(buffer, "E3 = %ld", encoder3.read());
   nh.loginfo(buffer);
   sprintf(buffer, "E4 = %ld", encoder4.read());
   nh.loginfo(buffer);
   Serial.println();
   positionencoder1 = newencoder1;
   positionencoder2 = newencoder2;
   positionencoder3 = newencoder3;
   positionencoder3 = newencoder4;

   pub_range.publish(&range_msg);
   nh.spinOnce();
 }
 }
 }
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-02-01 03:00:21 -0600

felixN gravatar image

Hi, could you please be a bit more specific about the problem?

Dosn't the code compile? Or does it compile but you don't get any data on your computer? Or you don't know how to write the code?

A guess might be that you run the code on your the arduino, and nothing happens on your computer. If that's so, have you the rosserial node running on the computer (with proper baudrate and port)?

edit flag offensive delete link more

Comments

it aint publishing and i am new to ros and arduino...it is compiling but i am not getting the output on the terminal

ar89 gravatar image ar89  ( 2021-02-01 05:59:33 -0600 )edit

Do you run a rosserial node? If so, could you provide the exact command you use to launch it?

Could you also give the outputs of the following commands :

rosnode list

rostopic list

felixN gravatar image felixN  ( 2021-02-01 08:02:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-01-31 00:53:29 -0600

Seen: 100 times

Last updated: Feb 01 '21