I'm writing a mobile robot C program which reads data from a serial port to check some sensors and then try to load a joypad to control two servos.
Since i'm using a blocking read to acquire data for sonars from serial port, i can't get input from joypad and from the serial port at the same time, so i can't stop my robot in time if sonars detect a possible collision.
For this reason, after having received few infos from the serial port, I start an infinite while loop to reads possible collisions from the serial port and I start a new thread with Boost library to load and receive input from the joypad
With this solution, i'm able to use the joypad and receive collisions information at the same time; the problem is that i'm not able to share variable between the previous thread which reads from serial port and the new thread which load the joypad so the previous thread is not able to communicate the the joypad thread if a collision occurs
I want to do this:
Previous thread detect a collision -> write a value in a linked list -> new thread read it and stop servos.
What can i do in order to let the new thread detect a collision from the previous thread?
I have to use signal() function?
When i try to compile this code, g++ says me that linked list, and in particular, result variable is not declared in the scope.
I think because new thread does not share the old variables and declarations.
// do a lots of stuff then..
boost::thread collisions_thread1(&robot_move); <-- LOAD JOYPAD IN A NEW THREAD
std::string detect_collisions = s_readBlocking(serial_port, TIME_DELAY);
parseCommand(detect_collisions); <-- I SAVE COLLISIONS IN A LINKED LIST
// load joypad and then check for collisions
// check for collisions while getting input from joypad
std::map<std::string, std::string>::const_iterator sonar_collisions = result.find("sonar-collision");
if(sonar_collisions != result.end() && (sonar_collisions->second == "ok"))
std::cout << "STOP!\nCollisions " << sonar_collisions->second << " e count= " << collisions << "\n";