Rosserial, Interrupt output nonsense when range header assigned?

asked 2021-01-21 10:30:57 -0600

Obscuriosity gravatar image

updated 2021-01-24 12:28:01 -0600

Running Ubuntu 18.04 on raspberry pi 3+, Ros melodic, Arduino Mega

Warning - I am new to ROS.

I have created an arduino sketch which is an expanded version of the hello world example. I am sending messages from 3 bumper switches, 2 wheel encoders (forward and back each) and 8 sonar. Everything works well until I try to assign a header to the last of 8 sonar sensor_msgs::Range messages If I assign the header to sonar_8 the wheel encoder data becomes a very long integer with no relation to the output. if the header is not included all is fine. It is just this header, if I comment in or out other header assignments it makes no difference just snr_8?

So it all runs fine with no header on sonar 8.

I would prefer to have all information from all sonar and do not understand why I cannot.

Update: I experimented by reducing the sonars to 4 and the same thing happened, I must comment out the last header assignment, this time on sonar 4, to prevent the interrupt message becoming 6987879582638596873638 or something similar.

Any help or observations about my code would be most welcome.

Here is the code.

**#define bpr_lf_pin 6
**#define bpr_mf_pin 7
**#define bpr_rf_pin 5
**#define SONAR_NUM 8          //The number of sensors. 
**#define MAX_DISTANCE 50**

bool reading[3];
bool last_reading[3];
long last_debounce_time[3];
long debounce_delay = 30;
bool published[] = {true, true, true};
int switchPins[] = {bpr_lf_pin, bpr_mf_pin, bpr_rf_pin};

std_msgs::UInt64 enc_lf_msg;
std_msgs::UInt64 enc_lb_msg;
std_msgs::UInt64 enc_rf_msg;
std_msgs::UInt64 enc_rb_msg;
std_msgs::Bool bpr_lf_msg;
std_msgs::Bool bpr_mf_msg;
std_msgs::Bool bpr_rf_msg;
sensor_msgs::Range snr_1_msg;
sensor_msgs::Range snr_2_msg;
sensor_msgs::Range snr_3_msg;
sensor_msgs::Range snr_4_msg;
sensor_msgs::Range snr_5_msg;
sensor_msgs::Range snr_6_msg;
sensor_msgs::Range snr_7_msg;
sensor_msgs::Range snr_8_msg;

unsigned long previousMillis;
int interval;
volatile unsigned long leftFor, leftBac;
volatile unsigned long rightFor, rightBac;
bool Lenc, Renc;

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(37, 37, MAX_DISTANCE),
  NewPing(35, 35, MAX_DISTANCE),
  NewPing(33, 33, MAX_DISTANCE),
  NewPing(31, 31, MAX_DISTANCE),
  NewPing(29, 29, MAX_DISTANCE),
  NewPing(27, 27, MAX_DISTANCE),
  NewPing(25, 25, MAX_DISTANCE),
  NewPing(23, 23, MAX_DISTANCE)
};
int snrReadings[8]; // list to store readings

ros::NodeHandle nh;

ros::Publisher enc_lf_pub("/enc_lf", &enc_lf_msg);
ros::Publisher enc_lb_pub("/enc_lb", &enc_lb_msg);
ros::Publisher enc_rf_pub("/enc_rf", &enc_rf_msg);
ros::Publisher enc_rb_pub("/enc_rb", &enc_rb_msg);
ros::Publisher bpr_lf_pub("/bpr_lf", &bpr_lf_msg);
ros::Publisher bpr_mf_pub("/bpr_mf", &bpr_mf_msg);
ros::Publisher bpr_rf_pub("/bpr_rf", &bpr_rf_msg);
ros::Publisher snr_1_pub("/snr_1", &snr_1_msg);
ros::Publisher snr_2_pub("/snr_2", &snr_2_msg);
ros::Publisher snr_3_pub("/snr_3", &snr_3_msg);
ros::Publisher snr_4_pub("/snr_4", &snr_4_msg);
ros::Publisher snr_5_pub("/snr_5", &snr_5_msg);
ros::Publisher snr_6_pub("/snr_6", &snr_6_msg);
ros::Publisher snr_7_pub("/snr_7", &snr_7_msg);
ros::Publisher snr_8_pub("/snr_8", &snr_8_msg);

void sensor_msg_init(sensor_msgs::Range &range_name, char *frame_id_name)
{
  range_name.radiation_type = sensor_msgs::Range::ULTRASOUND;
  range_name.header.frame_id = frame_id_name;
  range_name.field_of_view = 0.26;
  range_name.min_range = 0.0;
  range_name.max_range = 2.0;
}
void setup()
{
  nh.initNode();
  nh.advertise(bpr_lf_pub);
  nh.advertise(bpr_mf_pub);
  nh.advertise(bpr_rf_pub);
  nh.advertise(enc_lf_pub);
  nh.advertise(enc_lb_pub);
  nh.advertise(enc_rf_pub);
  nh.advertise(enc_rb_pub);
  nh.advertise(snr_1_pub);
  nh.advertise(snr_2_pub);
  nh.advertise(snr_3_pub);
  nh.advertise(snr_4_pub);
  nh.advertise(snr_5_pub);
  nh.advertise(snr_6_pub ...
(more)
edit retag flag offensive close merge delete