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)

doyler 20-12-2004 13:57

Rookie help
 
I am new to programming and extremely new to a lot of mechanical stuff in general, so my team gave me something to work on over the holidays, but I am very confused.
I will try and be as specific as possible to receive help

They gave me an edubot controller, a battery, a charger, 2 servos, a serial to usb converter, and something called a BEI GyroChip

I know what the battery, charger, and serial -> usb cable do/are for, but how would i hook up the rest of thie stuff to the control

Thanks

Mark McLeod 20-12-2004 14:52

Re: Rookie help
 
Quote:

Originally Posted by doyler
... 2 servos, ... and something called a BEI GyroChip

... how would i hook up the rest of the stuff to the control

The servos plug into any PWM available on your controller. Setting the value (0-254) of the PWM will turn the servo to a particular spot. The same spot everytime you use a particular value.

The gyro plugs into an analog input on the controller (one of the first inputs based on User_Initialization() in user_routines.c). If it doesn’t already have a connector attached you’ll have to make one out of a PWM cable. You get values from it doing something like “x = Get_Analog_Value(rc_ana_in01);“ The neutral position of the gyro will be close to 512. When the gyro is twisted slowly in one direction you’ll get numbers between 0 and 512 only while you are twisting it. Do the same in the other direction and you’ll get numbers between 512 and 1023. If you keep track of these numbers you can tell if your robot is driving straight or not and correct. It can also be used to make precise turns, to say 45 degrees if you calibrated it.
This particular gyro is no longer "street legal" to use on your competition robot, but it's good to learn with. This one will be overly sensitive to turns or sudden movements that are too quick, and will more easily lose track of the original robot heading.

Do a search on “gyro” for a lot of threads on the subject.

doyler 20-12-2004 15:14

Re: Rookie help
 
the gyro has a pwm, and my only job is to make the sensors move or something

sensors go to pwm out? if so, the black is on top right
the gyro goes to analog in with the black on botton?

if all this is correct thank you and i will probably have questions later about what all these do and what / how to code

Mark McLeod 20-12-2004 15:21

Re: Rookie help
 
Quote:

Originally Posted by doyler
sensors go to pwm out? if so, the black is on top right
the gyro goes to analog in with the black on botton?

if all this is correct thank you and i will probably have questions later about what all these do and what / how to code

Yes, black on the outside, or the pin furthest from the controller, of each connector.
The gyro is a sensor and the servos are specialized motors.

doyler 20-12-2004 15:30

Re: Rookie help
 
um, i have 4 pwm out pins, but only three holes, does the closest one not matter for these servos?

Mark McLeod 20-12-2004 15:33

Re: Rookie help
 
Quote:

Originally Posted by doyler
um, i have 4 pwm out pins, but only three holes, does the closest one not matter for these servos?

Leave the inside pin unconnected. It's a duplicate power pin.

doyler 20-12-2004 15:41

Re: Rookie help
 
I can use any 2 pwm out pins right
And I just put the gyro in the first digital in spot

Mark McLeod 20-12-2004 15:47

Re: Rookie help
 
Quote:

Originally Posted by doyler
I can use any 2 pwm out pins right
And I just put the gyro in the first digital in spot

Yes, any pwms will do for the servos.

If you are starting with the default EDU code, that arranges for the first two inputs to be analog, so either 1 or 2 will work.

doyler 20-12-2004 15:49

Re: Rookie help
 
Will the default EDU code have stuff that i can just upload and make it work, just to learn the workings?

Mark McLeod 20-12-2004 15:55

Re: Rookie help
 
Quote:

Originally Posted by doyler
Will the default EDU code have stuff that i can just upload and make it work, just to learn the workings?

Yes. Start with that. Also get the EDU documentation from the innovationfirst.com website:
http://innovationfirst.com/FIRSTRobo...04-Jan-14a.pdf
http://innovationfirst.com/FIRSTRobo...10-15-2003.pdf
http://innovationfirst.com/FIRSTRobo...t_02-16-04.zip

doyler 20-12-2004 16:01

Re: Rookie help
 
And I just use MPLAB IDE to mess with it right?

Also, what kind of code am i looking for just to make these things move for starters

What code(s) will I have to upload as well?

Mark McLeod 20-12-2004 16:17

Re: Rookie help
 
Quote:

Originally Posted by doyler
And I just use MPLAB IDE to mess with it

Also, what kind of code am i looking for just to make these things move for starters

Yes, learn to use MPLAB.
How about putting something like this where it says "/* Add your own code here. */" in the file "user_routines.c" in the routine "Process_Data_From_Master_uP"
Code:

static unsigned char servo1=0; //Put these two lines right before the "Getdata" call
static unsigned char counter=0; // ditto
 
//Put these lines after "Add your own code"
 
if (counter < 20) // about half a second in the slow loop
counter++;
else
{
counter = 0;
 
if (servo1 < 255)
        servo1++; // servo1 will slowly step through each of it’s positions
else
        servo1 = 0; // servo1 will quickly reset to the zero position
}
pwm03 = servo1;


doyler 20-12-2004 16:21

Re: Rookie help
 
Thank you, i will fiddle and learn from that code (is user routines akin to autonomous cause i dont know how im gonna make them move if it isnt)

1 more question (i hope) how and what do i upload to my controller now?

Mark McLeod 20-12-2004 16:35

Re: Rookie help
 
Quote:

Originally Posted by doyler
Thank you, i will fiddle and learn from that

1 more question (i hope) how and what do i upload to my controller now?

Here are the basic steps:
  1. You’ll need to install MPLAB, IFI_Loader, and the mcc18 compiler on your computer.
  2. Download the default EDU code from http://innovationfirst.com/FIRSTRobo...umentation.htm (this will have a default.hex file that can be downloaded into the controller without having to edit anything with MPLAB) Save the original .zip file so you can restore the default code if some edit goes terribly wrong.
To edit the default code:
  1. Start MPLAB
  2. Select File -> Open Workspace…
  3. Browse to the EDU default code folder
  4. Open Default.mcw
  5. Make your code changes
  6. Use the “Build All” icon or select Project -> Build All (this will create a .hex file)
Start IFI_Loader
  1. Browse to the default code folder
  2. Select “Default.hex”
  3. Connect the serial cable from your computer to the EDU controller
  4. Turn on the EDU controller
  5. Press the “Prog” button until the program light turns orange
  6. Click on “Download” in IFI_Loader
Your program will start running.

doyler 20-12-2004 16:39

Re: Rookie help
 
C:\...\EDU_Default_02-16-04\user_routines.c:249:Error: syntax error
Halting build on first failure as requested.
BUILD FAILED: Mon Dec 20 16:36:48 2004

here is the snippet i edited

Code:

/*******************************************************************************
* FUNCTION NAME: Process_Data_From_Master_uP
* PURPOSE:      Executes every 17ms when it gets new data from the master
*                microprocessor.
* CALLED FROM:  main.c
* ARGUMENTS:    none
* RETURNS:      void
*******************************************************************************/
void Process_Data_From_Master_uP(void)
{
  Getdata(&rxdata);  /* Get fresh data from the master microprocessor. */

  Default_Routine();  /* Optional.  See below. */

  /* Add your own code here. */

  static char servo1=0;
  static char counter=0;
 
  if (counter < 20) // about half a second in the slow loop
    counter++;
  else
  {
    counter = 0;
    if (servo1 < 254)
          servo1++; // servo1 will slowly step through each of it’s positions
    else
            servo1 = 0; // servo1 will quickly reset to the zero position
  }
  pwm03 = servo1;

  printf("PWM OUT 7 = %d, PWM OUT 8 = %d\n",(int)pwm07,(int)pwm08);  /* printf EXAMPLE */

  Putdata(&txdata);            /* DO NOT CHANGE! */
}

(sorry if i am being annoying, im just new at this)

Mark McLeod 20-12-2004 16:43

Re: Rookie help
 
Variable declarations must be at the very beginning of a routine. I noted that above in an edit after my original post. Sorry about that.
Quote:

Originally Posted by doyler
Code:

/*******************************************************************************
* FUNCTION NAME: Process_Data_From_Master_uP
* PURPOSE: Executes every 17ms when it gets new data from the master
* microprocessor.
* CALLED FROM: main.c
* ARGUMENTS: none
* RETURNS: void
*******************************************************************************/
void Process_Data_From_Master_uP(void)
{
static unsigned char servo1=0;
static unsigned char counter=0;
Getdata(&rxdata); /* Get fresh data from the master microprocessor. */
 
Default_Routine(); /* Optional. See below. */
 
/* Add your own code here. */
 
if (counter < 20) // about one 1/3 second in the slow loop
counter++;
else
{
counter = 0;
if (servo1 < 255)
        servo1++; // servo1 will slowly step through each of it’s positions
else
        servo1 = 0; // servo1 will quickly reset to the zero position
}
pwm03 = servo1;
 
printf("PWM OUT 7 = %d, PWM OUT 8 = %d\n",(int)pwm07,(int)pwm08); /* printf EXAMPLE */
 
Putdata(&txdata); /* DO NOT CHANGE! */
}

(sorry if i am being annoying, im just new at this)

You're not annoying. I like to help you get started.

doyler 20-12-2004 16:54

Re: Rookie help
 
Error - Source file 'C:\...\ifi_utilities.c' name exceeds file format maximum of 62 characters.
:)

silly mplab

[edit]

hmm, do i need to install my usb to serial converter?

[/edit]

Mark McLeod 20-12-2004 16:55

Re: Rookie help
 
Quote:

Originally Posted by doyler
Error - Source file 'C:\...\ifi_utilities.c' name exceeds file format maximum of 62 characters.
:)

silly mplab

Have that default code folder sitting on your desktop don't you?:rolleyes:

doyler 20-12-2004 16:56

Re: Rookie help
 
yeah, but i have a keyspan usb -> serial

does xp not auto add it?

[edit]

Build Succeeded!

now to add it to the controler :(

[/edit]

Mark McLeod 20-12-2004 17:02

Re: Rookie help
 
Quote:

Originally Posted by doyler
yeah, but i have a keyspan usb -> serial

does xp not auto add it?

XP will. [edit2] But you probably have to install that particular device driver the first time. An install CD should have come with the converter, or you might be able to download the driver from keyspan.

Are you getting a download error using IFI_Loader?
Sometimes those adaptors auto connect to a different COM port than it advertises. You can run through and try all the possible COM ports in IFI_Loader by choosing "PortSettings" and COM1, try to download, then COM2, and try to download, etc.

[edit]
I misunderstood.
Do you have the controller or will you try that later?

For those of you reading along...
The error doyler got was because one of the path and filenames was too long for MPLAB to handle. Combined they cannot be more than 62 characters or so. The usual solution is to keep the directory for all your FIRST code at the top level of the C drive, i.e., "C:\FIRST" not on your desktop because that's really at "C:\Documents and Settings\doyler\Desktop\" which is already 41 characters long.

Mark McLeod 20-12-2004 18:43

Re: Rookie help
 
Quote:

Originally Posted by doyler
keyspan usb -> serial

How about getting the driver from here?
http://www.keyspan.com/downloads/win/

doyler 21-12-2004 14:21

Re: Rookie help
 
2 questions
For that code do i have to plug them into certain pwm outs?
Also, will my code just start?

Mark McLeod 21-12-2004 14:43

Re: Rookie help
 
Quote:

Originally Posted by doyler
2 questions
For that code do i have to plug them into certain pwm outs?
Also, will my code just start?

It assumes one servo is attached to pwm 3. You can change this to any other pwm.
Code:

pwm03 = servo1;

The code should start immediately after the download has completed.

doyler 21-12-2004 14:47

Re: Rookie help
 
I charged my battery and the chargers light turned green
I plugged it in, the battery power and fault status lights blinked, but then nothing

[edit]
now battery power, program state, and fault status are blinking green while pwn in/radio is a constant red

is this good?
[/edit]

Mark McLeod 21-12-2004 14:49

Re: Rookie help
 
Quote:

Originally Posted by doyler
I charged my battery and the chargers light turned green
I plugged it in, the battery power and fault status lights blinked, but then nothing

[edit]
now battery power, program state, and fault status are blinking green while pwn in/radio is a constant red

is this good?
[/edit]

Very good.
Now press the Prog button to get a solid orange LED.

doyler 21-12-2004 14:51

Re: Rookie help
 
Ok it works
Now i just need to learn how, why, and how to change it

First off:
How would i make the program loop somewhat faster

Mark McLeod 21-12-2004 14:59

Re: Rookie help
 
Quote:

Originally Posted by doyler
How would i make the program loop somewhat faster
Also, what will the servo do when the program executes so i know i did it right

The first sound you heard was the servo moving to it's "0" position.
The servo will then take 1.4 minutes to move very slowly through a 60 degree turn before it quickly whips back to the "0" position and repeats.

You can speed this up by changing the delay we built in.
Code:

if (counter < 20) // about 1/3 second in the slow loop for one ~60/255 degree turn
Make it "counter < 2" and it'll take about 17ms to complete a cycle.

doyler 21-12-2004 15:04

Re: Rookie help
 
Code:

static unsigned char servo1=0;
static unsigned char counter=0;

Ok, so theses make two variables, 1 is the servos position(?) and the other is the counter right

(i will go through it step by step)

Mark McLeod 21-12-2004 15:17

Re: Rookie help
 
Quote:

Originally Posted by doyler
Code:

static unsigned char servo1=0;
static unsigned char counter=0;

Ok, so theses make two variables, 1 is the servos position(?) and the other is the counter right

(i will go through it step by step)

Let me know if it worked as expected above.

Yes, servo1 is the position you set for the servo to move to. You can actually set it to any value you like. For instance if you have an RC unit, you could use that to move the servo.

The variable "servo1" could be completely replaced in this code sample with the system variable "pwm03" if you wanted. It isn't strictly needed, but it does make it easy to switch to a different pwm without having to change all your code.

This routine (Process_Data_From_Master_uP) in the default code is called the slow loop. It executes about 59 times per second (in the EDU, FRC is 38 times per sec.), because that's how fast the radio packets come in. We're using that characteristic of the loop to make "counter" a cheap timer. Each loop equals 1/59 second, so "counter<59" , for example, will equal one second.

[edit]
P.S.
You can also change "servo1++;" to something like "servo1 += 5;" to make the servo move faster still. But be careful that the logic of the check "if (servo1 < 255)" catches servo1 before it tries to become larger than 255. When that happens servo1 cannot hold a value larger than 255, so it will suddenly become a completely unexpected value.

doyler 21-12-2004 15:21

Re: Rookie help
 
Yes, it worked

So 38 is 1 second?
Then why is 2 faster then 20

I understand the variables part (except for the counter which is covered in the below code)

Code:

if (counter < 2) // about half a second in the slow loop for one 60/255 degree turn (2 = 7 seconds, 20=2 minutes)
counter++;

I know counter++ just adds one to the variable counter, but i dont understand how the counter itself works

[edit]
i dont understand your edit fully
[/edit]

Mark McLeod 21-12-2004 15:48

Re: Rookie help
 
[edit] I confused the issue by mixing up the number of slow loops per second between the EDU (59) and the FRC (38) [/edit]
Well, we’re moving the servo a tiny bit (servo1++; equals approx. 60/255 degrees) each time counter adds up to:
- 59 loops = 1 second (counter < 59)
- 59/2 or ~30 loops = ½ second (counter < 30)
- 59/19 or 1 loop = 17ms (counter < 2)

We don’t have to move servo1 (+1) each time. A servo is made to jump immediately to an exact position. It just happened to be convenient for our example to move it slowly through it’s entire range of motion. You could have servo1 make bigger jumps like +5 or +10 if you like.

You will have to become aware of what values the different variable types are made to hold. We declared servo1 to be an unsigned char. An unsigned char is only big enough to store the values 0 through 255. If you try to make it equal something outside this range, such as 300, you won’t get any error message, but you will get what seems to be a random number instead.

doyler 21-12-2004 15:50

Re: Rookie help
 
Ok, i understand the whole counter except how to calculate it

- 38/2 or 20 loops = ½ second (counter < 20)
- 38/19 or 2 loops = .05 sec. (counter < 2)

how do you know counter < 20 is equal to 38/2 and how do you know that is 1/2 a second

[edit]
and how/why is 1 second equal to 38 loops

/************************************************** *****************************
* FUNCTION NAME: Process_Data_From_Master_uP
* PURPOSE: Executes every 17ms when it gets new data from the master
* microprocessor.

[/edit]

Mark McLeod 21-12-2004 16:17

Re: Rookie help
 
The confusion is my fault. I'm mixing the numbers between the FRC and the EDU controller.

The EDU receives radio packets once every 17ms or approx 58.8 times per second.
The FRC receives packets once every 26.2ms or approx 38 times per second.

I'll go back and correct my examples above to reflect:
~59 = 1 second
~30 = 1/2 second
~15 = 1/4 second
etc.

doyler 21-12-2004 16:19

Re: Rookie help
 
So how would i calculate whatever number i put in here

Code:

if (servo1 < 254)
        servo1+=5; // servo1 will slowly step through each of it’s positions //servo1++; will make it move slower, but it is default

how fast it will move

also here:

Code:

if (counter < 2) // about half a second in the slow loop for one 60/255 degree turn (2 = 7 seconds, 20=2 minutes)
counter++;

and how their speeds work in conjunction to make how fast my eyes see the servo move

[edit]
also, where does my output go from

Code:

printf("PWM OUT 7 = %d, PWM OUT 8 = %d\n",(int)pwm07,(int)pwm08); /* printf EXAMPLE */
(some of the default code)

[/edit]

Mark McLeod 21-12-2004 16:35

Re: Rookie help
 
(counter < n) is the time between each distinct movement of the servo. Where n/59 = the time seconds. For example,
n= 59 is 1 second,
n=118 is 2 seconds,
n=30 = ½ second

(servo1 += y) is how large a movement the servo will make each time (according to the above). For example, y=1 position and is the smallest movement the servo can make, y=5 moves 5 servo positions and because of this will appear to be 5 times as fast.
You’ll have to change the limit check “if (servo1 < 254)” to match whatever number you use, to make sure servo1 will never end up greater than 255. For instance,
if y=5 the check should become “if (servo1 < 250)”
if y=10 the check should become “if (servo1 < 240)”

The range of motion depends on the actual servo model you have.

doyler 21-12-2004 16:43

Re: Rookie help
 
Ok, for the servo +=5 is the little movements it makes before it resets?

If so i understand all that

Code:

pwm03 = servo1;
just means that the code for servo1 goes to pwm?

if so then 1 question not pertaining to the servos

how does

Code:

printf("PWM OUT 7 = %d, PWM OUT 8 = %d\n",(int)pwm07,(int)pwm08); /* printf EXAMPLE */
work and where does it go

Mark McLeod 21-12-2004 16:57

Re: Rookie help
 
Quote:

Originally Posted by doyler
Code:

pwm03 = servo1;
just means that the code for servo1 goes to pwm?

Yep. Whatever pwm03 is set to is what goes out of the pwm pins.
Quote:

Originally Posted by doyler
how does
Code:

printf("PWM OUT 7 = %d, PWM OUT 8 = %d\n",(int)pwm07,(int)pwm08); /* printf EXAMPLE */
work and where does it go

printf is a debugging statement. Any printf statement will show up in the IFI_Loader terminal window as long as your computer is still connected to the controller. You can print the value of anything you want, but there are some limitations and it has a few bugs.
In this case the statement probably shows up in the terminal window as:
PWM OUT 7 = 127, PWM OUT 8 = 127

Do a search in Chiefdelpi for printf and you'll get a lot of information about it.
You need to pick up a C programming book. printf is a standard C function.

doyler 21-12-2004 16:59

Re: Rookie help
 
So I can do pwm08 = servo1 and they will both moved if plugged in 3 and 8?

Also, I can just change the 7 and 8 to the 2 i am using and just load ifi loader to see whats happening?

Mark McLeod 21-12-2004 17:01

Re: Rookie help
 
Quote:

Originally Posted by doyler
So I can do pwm08 = servo1 and they will both moved if plugged in 3 and 8?

Also, I can just change the 7 and 8 to the 2 i am using and just load ifi loader to see whats happening?

Yes on all counts:)

Astronouth7303 21-12-2004 18:04

Re: Rookie help
 
Quote:

Originally Posted by doyler
now battery power, program state, and fault status are blinking green while pwn in/radio is a constant red

is this good?

You're fine.

There are some PWM Ins along the top (the female connectors) for a hobby radio (similar to expensive RC cars or planes). Since I don't think you have one, don't worry about it.

[edit]Sorry, that's a little old.[/edit]

doyler 22-12-2004 14:41

Re: Rookie help
 
I was messing with it last night and just got stuck on one thing

How would I make it go to the end, and then reset without stopping in the middle at times

Mark McLeod 22-12-2004 14:57

Re: Rookie help
 
Quote:

Originally Posted by doyler
I was messing with it last night and just got stuck on one thing

How would I make it go to the end, and then reset without stopping in the middle at times

Give me a step-by-step description of what you see happening. I won't be able to test until tonight when I get home (and drop by the school).

What you are seeing could be due to the servo not being given enough time to reach the requested position. The electronic controller operates much faster than the mechanical servo and could be sending position requests too quickly.

doyler 22-12-2004 15:51

Re: Rookie help
 
I wasn't seeing anything

I was just trying to make it go straight to the end and then reset and then loop

Mark McLeod 22-12-2004 15:55

Re: Rookie help
 
Quote:

Originally Posted by doyler
I wasn't seeing anything

I was just trying to make it go straight to the end and then reset and then loop

Do you mean servo1 = 0 followed by servo1 = 255, then repeat?
What kind of delay do you have between changing positions (i.e., counter)?

doyler 22-12-2004 16:05

Re: Rookie help
 
Yeah like that, but i didnt put in a counter because I couldn't figure out how to time it

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