Hi everyone,
I received this mail and I prefer to answer it here:
—
I've not used event timers before in robots like these and I am not seeing how to set that up in text-based code.
we want to go forward 2 seconds, turn left, go backwards 3 seconds, then stop.
how can I code that?
—
The easiest is to code a state machine. I will go quickly now but don't hesitate to ask for more info :)
Here is the final code:
var state = STOP
var counter = 0
timer.period[0] = 200
onevent timer0
if state != STOP then
counter++
if state == START and counter > TIME_STRAIGHT then
state = TURN
motor.left.target = -SPEED
motor.right.target = SPEED
counter = 0
end
if state == TURN and counter > TIME_TURN then
state = BACK
motor.left.target = -SPEED
motor.right.target = -SPEED
counter = 0
end
if state == BACK and counter > TIME_BACK then
state = STOP
motor.left.target = 0
motor.right.target = 0
counter = 0
end
end
onevent buttons
when button.forward == 1 do
state = START
motor.left.target = SPEED
motor.right.target = SPEED
end
with these constants:
<constant value="200" name="SPEED"/>
<constant value="0" name="STOP"/>
<constant value="1" name="START"/>
<constant value="2" name="TURN"/>
<constant value="3" name="BACK"/>
<constant value="10" name="TIME_STRAIGHT"/>
<constant value="5" name="TIME_TURN"/>
<constant value="15" name="TIME_BACK"/>
(the constants are added on the top right of Aseba IDE, it's easier to change the global values like speed with that)
In two words, here is how it works:
- The state variable manages the state of the robot. It is equal to STOP, START, TURN… (it's just a value like 0, 1, 2, the essential is that you don't have two states with the same value).
- By pressing the forward button, the robot goes to START and start the motors.
- Then, in the timer0 event, it counts the time (timer = 200 ms => every time a timer0 event is triggered, 200 ms passed).
- Then, one state after the other, the robot changes its wheels speed. The constants TIME_… are used to know how much time to take for every state.
The TIME_TURN has to be adjusted regarding the rotation we want to make. The bigger it is, the longer the rotation.
I have to go now, but ask if anything is not clear :)
Best
Christophe