Autonomous code

I was just trying to make my robot controller run forward for some time, and then backward. I have tried so hard but the thing acts so wierd.
I started with the following code, before the serout statement:
count VAR byte
FOR count=1 TO 100
debug ? count
I can see the counter going from 1 to 100 and repeating infinitely in the debug screen, but the p1_y doesn’t go to 150, and the operator interface says BASIC ERROR

I’m a newbie, so please tell me what is wrong with this code, and help me write code for making the robot just move forward for some time, and then backwards for some time

The RC program must loop through the “Do … Loop” about once every 40 ms. It may be looosing sync with the OI with the time you are wasting, because, for each packet-loop, you’re counting to 150, and for each count value you’re sending a debug message and re-setting p1_y to a value without leaving your “count-loop”. For each packet-loop. you’re also doing the processing of the “Do … Loop”.

Now, the OI sends a new buch of values to the Serin line every few ms. each with a (no pun inteded) serial number, called a packet number which the default program ignores.

If you allow the RC to read packet_num (uncomment the declaration of packet_num near the end of declarations, near the start of the program, set c_packet_num CON 1 (changed from 0), and put packet_num in the correct place in the serin line) then you can store the packet number in a variable like “start_packet_num”, and set p1_y to your value until you have gone 150 loops.

if auton_mode = 0 then fugeddabahtit

if packet_num = 0 then pack_count_zero = pack_count_zero + 1
'you have to declare the variables like pack_count_zero and temp (seen below)

if packet_num < start_packet_no then it_overflowed
temp = packet_num - start_packet_num
goto to checked_p_num

temp = (((2000 + packet_num + 256) - start_packet_num) - 2000)
'if the start_packet_num is more than 105, then in 150 loops it will roll over to a numer smaller than that. this cures the problem for loop counts of less than 256

if temp < 151 then p1_y = (your value)


BTW, 150 counts is only a few seconds in the above. I’m sure someone will call me a doddering old fool or worse for not remembering whether you get an update 25 or 40 times per second. A quick perusal of the 2003 OI manual yields no info.

don’t you need a serout?


Well lets see, how can you go forward and then backward

Well try to find these three lines of code

MainLoop :
'(there is code between here)

Serout USERCPU, OUTBAUD, [255,255,p1_y,relayA,p2_y,relayB,p3_y,p4_y,p1_x,p2_x,p3_x,p4_x,p1_wheel,p2_wheel,p3_wheel,p4_wheel]

'(there is code between here)
Goto MainLoop :

Well the way it works is that every 26 miliseconds or so, the program goes through the loop and sends output through the serout command. You can’t use a for loop – it would stay inside the for loop and never get to the serout command. What you want is a counter variable to keep track of how long the robot goes forward and backwards.

For example :

'=== DECLARE VARIABLES (out of main loop)
state var byte
state = 0
counter var byte
counter = 0

’ PUT THIS AFTER MainLoop, before SEROUT command
if NOT (auton_mode = 1 and state = 0) then skip1
state = 1
counter = 0
p1_y = 254

counter = counter + 1

if NOT (state = 1 and counter = 50) then skip2
state = 2
p1_y = 0
counter = 0

if NOT (state = 2 and counter = 50) then skip3
state = 3
p1_y = 127
’ =========

You could easily alter this for tank-steering/other purposes

You notced I use the NOT and brackets, that’s how I get into making the if statement a true if statement… it normally goes to the label, but if I put a NOT, it reverses, going to the label if the opposite is true… neat eh?

I’m going to be posting our team’s autonomous code soon. The problem I heard from another post is that since the battery voltage changes, motor speed changes, so using an internal counter is not that reliable… but I’m not that convinced that can happen, how much voltage drop is going to happen in 2 minutes?

Hope this helps

Brian Lim

replace this part if you want it so it starts when you press a joystick button.

if NOT (p1_sw_top = 1 and state = 0) then skip1
state = 1
counter = 0
p1_y = 254

or, if you want something simpler, using the 2.5 code:

counter VAR WORD
counter = 0

inside the do…loop:

if counter++ > 500 then 'arbitrary time value
p1_x = 254
p1_x = 0

should go forward for 13000 milliseconds, then go backwards.
change the 500 value up/down to have longer/shorter, it should incriment once every 26 ms (based on speeds of the default code)


*Originally posted by frumious *
if counter++ > 500 then 'arbitrary time value
p1_x = 254
p1_x = 0

Yeah, that will work as long as you ditch the ++ part. Doing a little C recently? :wink:

Anyway, dead-reckoning with the 2.5 syntax begs you to use the SELECT:

counter var word

serin ....

IF auton_mode = 1 then
  SELECT counter
    CASE 0 to 100
    CASE 101 to 200
    CASE else
   'regular code goes here


This code will go forward for approx. 2.6 seconds, then turn for another 2.6. To add new steps, just add another CASE. After it’s done, it just stops and no longer incremements the counter. If you put the counter incrementer outside the SELECT, it will loop back to 0 after approx. 28 minutes. This may be OK, it may not.

erp, yeah, i didn’t notice i put the ++ in there :slight_smile: way too much c/c++ for me (: