Cleanup in a thread

3 replies [Last post]
Title: NooBot
Joined: 02/04/2013
BotPoints: 22
User offline. Last seen 9 years 21 weeks ago.

  1. void offAll();
  2. thread turnOff;
  3. int main()
  4. {
  5. create_connect();
  6. create_full();
  7. set_a_button_text("Stop");
  8. turnOff = thread_create(offAll);
  9. thread_start(turnOff);
  10. while (1)
  11. printf("Running\n");
  12. return 0;
  13. }
  14. void offAll()
  15. {
  16. while (!a_button())
  17. {
  18. // do nothing while button not pressed
  19. }
  20. thread_destroy(turnOff);
  21. create_stop();
  22. create_disconnect();
  23. printf("Turning off\n");
  24. _Exit(0);
  25. }

in the offAll() method, how can we stop the brick from executing? Currently the method stops the create and disconnects but continues to move regular servos/motors, and still gets data from sensors, etc.
What's the method for killing the current code executing?

Marty Rand
Marty Rand's picture
Title: Botball Youth Advisory Council
Joined: 07/04/2009
BotPoints: 253
User offline. Last seen 8 years 6 weeks ago.


  1. shut_down_in(0.0);


  1. shut_down_in(0.1);

shut_down_in() is the normal KIPR function to stop the robot at the end of a match, but it doesn't stop the create. Add one of those to offAll() and see what happens.

-Marty Rand
Senior programmer at Norman Advanced Robotics

Former senior programmer at Whittier Middle School

Youth Advisory Council

All around nerd

Title: NooBot
Joined: 02/13/2012
BotPoints: 130
User offline. Last seen 5 years 47 weeks ago.

Didn't they fix the create not stopping bug in 1.9.5?

-Daniel Goree
Senior Project Manager
Norman Advanced Robotics (13-0113)

Title: NooBot
Joined: 06/01/2009
BotPoints: 28
User offline. Last seen 9 years 22 weeks ago.

To reply to your question:

(1) Do not call thread_destroy from within the thread that it is destroying. This may produce some unpredictable side effects. You don't need it here anyway.

(2) __Exit is an immediate termination of the program. This includes the main thread. The only cleanup is that it closes the file descriptors.

(3) As you have noticed the servos, motors, etc. are still active. This is because the result of mav() (for example) is that a value is loaded into a hardware register, which results in the motor moving and this action is asynchronous to your program. When __exit is called, nothing has been done to stop the motor (and servos) as those registers still have their original values in them.

So, before _exit is called in offAll(), you need to put in the code to stop the motors and disable the servos.

Eugene Myers
Cedarhouse Robotics