Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Technical Discussion (http://www.chiefdelphi.com/forums/forumdisplay.php?f=22)
-   -   Rookie help (http://www.chiefdelphi.com/forums/showthread.php?t=31880)

Mark McLeod 22-12-2004 16:16

Re: Rookie help
 
Quote:

Originally Posted by doyler
Yeah like that, but i didnt put in a counter because I couldn't figure out how to time it

Then it's trying to change way too fast for the servo to have time to react and make the move. Slow it down with counter like we did before, e.g.,
Code:

if (counter < 59) // About a second
        counter++;
else
{
        counter = 0;
        if (servo1 == 0)
                servo1 = 255;
        else
                servo1 = 0;
}

P.S. A typical time for a servo to make a large movement is only ~.2 sec, but the (EDU) controller slow loop will try to change it every .017 seconds. The code will completely change it's mind about where it wants the servo to be 12 times in the amount of time the servo needs to complete even one movement.

doyler 22-12-2004 16:28

Re: Rookie help
 
Ok, that is how i had to do it
Thanks, and it worked

[edit]
I almost fully understand how everything works, but what is that sensor for?
[/edit]

Mark McLeod 22-12-2004 16:52

Re: Rookie help
 
Quote:

Originally Posted by doyler
I almost fully understand how everything works, but what is that sensor for?

The gyro can be quite a useful sensor to drive straight, make precise turns, detect tilt, identify ramps or steps when you climb them, and on and on...

More has been posted on gyro's than I care to imagine.
Do an Advanced Search on gyro in just the "Programming forum" and start reading through the 70 or so threads you'll get.

doyler 22-12-2004 16:54

Re: Rookie help
 
No, I mean I know what they are for, but in my setup i mean

Mark McLeod 22-12-2004 17:05

Re: Rookie help
 
Quote:

Originally Posted by doyler
No, I mean I know what they are for, but in my setup i mean

The servos you mean? Sorry, I got confused because I think of them as motors rather than sensors.

The example you're using could wave a flag I suppose...
On the EDU robot a servo could be used in this way to move an arm to a precise position to scoop something up then another position to carry that something around.

On the full size robot it can be used to change gears in a drive train from low to high or vice versa.
In 2004, many robots used a servo to detect the direction to the IR beacons.

doyler 04-01-2005 08:22

Re: Rookie help
 
I was changing values and learning how it worked, but I got confused on one of my changes and how it worked

This is the code for 1 of my servos, but why does it look like it moves regularly from 255 towards zero and then it goes back to 255, back to 0, and then back to 255 to count down again

Code:

if (counter2 < 59)
counter2+=58;
else
{
counter2 = 0;
if (servo2 < 254)
        servo2+=253;
else
        servo2 = 0;
}
pwm08 = servo2;


Mark McLeod 04-01-2005 10:50

Re: Rookie help
 
Quote:

Originally Posted by doyler
This is the code for 1 of my servos, but why does it look like it moves regularly from 255 towards zero and then it goes back to 255, back to 0, and then back to 255 to count down again

Code:

if (counter2 < 59)
counter2+=58; // Normally you'd just increase by 1 here
else
{
counter2 = 0;
if (servo2 < 254)
        servo2+=253; // This should never result in a # > 255
else
        servo2 = 0;
}
pwm08 = servo2;


Normally you'd just increase the counter by one every time.
What you're doing here results in:
counter2=0
counter2=58
counter2= 116
counter2=0
etc.
This pretty much means that servo2 will get changed every 4 times through this loop ~1/15 sec. That's right on the edge of giving the servo enough time to complete a full movement. If you want to change the timing just change the "59" to something else.

servo2 produces stranger results:
servo2=0
servo2=253
servo2=506 //but it wraps around at any value over 255 so it ends up ~251
servo2=504 // ditto

doyler 05-01-2005 19:50

Re: Rookie help
 
Ok, that is what I thought
I have the one servo running through the code to go 1 degree a second and the other one doing the start and reset loop

What else what I program for other things(other then user_routines)

I have nothing specific because I am still getting my second job, but I know I'll need help :)

Mark McLeod 05-01-2005 23:06

Re: Rookie help
 
Quote:

Originally Posted by doyler
What else what I program for other things(other then user_routines)

I have nothing specific because I am still getting my second job, but I know I'll need help :)

(I just timed out on a longer message and lost it. Here's an abreviated response.)

You can play with reading toggle switches attached to the RC to control which type of servo movement to do. You’ll need external switches later on to be able to select between two different autonomous programs for example, or to tell your program which side of the playing field your robot is starting on.

What you actually have is your first autonomous program. The next thing is to make it perform multiple sequential steps, such as
-moving slowly for 5 seconds,
-then doing your “start and reset” for 5 seconds,
-then do a third action for 5 seconds.

One way to do this is to use another “counter,” but do different things as it increases in value (as time goes by), like this:
Code:

static int counterX=0; // Use int for larger numbers
if (counterX < 295) // Five seconds (59 x 5)
  counterX++;
// Do something
else if (counterX < 590) // Five more seconds (10 sec in all)
  counterX++;
// Do something different
else if (counterX < 885) // Five more seconds (15 sec in all)
  counterX++;
// Now for something completely different

A more traditional way to get a similar result is to use a technique called a State Machine. You can probably do a search on Chiefdelphi or Google to turn up comments on State Machines, but the idea is to define a variable to remember your current “state” or what you want to be doing. A simple two state State Machine might look like:
Code:

static char servoState=0;
if (counter < 30) // Only do something every half second
counter++;
else
{
counter = 0;
if (servoState == 0) // State 0 is to move the servo slowly through all 255 positions
{
if (servo1 < 255)
        servo1++;
else
        {
        servoState++; // State 0 has been completed. Go do the next action.
        servo1 = 0;
}
else if (servoState == 1) // State 1 is to move the servo from max to min repeatedly
{
        if (servo1 == 0)
        servo1 = 255;
        else
                servo1 = 0;
}
}


doyler 10-01-2005 21:17

Re: Rookie help
 
so for the states, how would I make each one run 5 seconds later?

Mark McLeod 11-01-2005 08:59

Re: Rookie help
 
Quote:

Originally Posted by doyler
so for the states, how would I make each one run 5 seconds later?

You could use a second counter and replace

ServoState++; //or better “ServoState = 1;”

with something like:
Code:

if (ServoCounter > 295)
{
        ServoState = 1;
        ServoCounter = 0;
}


Eventually you should move to a clock based scheme instead of the more primitive use of counters to time your loops. The counters don’t transition easily from the Robovation controller to the FRC, since the loop times are different, and it isn’t as difficult to keep track of many different counters.
Using one of the built-in timers you could maintain a “clock” variable that just kept the current time. Then the logic might look something like:
Code:

#define STATE1_DURATION 5 //in seconds
static int ServoTimeout;

ServoTimeout = clock + STATE1_DURATION; // set when starting state 1

if (clock > ServoTimeout)
{
        ServoState = 1;
}

You can find a white paper on setting up timers on the IFI website.


All times are GMT -5. The time now is 02:29.

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