Go to Post Even though others have had success, I will be honest, this method sort of creeps me out. - Joe Johnson [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-03-2004, 22:03
Xufer's Avatar
Xufer Xufer is offline
8 Seasons and as stable as ever
AKA: Working, 2/4 degrees complete still schooling...
FRC #1740 (The Cyber Colonels)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Waterford, CT
Posts: 231
Xufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to behold
Send a message via AIM to Xufer Send a message via MSN to Xufer Send a message via Yahoo to Xufer
quick question: TIMERS

The code below works but i need just a few more loops in the code in order to get it to go exactly where i need it to any one know how i can make it start a new count after it gets past 254 so it doesnt loop, and i can add a few more steps to it, or does some one have a better way of doing this ? I got this way to work it just needs to run for a little bit longer.

Code:
void User_Autonomous_Code(void)
{
 int count;
 count = 1;
 while (autonomous_mode)   /* DO NOT CHANGE! */
 {  
     if (statusflag.NEW_SPI_DATA)      /* 26.2ms loop area */
     {
         Getdata(&rxdata);   /* DO NOT DELETE, or you will be stuck here forever! */
   /*40ticks = 1 sec 20ticks = .5 secs 10ticks=.25 secs */ //increse counter by 1 /* DO NOT DELETE, or you will get no PWM outputs! *//*40ticks = 1 sec 20ticks = .5 secs 10ticks=.25 secs */
   
   if ( count >= 124 ) {
   }   

   if (count > 0 && count < 34 )
   { 
    pwm05 = 60;
       }
   if (count > 35 )
   { 
    pwm05 = 127;
       }
   if (count > 0 && count < 30 )
   { 
    pwm04 = 0;
       }
   if (count > 32 )
   { 
    pwm04 = 127;
       }
   if (count > 35 && count < 90 )
   { 
    pwm01 = 190;
       }
   if (count > 35 && count < 90 )
   { 
    pwm02 = 190;
       }
   if (count > 92  && count <= 122)
   {
    pwm01 = 190;
   }
   if (count > 92 && count <= 122)
   {
    pwm02 = 53;
   }
   if (count > 130 && count < 160 )
   { 
    pwm01 = 190;
       }
   if (count > 130 && count < 160 )
   { 
    pwm02 = 190;
      }
   if (count > 163  && count <= 183)
   {
    pwm01 = 190;
   }
   if (count > 163 && count <= 183)
   {
    pwm02 = 53;
   }
   if (count > 223 && count < 250 )
   { 
    pwm01 = 190;
       }
   if (count > 223 && count < 250 )
   { 
    pwm02 = 190;
      }
   if (count > 255  && count <= 275)
   {
    pwm01 = 190;
   }
   if (count > 255 && count <= 275)
   {
    pwm02 = 53;
   }

   ++count;  
      Putdata(&txdata);   
  }
 }
}
any help would be greatly appreciated.
__________________


If saftey is first and i put FIRST ahead of everything, then i must be the safest guy in the world.

Team 521 Alumni -- Team 1740 Mentor-- Go Go 1740!
Sometimes I think I've been doing this too long and other times I think I haven't done this enough... but all the times I'm sure I'm crazy
Photonics, It's Your Future
  #2   Spotlight this post!  
Unread 10-03-2004, 22:05
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: quick question: TIMERS

you could just do an if of when it goes above the time.

if(timer>40)
{}
else if(timer>80)
{}
  #3   Spotlight this post!  
Unread 10-03-2004, 22:36
Daniel Daniel is offline
Daniel Katanski
#0240
Team Role: Mentor
 
Join Date: Feb 2004
Location: Monroe, MI
Posts: 32
Daniel is on a distinguished road
Re: quick question: TIMERS

Your code is begging to become a state machine. It is a codeing techinque that uses a switch statement and where each case is an independant state. This works well because of the 26.3 ms looping process. The benefit of state machines is that it can be written so that some states are re-entrant where they look for some event like time or distance to trigger the program into the next state.

switch (state) {
case 1 :
// start motors
t = time + SomeDiration
break;

case 2 :
if (time < t) // This is re-entrant.
state = 2;
else
state = 3;
break;

case 3 :
// stop motors and start next manuever...
...

Get the picture? It's not hard. It does require some thought. But this is how most very complex programs start out. There are other ways of doing this with pointers and trillions of function - but this is just so simple and readable.
If you are at the Great Lakes Competition look me up and I'll show you more.
  #4   Spotlight this post!  
Unread 10-03-2004, 22:36
Xufer's Avatar
Xufer Xufer is offline
8 Seasons and as stable as ever
AKA: Working, 2/4 degrees complete still schooling...
FRC #1740 (The Cyber Colonels)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Waterford, CT
Posts: 231
Xufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to behold
Send a message via AIM to Xufer Send a message via MSN to Xufer Send a message via Yahoo to Xufer
Re: quick question: TIMERS

huh ? dun quite get it, too tired maybe ...
__________________


If saftey is first and i put FIRST ahead of everything, then i must be the safest guy in the world.

Team 521 Alumni -- Team 1740 Mentor-- Go Go 1740!
Sometimes I think I've been doing this too long and other times I think I haven't done this enough... but all the times I'm sure I'm crazy
Photonics, It's Your Future
  #5   Spotlight this post!  
Unread 11-03-2004, 02:06
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: quick question: TIMERS

A few things:

At the most basic level, you don't need to worry about the int looping over. Ints in C are two byte variables; an unsigned one will store up to 32000+. If you need more than that, you can use long, which is a four byte variable.

Second, you don't need two seperate if statements when you are setting pwm01 and pwm02--they do the same thing, so put all the code you need in one! You'll save processing power and increase readability.

State machines can be a little confusing at first, but they will make your code much more readable in the end, and much faster. A state machine uses the switch command, which will look at one variable, and jump to the "case" that the variable number points to. So you would have an int variable to count another int variable that would be the state.

int count;
int state = 0;

void InitAuto(void){
count = 0;
//any other variables that you need to reset
}

void User_Auto_Code(void){
switch(state){ //look at the state variable for code to jump to

case 0:
//do some stuff
if(count > 100){
state++; //move to state 1
InitAuto();
}
break;

case 1:
//do some other stuff
if(count > 50){
state++; //move to state 2
InitAuto();
}
break;

etc etc etc....
}//close switch
count++;

As you can see, its really easy to make readable code this way, and you know exactly how many program counts are in each state without having to do any math =). This is also a much lighter load on your processor; your program will execute a lot faster and be cleaner in memory.

A couple symantic things: the break; statement is needed at the end of every case, to tell the compiler that you are done with the case. Just set up your cases in order, and tell your motors what to do =).

If you can implement real timers, it will make the program that much better (254 uses a millisecond timer), but you should be fine with the program counts.

Good luck!
__________________
~Alex Baxter
Programming, Arms operation, Team 254

Last edited by 10intheCrunch : 11-03-2004 at 02:08.
  #6   Spotlight this post!  
Unread 11-03-2004, 06:06
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: quick question: TIMERS

Quote:
Originally Posted by Xufer
huh ? dun quite get it, too tired maybe ...
Instead of stead of what you currently have, you'd put in something like his example. The basic idea is laid out below: (in semi-pseudo-code)
Code:
Static state variable starting at 0

switch(state variable)
{
    case 0:
        perform actions set 1
        check if this state is complete
             if it is, increment the state variable

     case 1:
        perform actions set 2
        check if this state is complete
             if it is, increment the state variable

    case 2:
        perform actions set 3
        check if this state is complete
             if it is, increment the state variable

    ...

     case X:
        don't do anything
}
By repeatedly calling this switch statement, it will work its way through all the states. You can, of course, also have statements in there that reset the statee variable, or put it to where ever you want.

Hope that clarifies it some.
__________________

  #7   Spotlight this post!  
Unread 11-03-2004, 14:23
Xufer's Avatar
Xufer Xufer is offline
8 Seasons and as stable as ever
AKA: Working, 2/4 degrees complete still schooling...
FRC #1740 (The Cyber Colonels)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Waterford, CT
Posts: 231
Xufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to behold
Send a message via AIM to Xufer Send a message via MSN to Xufer Send a message via Yahoo to Xufer
Re: quick question: TIMERS

Quote:
Originally Posted by Texan
Instead of stead of what you currently have, you'd put in something like his example. The basic idea is laid out below: (in semi-pseudo-code)
Code:
Static state variable starting at 0

switch(state variable)
{
    case 0:
        perform actions set 1
        check if this state is complete
             if it is, increment the state variable

     case 1:
        perform actions set 2
        check if this state is complete
             if it is, increment the state variable

    case 2:
        perform actions set 3
        check if this state is complete
             if it is, increment the state variable

    ...

     case X:
        don't do anything
}
By repeatedly calling this switch statement, it will work its way through all the states. You can, of course, also have statements in there that reset the statee variable, or put it to where ever you want.

Hope that clarifies it some.
yea kind of but what tells it when a state is complete ? how would i say
case 1 is to happen for 2 seconds and case 2 is supposed to happen for 3 seconds?
pseudo of what im talking about :

case 1 for 2 seconds
pwm01= 200
pwm02= 200
pwm03= 150
pwm04= 200

then go to case 2

case 2 for 3 seconds
pwm01= 200
pwm02= 100

then go to case 3

case 3
pwm01= 200
pwm02= 200

etc . what keeps it with in time constraints and what cuases it to go to the next case statement.
__________________


If saftey is first and i put FIRST ahead of everything, then i must be the safest guy in the world.

Team 521 Alumni -- Team 1740 Mentor-- Go Go 1740!
Sometimes I think I've been doing this too long and other times I think I haven't done this enough... but all the times I'm sure I'm crazy
Photonics, It's Your Future
  #8   Spotlight this post!  
Unread 11-03-2004, 14:31
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: quick question: TIMERS

That's all up in my post...

Make a "StateInit" function that runs everytime you move to a new state, that at the very least resets your count to 0. Put a count++; statement after the switch. Then, inside each case, say

if(count > someNumber){
StateInit();
state++;
}

That will move it to the next case when the count is above the number you want. Since you don't have a real timer you'll have to do the math yourself for how long to stay in each one but it shouldn't be that bad.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #9   Spotlight this post!  
Unread 11-03-2004, 14:50
Xufer's Avatar
Xufer Xufer is offline
8 Seasons and as stable as ever
AKA: Working, 2/4 degrees complete still schooling...
FRC #1740 (The Cyber Colonels)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Waterford, CT
Posts: 231
Xufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to behold
Send a message via AIM to Xufer Send a message via MSN to Xufer Send a message via Yahoo to Xufer
Re: quick question: TIMERS

Quote:
Originally Posted by 10intheCrunch
That's all up in my post...

Make a "StateInit" function that runs everytime you move to a new state, that at the very least resets your count to 0. Put a count++; statement after the switch. Then, inside each case, say

if(count > someNumber){
StateInit();
state++;
}

That will move it to the next case when the count is above the number you want. Since you don't have a real timer you'll have to do the math yourself for how long to stay in each one but it shouldn't be that bad.
If you do that wont the count everflow after it reaches 254 ? whats to keep it from doing that ? Im still kind of missing it i guess. Or how would i make a real timer i mean i treid experimenting with one but i failed.
__________________


If saftey is first and i put FIRST ahead of everything, then i must be the safest guy in the world.

Team 521 Alumni -- Team 1740 Mentor-- Go Go 1740!
Sometimes I think I've been doing this too long and other times I think I haven't done this enough... but all the times I'm sure I'm crazy
Photonics, It's Your Future
  #10   Spotlight this post!  
Unread 11-03-2004, 15:09
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: quick question: TIMERS

Again...read my post...

If you use an int for your counter variable, you will have more than enough space. An int in C is a two byte variable, meaning the signed version can store from -32000 to 32000.

What you're thinking about is a char, a one byte variable. That will turn over at 254 (or 128 if it is signed).

Have you tested your code yet?
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #11   Spotlight this post!  
Unread 11-03-2004, 15:12
Xufer's Avatar
Xufer Xufer is offline
8 Seasons and as stable as ever
AKA: Working, 2/4 degrees complete still schooling...
FRC #1740 (The Cyber Colonels)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Waterford, CT
Posts: 231
Xufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to beholdXufer is a splendid one to behold
Send a message via AIM to Xufer Send a message via MSN to Xufer Send a message via Yahoo to Xufer
Re: quick question: TIMERS

Quote:
Originally Posted by 10intheCrunch
Again...read my post...

If you use an int for your counter variable, you will have more than enough space. An int in C is a two byte variable, meaning the signed version can store from -32000 to 32000.

What you're thinking about is a char, a one byte variable. That will turn over at 254 (or 128 if it is signed).

Have you tested your code yet?

so what if i use my orrigianl code and just change char to int then i wont have to eorry about the overflow problem right ?
__________________


If saftey is first and i put FIRST ahead of everything, then i must be the safest guy in the world.

Team 521 Alumni -- Team 1740 Mentor-- Go Go 1740!
Sometimes I think I've been doing this too long and other times I think I haven't done this enough... but all the times I'm sure I'm crazy
Photonics, It's Your Future
  #12   Spotlight this post!  
Unread 11-03-2004, 15:16
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: quick question: TIMERS

Quote:
Originally Posted by Xufer
yea kind of but what tells it when a state is complete ? how would i say
case 1 is to happen for 2 seconds and case 2 is supposed to happen for 3 seconds?
pseudo of what im talking about :

case 1 for 2 seconds
pwm01= 200
pwm02= 200
pwm03= 150
pwm04= 200

then go to case 2

case 2 for 3 seconds
pwm01= 200
pwm02= 100

then go to case 3

case 3
pwm01= 200
pwm02= 200

etc . what keeps it with in time constraints and what cuases it to go to the next case statement.

You could have a variable that gets increment every 26.2ms. If this functions is getting called that often, it could be static to it. If not, it could be a global variable that is incremented in something that is. Then, for the tests, you would check if the variable was greater than, say, 78. This would make it run for approzimately 2 second ((1000ms/26.2ms)*x seconds). Or, if you need more accuracy than that, and that's not bad, you can have an external clock attched to pin 1/2 and have an interrunpt that increments it.
__________________

  #13   Spotlight this post!  
Unread 11-03-2004, 15:40
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: quick question: TIMERS

Quote:
Originally Posted by Xufer
so what if i use my orrigianl code and just change char to int then i wont have to eorry about the overflow problem right ?
Your original code does use int instead of char, but yes. You shouldn't have any overflow problem.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #14   Spotlight this post!  
Unread 11-03-2004, 16:00
jacob_dilles's Avatar
jacob_dilles jacob_dilles is offline
Registered User
AKA: theshadow
FRC #0620 (WarBots)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Vienna, VA
Posts: 245
jacob_dilles will become famous soon enough
Send a message via AIM to jacob_dilles
Re: quick question: TIMERS

Quote:
Originally Posted by Texan
you can have an external clock attched to pin 1/2 and have an interrunpt that increments it.
doesnt the pic have like 3 built in timers?
__________________
--------------------------
"You're not a real programmer until all your sentences end with semicolons;"
  #15   Spotlight this post!  
Unread 11-03-2004, 17:18
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: quick question: TIMERS

5: 0-4. 0 is used by FRC_library.lib and is slowest of all, 1&3 are faster than 2&4.
Range: (Prescaler Range|Postscaler Range)
0: 1:2-1:256|1:1
1: 1:1-1:8|1:1
2: 1:1-1:16|1:1-1:16
3: 1:1-1:8|1:1
4: 1:1-1:16|1:1-1:16
The prescalers are powers of 2 (*2), and postscalers are integers (+1). The base frequincies of all the timers is the same: w/o scaling they are 10MHz clocks and a 100ns tick.

This all out of Kevin's interupt.c file. WELL commented
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question Burgabot Programming 31 02-12-2003 19:22
Quick <hr> question Jack Website Design/Showcase 1 31-01-2003 22:19
A quick question Joelster Technical Discussion 6 10-02-2002 09:09
quick question about qualifying points Hymnson General Forum 4 25-09-2001 21:19
Quick Question, i guess Andrew Rudolph Motors 1 12-08-2001 14:39


All times are GMT -5. The time now is 12:47.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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