Exercise 2.2 Segmentation Fault

Hello @rzegers

I am stuck here. I know there is something wrong with my for loop to assign the values to my vector called “heading” but I’m not sure what the fix is. The fault comes right after the line " cout << "temp new value = " << temp << endl; "

#include <ros/ros.h>
#include <iostream>
#include "robot_commander/robot_commander.h"
#include <vector>
using namespace std;

int main(int argc, char **argv){

ros::init(argc, argv, "heading_vector");

RobotCommander my_robot;
my_robot.move_in_circles();

int total_values; 
cout << "enter number of heading values: " << endl;
cin >> total_values;
vector<float> headings;
cout << "starting for loop: " << endl;
float temp = 0;
cout << "temp value = " << temp << endl;

for (int i = 0; i < total_values; i++){
temp = my_robot.get_heading();
cout << "temp new value = " << temp << endl;
headings[i] = temp;
 }

 for (int x : headings){
 cout << "Heading is: " << x << endl;
 }

my_robot.stop_moving();
return 0;

}

@bitcloude ,

I have experienced similar problems. Here are my solutions (may not be the actual solution but worked for me).

If you are getting your Heading from IMU, there seems to be a problem with that. For me (at least), the IMU only worked after ROS restart for simulations. So if you run a program, stop/kill it and then re-run the program with IMU, the IMU does not work. Fix is to restart ROS everytime, if possible.

If you get your Heading from Odometry, then the solution (might be) is to move the robot in some direction a little bit for about 1 second and then take the readings. [Somehow moving seems to kick-start the subscribers in ROS].

Try this and let me know if this worked for you!

Regards,
Girish

I will try this… And let you know. thank you @girishkumar.kannan

Actually the heading is coming from the laser. Does that change your answer any? @girishkumar.kannan

@bitcloude ,
It you get your Heading from laser, the answer would be same as what I said regarding Odometry.
In short: You still need to move the robot for 1 second before you can acquire your heading values, else you will get a segmentation fault.
Refer these posts:

Hope I helped!

Regards,
Girish

Hello @bitcloude,
did you manage to solve this?
It could be that what you type as input to cin is causing the loop to fail.
To do a test please comment out the line with cin >> total_values; and use a hard coded value for the variable total_values . Let’s see what happens then.

Regards,

Roberto

@rzegers i tried that as well. It does not work.

@bitcloude ,

I think I see the problem here.

Your code seems to work in a blocking manner / synchronously.
Your my_robot.move_in_circles(); would complete before you get the headings.
This means that you will never get any values for headings in the for loop since headings is not pointing to laser scans from Robot Commander nor has any initialized values.

Thus, you get a segmentation fault in place of the g++ compiler printing garbage values.

Hope I was clear and not confusing you.

Let me know if you understood!

Regards,
Girish

Hmm maybe I need to look at the definition of movecircles(). I thought the code ran until it was told to stop using the stop() function @girishkumar.kannan

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.