Society of Robots - Robot Forum

Software => Software => Topic started by: Crunchy Theory on September 19, 2011, 10:47:22 PM

Title: Differential Drive Robot - state machine
Post by: Crunchy Theory on September 19, 2011, 10:47:22 PM
I finally finished the movement code for my tankbot. I took Webbot's example (http://www.societyofrobots.com/robotforum/index.php?topic=10754.0 (http://www.societyofrobots.com/robotforum/index.php?topic=10754.0)) and fleshed it out more. It works nicely for simple states like "wait", "forward", "reverse" but it gets messy if I use this technique to control each tread individually (like if I want the robot to curve by having one tread move faster than the other). So I basically have two giant switch statements, one for each tread, that are almost identical except all the variables have a "left"/"right" appended to the name. The only point in the code where the states converge is when poll for button.pressed (kill switch).

So, have you guys come up with a better technique for more advanced control of a differential drive robot? And for more context, I am planning to have the Axon receive serial commands from its companion ARM dev board that handles the computer vision. The serial commands will dictate how the robot will adjust its position based on what it's seeing. So maybe a state machine isn't even right for this application - I'm not sure.
Title: Re: Differential Drive Robot - state machine
Post by: rbtying on September 20, 2011, 01:13:28 AM
The way I usually implement this is to have states that utilize external variables:
Code: [Select]

enum { STOPPED, MOVE, FWD, REV } state;

switch (state) {
case REV:
  leftSpeed = rightSpeed = REV_SPEED;
  state = MOVE;
  break;
case FWD:
  leftSpeed = rightSpeed = FWD_SPEED;
  state = MOVE;
  break;
case STOPPED:
  leftSpeed = 0;
  rightSpeed = 0;
case MOVE:
  motors.setSpeed(leftSpeed, rightSpeed);
  break;
}

Serial commands can just send the leftSpeed and rightSpeed values over as 8-bit signed integers, and as long as the state is MOVE, the commands will be followed.