Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Inovation First FRC Robot Controller (http://www.chiefdelphi.com/forums/showthread.php?t=105817)

SeanPerez 21-04-2012 19:24

Re: Inovation First FRC Robot Controller
 
is there a way that i can delay something without delaying the rest of the code?

i want to do things with delays but i need pwm 99.9% of the time.

for example- im driving the robot forward and a sensor is triggered, 1 second after the sensor is trigered i want to turn on a motor for 3 seconds then turn it off. i want this to be done while im driving and never do i want to lose control of the pwms.

is this possible? what would the code be?

Mark McLeod 21-04-2012 20:51

Re: Inovation First FRC Robot Controller
 
The way it's done on this system is by way of something called a state machine.

Essentially, you just keep track of what your task(s) should be doing (delay, change a light, drive a motor, etc.) and keep checking every time you pass through the code.
If it's supposed to delay, then each time through the code checks to see if enough time has elapsed (e.g., has 38 counts gone by?). At the end of that delay the state changes to do whatever the next thing through might be.

SeanPerez 21-04-2012 20:57

Re: Inovation First FRC Robot Controller
 
Quote:

Originally Posted by Mark McLeod (Post 1161125)
The way it's done on this system is by way of something called a state machine.

Essentially, you just keep track of what your task(s) should be doing (delay, change a light, drive a motor, etc.) and keep checking every time you pass through the code.
If it's supposed to delay, then each time through the code checks to see if enough time has elapsed (e.g., has 38 counts gone by?). At the end of that delay the state changes to do whatever the next thing through might be.

What would the code look like for something like for...
driving a motor forward and having a delay of 1 second after a sensor is triggered to start another motor for 2 seconds then stop the motorn after the 2 seconds?

Mark McLeod 21-04-2012 21:37

Re: Inovation First FRC Robot Controller
 
A state machine looks something like this, but this hasn't been tested of course.


Code:

  static unsigned int state=0, timecounter;
 
  //Drive the robot forward at full speed (or use joysticks)
  pwm01 = 255;  //full forward in this system
  pwm02 = 0; // full reverse in this system. Note one motor will be backwards, because it’s opposite the other side of the robot.
 
  //If some button is pushed, then set state = 1 (you have to figure out how the sequence of actions begins)
 
  switch (state) {
 
      case 0: //Do Nothing
                  break;
 
      case 1: //Check for sensor
                  if (sensor == 1) {
                        state = 2;  //Go on to state 2 after the sensor triggers
                        timecounter=0;
                  }
                  break;
 
      case 2: // Delay for 1 second
                timecounter++;
                if (timecounter == 38) {
                    state = 3;
                    timecounter=0;
                }

              break;
 
      case 3: //Run motor for 2 seconds
                pwm03 = 255;
                timecounter++;
                if (timecounter == 76) {
                    state = 4;
                }
                break;
 
        case 4: // stop the motor
                pwm03=127; //neutral in this system
                state = 0;  //Go back to waiting
 

  }


SeanPerez 21-04-2012 22:14

Re: Inovation First FRC Robot Controller
 
i get a syntex error at

static unsigned int state = 0, timecounter;

what did i do wrong?

connor.worley 21-04-2012 22:20

Re: Inovation First FRC Robot Controller
 
Quote:

Originally Posted by SeanPerez (Post 1161220)
i get a syntex error at

static unsigned int state = 0, timecounter;

what did i do wrong?


Should be written as
static unsigned int state, timecounter = 0;

SeanPerez 21-04-2012 22:25

Re: Inovation First FRC Robot Controller
 
i got it, its actually:

unsigned int state, timercounter = 0;

Thanks though, it helped:D

SeanPerez 21-04-2012 22:35

Re: Inovation First FRC Robot Controller
 
I had the error sensor has not been defined but i just did

int sensor = rc_dig_in01

and that solved it. but now i get this error:

symbol timecount has not been defined
ivalue required

what code do i add to fix this?

SeanPerez 21-04-2012 22:51

Re: Inovation First FRC Robot Controller
 
unsigned int state;
int timecounter;
int timecount = 0;
int sensor = rc_dig_in01;


// this is the robot drive for a tank drive
pwm01 = 255; // full forward
pwm02 = 255; // full forward


// if some button is pressed, then set state = 1(i have to figure this out.)

timecounter = timecount + timecounter;

switch (state) {

//check sensor
if (sensor == 1) {
state = 2;
timecounter=0;
}
break;

//Delay for 1 second after trigger
timecount++;
if (timecounter == 38) {
state = 3;
timecounter=0;
}
break;

//Run motor for 2 seconds
pwm03 = 255;
timecount++;
if (timecounter == 76) {
state = 4;
}
break;

//stop the motor
pwm03 = 127;
state = 0; // go back to waiting
}





Does this seem right? i fixed the code posted in the reply and this is it. it built successfuly. does it look like it will work? if its wrong what do i do to fix it?

SeanPerez 22-04-2012 00:23

Re: Inovation First FRC Robot Controller
 
Since i could not get the code to work, can someone give me just a simple counter that i can use to count how many times the code has looped.?

Thanks
I will build on top it

RyanN 22-04-2012 01:49

Re: Inovation First FRC Robot Controller
 
The telop code is called through a function.

If you initialize your variable within the function, then the variable will be lost once that function is done executing. You will need to make a global variable at the top of the teleop.c file to keep the value throughout multiple iterations of the function.

You're also not using the switch (case) statement properly, and as you might be able to tell, you're missing the case part.

switch(variable)
{
case 0: //execute some code on condition 0
break;
case 1: //execute some code on condition 1
break;
case 2: //execute some code on condition 2
break;
default: // execute some code if it falls outside of any of the above
}

Again, there is no way to do a true timer on the robot controller. You can make a timer using an if statement and a counter though, much like what it appears you are trying to do.

I'm trying to recall information from 2008, but I think the teleop function is called every 26.4ms. Use that to do your timers.

If you want to wait for about 1 second, you'll need to count 37 or 38 times.

I would also be a bit careful with your motor values. 255 is full speed... I'm not sure about your robot, but full speed is hard to stop if you don't want it to run, especially if you don't have a competition box with a disable switch.

SeanPerez 22-04-2012 09:37

Re: Inovation First FRC Robot Controller
 
i tryed to use just a basic counter to tell me how many times code has looped but it never worked. can some one give me the code for a counter?

Thanks

SeanPerez 22-04-2012 09:55

Re: Inovation First FRC Robot Controller
 
Actually i just got it. the problem was i left out the part that resets the counter to 0 and i placed the variables in the wrong place.

SeanPerez 22-04-2012 15:19

Re: Inovation First FRC Robot Controller
 
For the state machine i finally got it to work. here is the working code. i used it to flash lights on the OI and use Pwm03.

int time;
int count;
int state = 1;
int sensor;


switch (state) {
case 0: // do nothing
break;

case 1:
//check sensor
if (sensor == 1) {
time = 0;
count = 0;
state = 2;
}
break;

case 2:
//Delay for 1 second after trigger
time = count;
count++;
if (time == 16) { // for 1 second time == 38
time = 0;
count = 0;
state = 3;
}
break;

case 3:
//Run motor for 2 seconds
pwm03 = 255;
Switch1_LED = 1;
Switch2_LED = 1;
Switch3_LED = 1;
time = count;
count++;
if (time == 16) { // for 1 second time == 38
time = 0;
count = 0;
state = 4;
}
break;

case 4: // stop them
pwm03 = 127 ;
Switch1_LED = 0;
Switch2_LED = 0;
Switch3_LED = 0;
state = 1;
}

SeanPerez 22-04-2012 17:57

Re: Inovation First FRC Robot Controller
 
I was told that the analog inputs can only be read using the Get_Analog_Value() function.
Analog inputs are: rc_ana_in01,...rc_ana_in16


How do i use this function if i want to read data from rc_ana_in01?

What would the code be?

Thanks


All times are GMT -5. The time now is 20:14.

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