Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Camera for turning (http://www.chiefdelphi.com/forums/showthread.php?t=52769)

DustinB_3 25-01-2007 20:41

Camera for turning
 
I am using a combination of the camera and a gyro to turn until the camera is lined up with the green light. With the code I'm using the robot just rotates back and forth and will never stop. Is there a better way to accomplish this. Here is the code:

Camera_Handler();
Servo_Track();

temp_gyro = Get_Gyro_Angle();
tracking_state = Get_Tracking_State();

printf("gyro_angle = %d\r\n", (int)temp_gyro);

if(capture == 0 && tracking_state == CAMERA_ON_TARGET )
{
goal_angle = ((((int)PAN_SERVO - 124) * 65)/124);
capture = 1;
}

milliradian_goal = ((goal_angle * 3.1415 * 1000)/180);

if(capture == 1)
{
if((milliradian_goal - 60) < temp_gyro)
{
pwm03 = pwm06 = 150; //Turns robot left
pwm04 = pwm05 = 150;
}
if(temp_gyro < milliradian_goal + 60)
{
pwm03 = pwm06 = 104; //Turns robot right
pwm04 = pwm05 = 104;
}
if(((milliradian_goal + 60) >= temp_gyro) && (temp_gyro >= (milliradian_goal - 60)))
{
pwm03 = pwm06 = 127;
pwm04 = pwm05 = 127;
capture = 2;
}
}

DanDon 25-01-2007 20:46

Re: Camera for turning
 
Quote:

Originally Posted by DustinB_3 (Post 565292)
I am using a combination of the camera and a gyro to turn until the camera is lined up with the green light. With the code I'm using the robot just rotates back and forth and will never stop. Is there a better way to accomplish this. Here is the code:

Camera_Handler();
Servo_Track();

temp_gyro = Get_Gyro_Angle();
tracking_state = Get_Tracking_State();

printf(&quot;gyro_angle = %d\r\n&quot;, (int)temp_gyro);

if(capture == 0 && tracking_state == CAMERA_ON_TARGET )
{
goal_angle = ((((int)PAN_SERVO - 124) * 65)/124);
capture = 1;
}

milliradian_goal = ((goal_angle * 3.1415 * 1000)/180);

if(capture == 1)
{
if((milliradian_goal - 60) < temp_gyro)
{
pwm03 = pwm06 = 150; //Turns robot left
pwm04 = pwm05 = 150;
}
if(temp_gyro < milliradian_goal + 60)
{
pwm03 = pwm06 = 104; //Turns robot right
pwm04 = pwm05 = 104;
}
if(((milliradian_goal + 60) >= temp_gyro) && (temp_gyro >= (milliradian_goal - 60)))
{
pwm03 = pwm06 = 127;
pwm04 = pwm05 = 127;
capture = 2;
}
}

Why are you setting all four PWM outs to the same value? If you want to turn left, set your right motors forward and your left motors either stopped or reversed and opposite for turning right.

DustinB_3 25-01-2007 20:53

Re: Camera for turning
 
The motors on the left side have to go in reverse in order to move the robot forward. So by using the same number the left side wheels and the right side wheels go opposite directions.

DanDon 25-01-2007 20:57

Re: Camera for turning
 
OK, we usually wire the motors up with the left motor polarities being opposite the right motor polarites so that 254==full forawrd on both the left and the right sides. Sorry bout that. In this case I would suggest using a proportional control loop where the output applied to the motors scales itself to the amount of error, lestening the oscillations.

DustinB_3 25-01-2007 21:33

Re: Camera for turning
 
I have messed around with control loops before build season but never could get one to work correctly. Could you post example?

Alan Anderson 25-01-2007 21:52

Re: Camera for turning
 
Making the motor speed proportional to the error will stop the oscillations if the proportional constant is low enough. But then you'll probably find the robot won't quite get to the desired angle, and you'll want to add some error integration to force it to move after it's been off-angle long enough. At that point, you might have some overshoot, and to fix that requires some negative feedback based on the rate of change of the error.

In short, it's time to implement a PID control system for your robot direction. Search the forums to find plenty of information, and even some code, that will do what you need.

DustinB_3 25-01-2007 22:18

Re: Camera for turning
 
I have actually searched all over chiefdelphi for information. I found a lot except that I couldn't find anything about an output to control more than one pwm.

Uberbots 25-01-2007 22:24

Re: Camera for turning
 
i actually have a bit of code that may help you.

PHP Code:

unsigned char PIkDControl(char Kpchar Kichar Kdint error) {
    static 
int errorTotal 0;  //there is a better way to do this... meh
    
static int prevError 0;
    
int P 0;
    
int I 0;
    
int D 0;
    
    
= (error*Kp)/100;
    
= (errorTotal*Ki)/100;
    
= ((error prevError)*Kd)/100;

    
errorTotal += error;
    
prevError error;

    return  (
Limit_Mix(2000+127+P+I-D));


There are a couple of other tweaks, but i edited them out. you should come across them whilst testing (=

DustinB_3 25-01-2007 22:26

Re: Camera for turning
 
Thanks, I'll try that. One question: What's the limit mix for. I have never seen it on a control loop.

Uberbots 25-01-2007 22:47

Re: Camera for turning
 
limiting output, of course. you don't want your unsigned char to overflow, that would be bad

Alan Anderson 25-01-2007 22:47

Re: Camera for turning
 
Quote:

Originally Posted by DustinB_3 (Post 565403)
...I couldn't find anything about an output to control more than one pwm.

A brief look at your existing code reveals that to be a non-issue. If you supply the same PID-computed value to all your motor pwms, your robot will turn in place. That's exactly what you want it to do, isn't it?

DustinB_3 25-01-2007 22:48

Re: Camera for turning
 
While using your code how do i implement a target value and how do I out put it to four motors. Sorry for all the questions but PID loops are still very new to me.

Uberbots 25-01-2007 23:46

Re: Camera for turning
 
the function is returning a motor value from 0-255. it is up to you to map it to your motors, like you would a joystick.

as for inputting the error... you need to figure out your error much like you did in your original loop. with the dual targets this year, though, you will have to do a little (lot) of extra math and AI to get your desired target.

Tom Bottiglieri 26-01-2007 00:18

Re: Camera for turning
 
Quote:

Originally Posted by DustinB_3 (Post 565445)
Sorry for all the questions but PID loops are still very new to me.

PID Without a Ph.D

All you had to do was ask...:cool:

DustinB_3 26-01-2007 20:44

Re: Camera for turning
 
Thanks a lot for all of your help. I finally got a PID loop running and it works great.


All times are GMT -5. The time now is 03:56.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi