Hi, I'm abit frustrated using rosserial at the moment. my code below
Generally works fine, when i establish connection with ROS i am able to view my encoder readings from the first motor aswell. The moment i publish speed data to any of the topics the arduino node is subscribed to, the sensor readings turn to 0 as though being canceled out. Please any help will be much appreciated.
#include <Arduino.h>
#include <ros.h>
#include <std_msgs/UInt16.h>
#include <stdio.h>
#include <stdlib.h>
#include "speedmeter.h"
#include "motor.h"
#include "brake.h"
#define roboclawRxPin1 9
#define roboclawTxPin1 8
#define roboclawRxPin2 11
#define roboclawTxPin2 10
ros::NodeHandle nh;
std_msgs::UInt16 speed_msg1;
//----------------------------------Publish Encoder readings-------------------
ros::Publisher pb_speed1("pb_speed1", &speed_msg1);
//----------------------------------Initialize motor and encoder values------------------
Motor *myMotor1;
Motor *myMotor2;
Encoder *myEncoder1;
Encoder *myEncoder2;
//----------------------------------Subscriber call back functions for R1 Speed---------------
void speedCallback1(const std_msgs::UInt16& cmd_msg) {
myMotor1->MotorSpeed(cmd_msg.data);
}
//----------------------------------Subscriber callback functions for R2 Speed-------------
void speedCallback2(const std_msgs::UInt16& cmd_msg){
myMotor2->MotorSpeed(cmd_msg.data);
}
//----------------------------------Create subscription instance for ROS-Subscriber-----------------
ros::Subscriber<std_msgs::UInt16> sub_speed1("pub_speed1", speedCallback1);
ros::Subscriber<std_msgs::UInt16> sub_speed2("pub_speed2", speedCallback2);
//---------------------------------Initialize ROS,motor, and encoders---------------
void setup() {
nh.getHardware()->setBaud(115200);
nh.initNode();
nh.advertise(pb_speed1);
//------------------------Create new motor object for controlling rollers1&2--------------
myMotor1 = new Motor(roboclawRxPin1, roboclawTxPin1);
myMotor1->MotorSpeed(10);
myMotor2 = new Motor(roboclawRxPin2, roboclawTxPin2);
myMotor2->MotorSpeed(10);
//------------------------Subscribe to published speeds from ROS---------------
nh.subscribe(sub_speed1);
nh.subscribe(sub_speed2);
//-----------------------Creates a new encoder object for interfacing with the motor------------------------
myEncoder1 = new Encoder(roboclawRxPin1, roboclawTxPin1);
}
//------------------------------Reads and publishes encoder value ----------------------------
void loop() {
//------------------------------Reads the encoder value---------------------------
int speed1Value = myEncoder1->readSpeed();
Serial.print("Encoder1: ");
Serial.println(speed1Value);
//-------------------------------Assign encoder vvalue to message -----------------------------
speed_msg1.data = speed1Value;
Serial.println(speed1Value);
//-------------------------------Publish message and handle ROS communication ------------------------------
pb_speed1.publish(&speed_msg1);
nh.spinOnce();
delay(100);
}
and the ROS topic publishing.
rostopic pub /pub_speed1 std_msgs/UInt16 'data: 20'