View Single Post
  #3   Spotlight this post!  
Unread 04-08-2010, 11:50
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,795
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Delay10KTCYx in C18

You've got lots of options we can help you with. Time can be based on counting the number of loops within the 18.5ms loop area or based on a hardware timer.

You can learn about state machines - what we typically use in cases like these.
Code:
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call and isn't necessary if you never leave the subroutine.
static int autostate=1;   //keep track of what step we're supposed to be doing
 
  switch (autostate)
  {
    case 1:   // Drive forward
        pwm02 = 100;
        pwm03 = 155;  //motor is reversed
        if (counter>54)  //1 second (1 sec divided by 18.5ms = 54 loops per second)
        {  
           autostate = 2;  // move on to the next step
           counter = 0;    // reset our timer for the next step
        }
 
    case 2:   // Turnaround
        pwm02 = 100;
        pwm03 = 100;  //motor is reversed
        if (counter>108)  //2 seconds
        {  
           autostate = 3;
           counter = 0;
        }
 
    case 3:   // Drive forward (returning now)
        pwm02 = 100;
        pwm03 = 155;  //motor is reversed
        if (counter>54)  //1 second
        {  
           autostate = 4;
           counter = 0;
        }
 
     case 4:   // Stop - What to do when everything else is done
     default:  // also what to do if an invalid autostate occurs
 
       pwm02 = pwm03= 127;   // Make sure the last thing you do is always stop
  }
  counter++;
You can also write your own WAIT subroutine to do what you want if you prefer to flow through one set of commands. There's an easy way to do this for Autonomous based on the User_Autonomous_Code structure and counting the way Foster suggested. It's not something you would use in Teleop where you want to respond to driver commands instantaneously.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 04-08-2010 at 13:50.