Log in

View Full Version : Older FRC autonomous code


johncap100
28-04-2010, 17:02
I have the 2007 FRC bot (along with the 2006, 2005). I am programming using mplab 7.2. I am trying to install an autononmous code. I just want a short code that moves the bot forward a short distance , turns and returns to the original location.
Does anyone have a default workspace that has the autonomous code imbedded, who might be willing to share?
thanks john

Mark McLeod
28-04-2010, 18:00
Do you mean the original default code (http://www.ifirobotics.com/docs/frc-code-2007-8722.zip)?
Or Kevin Watson's default variation (http://kevin.org/frc/ifi_frc_simple.zip)?

Or did you want direction on how to implement autonomous code?

Or were you looking for someone to drop your simple autonomous movements into the default to use as a starting example?

billbo911
28-04-2010, 18:19
I have the 2007 FRC bot (along with the 2006, 2005). I am programming using mplab 7.2. I am trying to install an autononmous code. I just want a short code that moves the bot forward a short distance , turns and returns to the original location.
Does anyone have a default workspace that has the autonomous code imbedded, who might be willing to share?
thanks john

I can't offer code from 2007, but I do have our 2008 code. The Autonomous code drives the robot the length of the field, makes a u-turn and drives back toward the starting line. There is a bit more to it than that, but that basic part is there. By the way, it is built on Kevin Watson's framework instead of IFI's. Honestly, Kevin's is way better! PM me and I'll zip it and send it to you.

efoote868
28-04-2010, 19:12
Do you want a smart or dumb autonomous (sensors or none?)

If you don't want any sensors, then this should be a pretty straight forward task. If you do want sensors, it'll be a little more complicated.


This is where you should start:
http://kevin.org/frc/

johncap100
28-04-2010, 22:22
Hello to Mark

well i have the kevin watson version i think.

and yes i wuold like to know how to drop in the autononmous code and
also create if you cuodl help.
basically on all three.

i do have the default code working so i have the teleop working both tank ans arcade., i would just like to do the autononmous too.
thanks john

johncap100
28-04-2010, 22:23
and billbo911 yes if you could zip your code i would like that too if you could
thanks a bunch.
john

Chris_Elston
28-04-2010, 22:37
We try and archive all our code here:

http://www.frcsoft.com/forums/index.php?autocom=downloads

Mark McLeod
29-04-2010, 13:59
In the Watson version you drop your autonomous code into the file "autonomous.c", specifically into the routine "Autonomous()".

This routine gets executed ~38 times per second in sync with the driver station control transmission packets.

You write your autonomous code in such a way that it does a little bit and goes away. When it gets called again it does a little bit more and goes away again.

Two ways to dead reckon autonomous movements are:

to keep track of and count the number of times you're Autonomous() has been called - 38 times = 1 second.
use a system timer and check each time to see if you've driven long enough.If your code already has a timer setup, then you can experiment with using that. It's the most accurate method.

Counting loops is accurate enough for what you're doing at first, and it's trivial to implement and understand right away, so you can start with that.

On top of these methods of keeping track of time you might want a state machine to keep track of the steps you should be doing.

Here's an example:

// Drive forward, turn, return, and stop

void Autonomous()
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing

switch (autostate)
{
case 1: // Drive forward
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}

case 2: // Turnaround
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}

case 3: // Drive forward (returning now)
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}

case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs

pwm01 = pwm02 = pwm03 = pwm04 = 127; // Make sure the last thing you do is always stop
}
counter++;
}


Our team code from 2007 is here (http://www.team358.org/history/2007/code/T358_2007_v3.ZIP), but it's fairly complex as it implements a scripting system for autonomous, so I definitely wouldn't start learning at that level. Our robot was somewhat complex that year too.

johncap100
29-04-2010, 17:43
so after i open up the default workspace then i can open the autonomous.c and edit it there?

i think in the default code there is a place to drop in code but i forget which file i need to open up once the workspace is opened.

i would like to start with the dumb code but actually it would be nice to do some 'smart" code :)
thanks john

Mark McLeod
29-04-2010, 19:03
so after i open up the default workspace then i can open the autonomous.c and edit it there?

Yep.
Do you need instruction in using MPLAB and downloading code using IFI_Loader or do you already know about that or can figure it out?

johncap100
30-04-2010, 10:47
no i can open the mplab and can upload from the ifi loader
thanks

johncap100
30-04-2010, 12:06
mark i tried the following code from one of my students and it does not seem to work, i will try your code above.

any help is greatly appreciated.

by the way i am opening up the user-routine_fast.c to modify the autonomous code is that correct?
thanks


// Drive forward, turn, return, and stop

void Autonomous()
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing

switch (autostate)
{
case 1: // Drive forward
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}

case 2: // Turnaround
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}

case 3: // Drive forward (returning now)
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}

case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs

pwm01 = pwm02 = pwm03 = pwm04 = 127; // Make sure the last thing you do is always stop
}
counter++;
}

efoote868
30-04-2010, 12:55
Did that compile OK?
When you loaded it and ran autonomous, I (hope) the robot just stayed still. Luckily your last state was no movement, because without break statements, it'll run through all the states.

Should be:

switch(variable)
{
case 0:
//do stuff
break;

case 1:
//do other stuff
break;

case 2:
//do some stuff, and case 3's stuff

case 3:
//do something else
break;

default:
//do nothing
break;
}

If you take the default case out in your current setup, you should drive forward forever (which is dangerous, so I suggest you don't).
I also suggest that you disconnect the motors and just watch lights to see if its doing what you want.

Mark McLeod
30-04-2010, 15:01
Those are the break;'s
The break statement concludes individual cases. Without it one case statement blends right into the next without a "break".
That is one of the reasons for stopping all motors at the end.

I unfortunately duplicated one block and missed copying the original break statement as part of the block of code.
Sorry about that...

Corrected version:

// Drive forward, turn, return, and stop

void Autonomous()
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing

switch (autostate)
{
case 1: // Drive forward
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}
break;

case 2: // Turnaround
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}
break;


case 3: // Drive forward (returning now)
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}
break;


case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs

pwm01 = pwm02 = pwm03 = pwm04 = 127; // Make sure the last thing you do is always stop
}
counter++;
}

Mark McLeod
30-04-2010, 15:28
by the way i am opening up the user-routine_fast.c to modify the autonomous code is that correct?
That means you're using the IFI default code rather than the Watson default code.

You'll need slightly different handling for that just because the framework structure is different. In user_routines_fast.c in the Autonomous() routine already there you'll want to keep some of the default code it uses. Below the lines in black are what you should already see in that default routine, and the lines in red are what need to be added. Really the critical part that's missing is the Getdata that tells the robot when autonomous mode is over and Putdata that sends your motor settings out to be used. Without that nothing would ever happen.


void User_Autonomous_Code(void)
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing

/* Initialize all PWMs and Relays when entering Autonomous mode, or else it
will be stuck with the last values mapped from the joysticks. Remember,
even when Disabled it is reading inputs from the Operator Interface.
*/
pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;
relay1_fwd = relay1_rev = relay2_fwd = relay2_rev = 0;
relay3_fwd = relay3_rev = relay4_fwd = relay4_rev = 0;
relay5_fwd = relay5_rev = relay6_fwd = relay6_rev = 0;
relay7_fwd = relay7_rev = relay8_fwd = relay8_rev = 0;

while (autonomous_mode) /* DO NOT CHANGE! */
{
if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{
Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */

// Drive forward, turn, return, and stop

switch (autostate)
{
case 1: // Drive forward
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}
break;

case 2: // Turnaround
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}
break;


case 3: // Drive forward (returning now)
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}
break;


case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs

pwm01 = pwm02 = pwm03 = pwm04 = 127; // Make sure the last thing you do is always stop
}
counter++;

Generate_Pwms(pwm13,pwm14,pwm15,pwm16);

Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */
}
}
}

johncap100
30-04-2010, 21:38
Well Mark i typed in the code above in red, and yes the black code is there.
It built fine, and i downloaded it , but the autonomous did not work, all that is working is the teleop, one joystick operates one motor and the other joystick the other one.
i have the motor controllers plugged into pwm ports 1 and 2. There seems that there should be someway to let the program that this is autonomous and not teleop. is it set up to go to autononmous if there is code there?
how many times will it go through the autononmous code?
thanks

Mark McLeod
30-04-2010, 22:11
You do have to put the robot into Autonomous mode before it'll take effect.

Normally, we use a special switch (http://www.team358.org/files/programming/ControlSystem2004-2008/competition-port-pinout-guide-reva.pdf) constructed for the purpose. (You can ignore switch 3 on that schematic. It's for earlier versions of the IFI control system.)
The disable button is handy to have in any case for emergency stops.

You can also setup an auto routine to run while you push a joystick button.

If you only want to run autonomous and don't care about Teleop, there is a way to set the team number most-significant-bit so the robot runs Auto mode all the time.


P.S.
Since you're using PWM 1 and 2 only, the auto code needs to be adjusted to reflect this, rather than pwm01/pwm02 and pwm03/pwm04
E.g.,
pwm01 = 200;
pwm02 = 54;

johncap100
01-05-2010, 08:09
Where does the switch plug in? on the robot controller?

How would one go about adding the routine into the code?

if you would be so kind as to helpme on these two things

thanks

apalrd
01-05-2010, 09:14
The switch plugs into the "competition" port on the Operator Interface. You only need sw1 and sw2, Disabled and Autonomous. If you would rather use a joystick button to indicate autonomous mode, you could say something like "autonomous_mode = pw_trig;" right after Getdata, for both Process_Data_From_Master_uP and User_Autonomous_Code.

Mark McLeod
01-05-2010, 16:03
Yes, it connects to the Operator Interface at the driver controls.
I've attached a photo of a Disable/Auto switch on our '08 robot controls.
It happens to be out in my van for an appearance at the Special Olympics tomorrow.

If you want to use one of the joystick buttons to trigger it as well, the Autonomous routine can be rewritten slightly to accommodate being called two different ways. I'd prefer not to flat-out clobber the autonomous mode flag, but to structure the code such that either way will trigger the auto routine.
Had one team fail year after year on the playing field whenever a particular mentor would return every-other year to help them. Always turned out he would overwrite the autonomous mode flag with a joystick switch so the real flag would never work. It's a bad habit to start.

Maybe something along the lines of (in User_Autonomous_Code and main.c):

while (autonomous_mode || (p1_sw_trig == 1) ) /* DO NOT CHANGE! */
You'd also need to modify main.c with the same statement modification to get it to start and the joystick button would act as a dead-man switch. You'd have to hold it to keep autonomous running. Letting go would stop it.

What might be simpler for you right now is to just call an auto routine like that in post #14 directly from user_routines.c whenever the button is held down. a la,

void Process_Data_From_Master_uP(void)
{
static unsigned char i;

Getdata(&rxdata); /* Get fresh data from the master microprocessor. */

if (p1_sw_trig == 1)
{
Autonomous();
}
else
Default_Routine(); /* Optional. See below. */

You don't have to be in Autonomous Mode to run some autonomous routine in teleop.

P.S. If you add a new routine like Autonomous(), depending on where you add it you may need to declare it in one of the header files (.h) so the compiler knows where to find it.

johncap100
01-05-2010, 17:32
Ok great with all the info, sounds like it is best to have a dongle mostly as a kill switch, and then do the simple code in autonomous_user.c using a button trigger. i will try it this afternoon
thanks a bunch and will post how it goes

johncap100
02-05-2010, 13:08
well i went to the users_routines.c and right after the"Getdata (&rxdata);" i typed in the code from post 20 (the second code). When i ran the build all i get back the following 2 errors:
pl_sw_trig has not been defined
and
"call of function without prototype" referring to the Autonomous (); line.

any help is appreciated.

also just a quick check, i am doing this i the users_routines.c and not in the users_routines_fast.c. I understand that the latter one is for autonomous code editing only.

apalrd
02-05-2010, 13:24
Where is your Autonomous() function? It (or a prototype to it) should be before the routine that gets the error, Process_Data_From_Master_uP(). If the routine is in a separate file, then the prototype needs to go in a header. If Autonomous() is in the same file, then put it before Process_Data_From_Master_uP().

If you build the box, then you don't need the joystick trigger. One or the other. If you don't build the box, then it is easier to just use a joystick trigger.

p1_sw_trig should be valid..... it is.

http://ifirobotics.com/docs/legacy/2004-programming-reference-guide-12-apr-2004.pdf has a list of the IO definitions, you can find everything there. If you look at ifi_aliases.h it will tell you what the definition for everything is. If you created a C file for your code, you have to include ifi_aliases.h to use... anything (OI data, PWM outs, Analog, Digital inputs).

Kevin Watson
02-05-2010, 13:58
...any help is appreciated.If you get stuck, feel free to send me your code and I'll have a look. Use kevin at kevin period org if you send me anything.

-Kevin

Mark McLeod
02-05-2010, 15:21
pl_sw_trig has not been defined

"p1_" not "pl_"
I think you might not have used the numeral one.

It stands for port 1 on the Operator Interface. You can also use a joystick connected to any of the four ports available and refer to the buttons by the prefixes "p1_" "p2_" "p3_" or "p4_"


"call of function without prototype" referring to the Autonomous (); line.

The compiler needs to be told a little bit about how the Autonomous() function is supposed to be called.
Up at the top of user_routines.c after the "#include" lines add the statement:

void Autonomous(void);

This'll tell the compiler that the routine doesn't have any calling arguments.

Doing this in users_routines.c is fine.

johncap100
02-05-2010, 21:05
Hey mark well i did cure the p1 issue, thanks

But when i try to build all i get an error as follows:
'could not find definition of symbol "Autonomous" in file 'C:\FRC Miller-2006 Codeopt b\user_routines.o' the file is where i have the workspace.

I suspect i need to add a line in an .h extension according to the bottom of your post #20, not sure what or where to put it.
thanks

apalrd
02-05-2010, 21:09
If Autonomous() is in the same file (user_routines.c) then you do not need the .h.
Put the following after the #includes at the top of user_routines.c:
void Autonomous(void);

Mark McLeod
02-05-2010, 22:08
But when i try to build all i get an error as follows:
'could not find definition of symbol "Autonomous" in file 'C:\FRC Miller-2006 Codeopt b\user_routines.o' the file is where i have the workspace.

I suspect i need to add a line in an .h extension according to the bottom of your post #20, not sure what or where to put it.
thanks
I suspect this is caused by something different.

I'd guess you have a new file containing the Autonomous routine?
It's in the directory of your workspace?
If none of this guesswork is true, then describe more about which files your new code is in.

If that's how you did it, that's all okay.
There is one more step though. Any new file has to be added to the workspace in MPLAB, otherwise, it doesn't get pulled in by the compiler.

In MPLAB go to Project -> Add Files to Project...
and find your new files in include as part of the compile workspace.

johncap100
02-05-2010, 22:29
sorry but i am not following the reasoning of what you said in the last post. I can go to the add files in mplab but when there what do i add?
you seem to be asking about a new file? not sure what you are saying.
thanks

Mark McLeod
03-05-2010, 10:44
Sorry, I just guessed wrong about how you've put your code together looking over your virtual shoulder...:)
The error you got meant the compiler couldn't find your Autonomous routine-"void Autonomous(void) {".
It'll be something simple, like my leaving out the keyword "void".

There are lots of different ways to do this, so here's a template for one way.
I assume you are doing all your work in the file user_routines.c
This is only one way to integrate the code from posts 20 and 14.


/************************************************** *****************************
* FILE NAME: user_routines.c <FRC VERSION>
*
* DESCRIPTION:
* This file contains the default mappings of inputs
* (like switches, joysticks, and buttons) to outputs on the RC.
*
* USAGE:
* You can either modify this file to fit your needs, or remove it from your
* project and replace it with a modified copy.
*
************************************************** *****************************/
#include <stdio.h>
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "user_routines.h"
#include "user_Serialdrv.h"

extern unsigned char aBreakerWasTripped;

// Drive forward, turn, return, and stop

void Autonomous(void)
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing

switch (autostate)
{
case 1: // Drive forward
pwm01 = 200;
pwm02 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}
break;

case 2: // Turnaround
pwm01 = 200;
pwm02 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}
break;


case 3: // Drive forward (returning now)
pwm01 = 200;
pwm02 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}
break;


case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs

pwm01 = pwm02 = 127; // Make sure the last thing you do is always stop
}
counter++;
}

/************************************************** *****************************
* FUNCTION NAME: Limit_Switch_Max
* PURPOSE: Sets a PWM value...

... (etc)

void Process_Data_From_Master_uP(void)
{
static unsigned char i;

Getdata(&rxdata); /* Get fresh data from the master microprocessor. */

if (p1_sw_trig == 1)
{
Autonomous();
}
else
Default_Routine(); /* Optional. See below. */

... (etc.)


You can also put the Autonomous routine way down at the bottom of the user_routines.c file, and then you'd need that prototype statement up at the top to warn and prepare the compiler about what any calls must look like (if the call comes before the compiler has seen the routine) .

johncap100
03-05-2010, 16:05
got it working thanks

Mark McLeod
03-05-2010, 16:34
Great!

Were these robots from Team 1695?

johncap100
03-05-2010, 21:28
yes kind of, i have just made up a "test" bot with a couple of drive motors and the ifi controller. We competed for three years and then because of $$ had to quit for a couple of years, we wrote a couple of grants and competed last year, and will this year again. I am a teacher at the local high school and i have an increasing interest from all grade levels in bots, so thought i would get this one running and use it to demo autonomous, teleop, camera feeback , sensor feedback etc. as one person said earlier smart and dumb autononmoue control.

my next goal is to get camera and ir sensor feedback controlling the autonomous motion.

i will probably have more questions, maybe i will start another thread?
but thanks for your help and hope you can give me some more guidance.
thanks