Autonomous With Camera

So, weve gotten tracking working just like everyone else here. I am trying to figure out how to move what we have in the normal mode to autonomous. Below are things I have to consider:

  1. Setting which color we should use for tracking
  2. Which functions must be called on in order for tracking to work properly

Has anyone gotten this to work properly? It seems to me that If i copy all the stuff in Default_Routine() function to the Default_Routine_Fast() function it should work. This means the following code:

camera_find_color(color) // Tells is what color to start tracking


if (p1_sw_trig > 0 && tracking > 0) { //If vision active and tracking use camera data
p1_y = speed_control; //set forward speed
p1_x = pan_position; //set turning rate
p1_x = 255 - p1_x; //invert turn direction
// Steering Compensation
if (p1_x > 135 && p1_x < 225 && steering_comp > 0)
p1_x = p1_x + steering_comp;
if (p1_x < 120 && p1_x > steering_comp && steering_comp > 0)
p1_x = p1_x - steering_comp;
pwm11 = Limit_Mix(2000 + ramp_y() + ramp_x() - 127); //uses our ramp
pwm12 = Limit_Mix(2000 + ramp_y() - ramp_x() + 127); //uses our ramp

Had anyone done this? I just wanted to share my thoughts.

–Team 486
–Positronic Panthers


I just had a thought. Since the code to pick your color isn’t in the Default_Routine() function, it is always running, even when the robot is disabled. This means that another way you can work setting up the camera is by mimicking the button controls. For instance:

I don’t think i’ll do this for changing the colors (it’ll start looking for each color in turn), but it might be useful for the other functions. An important thing to remember is that there is no need to change the output variable when you can change the input one!

I just realized:

One of the variables used by the code I copied earlier is external. This is the pan_position variable. Can I just define it as external and have it find it? The problem is that I can’t just copy the code that creates it because that code runs in the normal loop and I don’t want to screw up the timing. Suggestions?

I would love it if someone would write a white paper on this. It might seem obvious to some people, but to me (and probably a lot of others who are learning programming as they go) it is horribly confusing.
I have some ideas on how to do this, but the problem is that this is not one of those things I can do one step at a time, rather I have to do it all in one big step, then when it doesnt work I will have to try to find where in the mass of code the problem lies.

I think I will try this idea and maybe give you guys some feedback on wether it will work or not and if it does than i will post it in white paper so others can see. But only if i am Successful! :] :slight_smile: If you guys already started on this project than let me know this is my e-mail

Ok, I think what i’m going to do is just make the function within Default_Routine() a function outside of the loop and then call on it within it. Then I can run that code from user_routines_fast(). Color selection will be accomplished by mimicking joystick button presses.

Not the most elegant, but it might work.


–Team 486: Positronic Panthers

Depending on your autonomous strategy is what color you want. Possibly green if you want the vision tetra?

Here is what we have for tracking green autonomously

char look_for_green=1;


  while (autonomous_mode)   /* DO NOT CHANGE! */


I’ve taken a first cut at getting all of the little bits of camera code into one source file. If you’d like to check it out, here’s a link:


Does just executing that camera track green color make the robot go towards green?

No it isnt quite that simple. Here is generall what I did (I am writing this from memory here so you will have to forgive possible problems in it):
in user_routines_fast.c

int counter=0; //define a counter in the beginning

then down in the autonomous part.....


   if (cam.size>0)

    Ok now put some single joystick drive code here using the variable cam.pan_servo as the x axis of the joystick, and a constant value as the y axis

Something like that. It is crude but it is working for me.

Basically what it does is when the program starts the first time it runs it initializes the camera (oh yeah you are gonna want to change those values in there to the ones you got from calibrating), then it sets it up to use auto servo then it tells it to track green. Then it checks to see if there is new data from the camera. If there is then it checks to see if it is tracking one or more pixels and if so it sets the variable “tracking” equal to one, and if it is not it sets it equal to 0. Then if tracking is equal to one it executes your drive code, and if not then it stops your motors. It is important to stop your motors so that when you loose sight of a target your robot doesnt go nuts.

If you have any questions please ask away, I am quite proud of this relatively simple code and I would love to answer any questions. :smiley: Yeah, this is my first year programming…

What exactly is the cam.size variable?
Is your code waiting for the screen to be full of the color you’re tracking?
I think a little more info would help me out, but I would like to say thanks for all the info, I’m finally getting a tracking mode started for our bot! I’ll post it once it’s done…

The cam.size variable is the number of pixels the camera is tracking. I actually borrowed that idea from the default code. Let me edit that post and add an explanation.

Hi, I am having some trouble implementing the camera code into the autonomous as well… Should I be #include-ing anything other than user_camera.h? Also, with the camera hooked up correctly it doesn’t seem to be accessing the #if _USE_CMU_CAMERA sections of code in user_routines_fast, should I even be using that? I just need it to recognize the camera in autonomous!!! AHhhh!!

I just don’t think you should have to initialize the variables. These variables are global aren’t they? The only code you should have to change is that within the Default_Routine() loop. Everything else is run even when in autonomous mode.

ok, i understand how this code looks for one color, but what if we wanted it to look for two colors, like when we shift sides of the field? i am hoping it is not that time consuming, but if it is, that is ok.

I assume you mean you want to be able to track red, or blue depending on which team you are on? Hmmm. Is there some way your program can know which team you are on? If not then use a switch you can set for which team you are on, then when you are running those functions at the beginning have it say camera_find_color(thecolor); and earlier in the program have an if/then to make it set thecolor equal to red or blue (there are some numbers that represent the different colors… I think 4 and 5 respectively, but you might want to check that) based on the position of your switch. It is again, crude but it works.
If I were you though I would not use that code I posted above for your final program, I really only meant it as a model of what you can do, you will probably want to elaborate on it.

Nice job on the code. I noticed that you did not increment your counter though. Also, you said you wrote this off the top of your head, is it the same stuff that is running/tracking your robot now? Thanks,

Hmmmm, good point. It does increment the counter in the program I am running on the bot, and other than that the only differences in the functional part (I never comment my actual code :)) are that a) right now it tracks red, not green (for a demonstration tomorrow that could get us nine grand) and b) it doesnt say to put the drive code here, instead it uses a modified version of the default drive code. I realised today that I have not actually put the robot on the floor and had it drive with this code, but I have had it on blocks and it tracks nicely and the wheels definetly turn in the right directions. I will let you know after our demo tomorrow if it actually works :smiley: .

That demo sounds sweet, hope that went well for you. I have seen our robot a total of 2 times, so its a bit tough for me to experiment with the code, so I appreciate your help. I still dont understand exactly what is going on with the camera, but I think I know enough now to scrape an auto mode together. I had tested something similiar to your code, but the bot may have been tracking the wrong direction or something, it was acting weird. I dont like the way the default camera code sets turning/forward speed so I think I am just going to make my own code. Again, thanks, and good luck

Ah. Well, the demo thingy is over and the bot sort of worked. But like I said I wrote that code I posted from memory, and come to find out I would have been better off using that, as in my code for the bot I neglected to put an else to stop the motors. Ever seen what a robot can do to a vending machine? Actually it hit the side so nothing actually broke but it was still cool. As it was though the people were still very impressed and I think we will get the money, I was just very annoyed with myself. Anyway, yeah once I fixed that it worked just fine.

im sure that someone has asked this before. but what are the numerical values for the range of the servo on the camera? is it -127 to 127 or what? is it 0 to 254 like the robot? need some help with this please.