how to change an arduino code into a publisher
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();
}
}
}