Hi! We got our cam working and it detects different colours too. Problem is we have to put the colour in front of the cam in order for cam to recognize the colour. Our cam doesn’t spin on servos looking for the object we have to spoon feed it! Anyone knows why this is happening or any ideas? Please post thanks!
Hi! I really like your code and that was something that simmilar to what our team came up with. Infact our code wasn’t as successful as yours but anyways thanks for the code. I just want to mention that their is a problem with the code or our cam configuration because when i put your code into autonomous mode it shows Error in terminal window stating “*Error setting color tracking parameters Expected 8, and got 0” You know what is the reason behind it maybe? Me and my team truly appreciated your help thanks once again!
Has anyone actually successfully implemented a scanning routine? One problem I notice with the one above is that you have to use camera_stop() before setting the servos.
I have been playing with it the last two days, and the problem I am having is that once I get the camera pointing where I want it, then I have to set auto servos again and I have to call camera_find_color again, and one of the two seems to center the camera. At least all my attempts at a program like this have resulted in the cam exhibiting seizure like behavior. It seems like there has to be a solution to this, but so far it has eluded me quite successfully…
One problem with the above code is the servos controlled by the camera range from 41-210, with 128 being the middle. NOT the typical 0-254 w/ 127 as middle range
Thats easy enough to fix. Just change the numbers. But has anyone gotten this to work? I have just about given up on it. I think I am going to have to make the whole robot turn.
I’m not really familiar with the code that FIRST gave you, but basically what you want to do is:
send your TC command
wait for 1 T packet
if pixels = 0
{
-send a \r
-send the set servo command to the next counter
-repeat from beginning
}
else
{ //(it found a color)
-send a \r
-enable auto-servo mode
-send the TC command again
}
We’re not using servos and just rotating the whole robot. The only advantages I can see to the servos are using the tilt angle to calculate distance and pan and scanning before the match begins.
Hmm… I’m trying to get the servos to turn while tracking and I fail every time:
If I use camera_find_color and then set servos, it stops finding color.
If I use camera_stop(), camera_set_servo(), camera_find_color() then it resets to center because that’s what camera_find_color() does.
If I put all the color init code at the beginning, and then only run the TC command at the time, then stop it, it doesn’t recieve updates:
///// For some reason camera_track_update() returns 0 unless I call reset before I send the TC command.
static int counter=0;
if (camera_track_update()) {
// updated
} else {
// no update
}
counter++;
if (counter>15) {
counter=0;
camera_stop(); // in user_camera
camera_set_pan_servo(...); // sends "SV 0 xxx"
// camera_reset(); // If I call this then it works, but keeps jumping to the center servo position.
camera_track_continue(); // function that just sends "TC ... ... ..."
}
I wrote this from memory since I don’t have access to the other computer, but it is the general idea (check for updates, stop, turn, track).
I am sure that the individual camera_ functions work as they work when not setting the servo, and the servos move but don’t track. But the combination causes it ot not reply with a “T … …” packet.
Also, I read something about poll mode (“PM 1”) I tried this and it still did not recieve packets. Have any of you had this method work?
Maybe I have to call the TC command twice. (I remember having to click the button twice in the java program to retart the tracking…?
I could also try using HyperTerminal and pasting in all the stuff that it would have sent from the RC.
Do you have any ideas about what works for you?
If I can’t figure it out, it won’t hurt the time very much to actually turn the entire robot instead (pwm01=80;pwm02=-80;;))
You have to stop the auto-track to manually set the servo position using the default camera code. My temporary solution (till I find something better) is to not PAN smoothly… and to be very patient.
Don’t pan the servo, but set three distinct positions: Left, Center, and Right. Make them spread out enough so that you can cover the entire width of the field with these positions.
Start at Center, turn on auto-track, wait for a few track packets.
If you don’t get successfully tracking, set your servo to position left, and wait for a few track packets again.
If you don’t get successfully tracking, set your servo to position right, and wait for a few track packets again.
The goal is to NOT move the servo manually. When you do this, you have to turn of auto-track, and it takes a little while for the camera to turn it back on again, and to get a track on the object. If you’re constantly setting the servo position manually every cycle, you will never give the camera enough time to get a track.
There is code to toggle the camera’s position between LEFT, CENTER, and RIGHT, but it doesn’t automatically turn auto-tracking/servo back on immediately after. To turn it back on, you must re-select the colour you wish to track, wish just resets the servo position back to center anyways… so really it doesn’t work very well (read: at all). The code is linked to the top switch on joystick 2 I think? I can’t recall exactly, sorry! We’ve been working on our encoders and have been tuning our PID loops as of late, and haven’t gotten back to the camera just yet =(.
Hmmmm I should try playing with encoders and gyros some time… We dont have either now, but I am going to convert our practice bot into an awesome test platform once the season is over… Assuming my team will let me. Anyway thanks. We got kicked out early today because it snowed like six inches, but I will try it tomorrow.
I had this same problem. Rather then spend a long time fixing it, I just control the servos right off of the RC.
It’s working fine, and is legal (I had thought it wasn’t at one point, but there is a question in the Q&A thing that says otherwise, ID 1480)
Here’s the code that we are using… it should be easily changed to other configurations
//Pan and tilt servos
#define cam_pan pwm01
#define cam_tilt pwm02
//Internal posistion variables
unsigned char cam_p=128;
unsigned char cam_t=128;
if (cam.x == 0 && cam.y == 0) {
//cam_pan and cam_tilt control the servo's on the mount
//cam_p and cam_t are our local variables
cam_pan = cam_p;
cam_tilt = cam_t;
//Tell the camera where the servos are,
//so we get useful values when it finds a target
camera_set_servos(cam_pan,cam_tilt);
//What color do we want to track?
camera_find_color(GREEN);
//This does all the panning
cam_p += 10; //X pan
if (cam_p > 210) { //if we hit the x limit
cam_p = 46; //start over
cam_t += 10; //but change the tilt
}
//if we've tilted too much
if (cam_t > 117) cam_t = 97;
}
else {
//sometimes we get nonsense packets, this fixes them
if (cam_pan > 0 && cam_tilt > 0) {
//update the servo and our var with new servo values
cam_p = cam_pan = cam.pan_servo;
//update the tilt servo with new tilt value
cam_tilt = cam.tilt_servo;
}
}
The trick here is we fool the servo into thinking the servos are connected to it. We do that with the camera_set_servo command. After it finds a target, we update the servos with where the camera thinks it is. The only thing I wanted to do with this code is get rid of the internal variables that hold the servo posistions.
is their a way we can run two procedures at a time if we can than why not turn the movement of a servo into one procedure and the colour tracking which is already a procedure and than run them at the same time. the only thing i don’t know about if procedure stays at a monitor level and so if we try to place a procedure inside a procedure it might show an error. Can someon please try this!