Go to Post FIRST is like pizza. You and I may like different toppings, but in the end it's still delicious pizza. - Billfred [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 24-03-2004, 19:47
Robohawk-master's Avatar
Robohawk-master Robohawk-master is offline
Registered User
AKA: Barry
#1109 (Robohawks)
Team Role: Operator
 
Join Date: Mar 2004
Rookie Year: 2003
Location: Lively, Ontario
Posts: 21
Robohawk-master is on a distinguished road
More automonous help:ending stuff

OK! I got a line sensor code (verrrrrry basic) and I would like to now a few things:

1. I know the robot will arrive at the T in under 15 secs, so how do I stop it and do something different?

2. If I stop it, we would like to raise our arm (on winch), which is on a PWM.

3. How would I stop the arm? Same way as # 1?

Code:
 
if (rc_dig_in01 == 0 && rc_dig_in02 == 0 && rc_dig_in03 == 0)
			{
				pwm13 = 127;
				pwm15 = 127;
			}
			else if (rc_dig_in01 == 0 && rc_dig_in02 == 0 && rc_dig_in03 == 1)
			{
				pwm13 = 139;
				pwm15 = 175;
			}
			else if (rc_dig_in01 == 1 && rc_dig_in02 == 0 && rc_dig_in03 == 0)
			{
				pwm13 = 175;
				pwm15 = 139;
			}
			else if (rc_dig_in01 == 0 && rc_dig_in02 == 1 && rc_dig_in03 == 0)
			{
				pwm13 = 249;
				pwm15 = 249;
			}
__________________
- Barry -
-2004 Canadian Regional Semi-Finalists
-2004 Bruce Power Safety Award
  #2   Spotlight this post!  
Unread 25-03-2004, 00:12
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: More automonous help:ending stuff

Ok...well it's difficult to tell you exactly what to do without knowing your robot, but I'll try. There isn't a timing component in your code, it looks like, it just follows the line as far as it can, turning motors to different values when certain sensors see the line. Without some form of timing you won't be able to tell it to stop when you want, or time when the arm raises up/stops.

The simplest form of timer you can have is a program cycle counter, which will operate *around* 40hz, or about 25ms per count (its actually more like 28.something). So, add in a variable "count" (int count = 0; in your initialization) somewhere, and at the end of the loop say "count++;". Then, check when the count is higher than a certain number (the time you want to stop, or raise the arm, or stop raising the arm), and set the pwm values to something different that you want. Make sure to set those values lower in the loop than the sensor code, or they will get overridden.

You'll probably just have to time it yourself, or count the number of times a certain sensor sees the line (as in did it just turn?), and then raise the arm.

There are better ways of controlling the robot based on time and other factors, but its probably more complicated than you really have time for right now. Hope I helped.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #3   Spotlight this post!  
Unread 25-03-2004, 11:33
Robohawk-master's Avatar
Robohawk-master Robohawk-master is offline
Registered User
AKA: Barry
#1109 (Robohawks)
Team Role: Operator
 
Join Date: Mar 2004
Rookie Year: 2003
Location: Lively, Ontario
Posts: 21
Robohawk-master is on a distinguished road
Re: More automonous help:ending stuff

Thanks, but can you be more specific. Put timer where in ins? Could you give and example, I can change the code but I can't write it...++ means add twice to me
__________________
- Barry -
-2004 Canadian Regional Semi-Finalists
-2004 Bruce Power Safety Award
  #4   Spotlight this post!  
Unread 25-03-2004, 14:44
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: More automonous help:ending stuff

In your User_Initialization create a variable called count (you can create it anywhere but that's probably the best spot for now), with this syntax:

int count = 0;

Then at the bottom of the while(autonomous_mode) loop in your code, right above Putdata(&txdata), put in

count++;

++ in C means the same thing as "count = count + 1;", so you are incrementing it by one every time you pass through that loop. Multiply count by 40 and you have *approximately* the amount of time autonomous has been running. You'll have to time it and do the math yourself, but when you want it to break off, say something like (below your line following code):

if(count > 200){
pwm01 = 254;
pwm02 = 254;
}
else if(count > 300){
pwm01 = 0;
pwm02 = 0;
}
else if (count > 320){
pwm01 = 127;
pwm02 = 127;
}

That won't do much, just go forward and stop after a little over 2 seconds, but hopefully you get the picture on what you can do. Adjust values to your desire, and good luck.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #5   Spotlight this post!  
Unread 28-03-2004, 17:51
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: More automonous help:ending stuff

I think you'll need to declare that int as 'static' -- otherwise it won't be remembered from one cycle to the next:

Code:
static int count = 0;
Also, if you want to declare it in one place and increment it in another you will need to declare it as global, which means outside of all functions:

Code:
// this is global:
static int count = 0;

User_Initialization()
{
  static int anotherCount = 0;  // this is local to this function, 
                                         // and cannot be seen outside of
                                         // this fuction.

  
  count++;  // okay
  anotherCount++;  // okay
  
}

anotherFunctionInTheSameFile()
{
  count++;  // okay -- it's global
  anotherCount++;  // won't compile
}
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #6   Spotlight this post!  
Unread 28-03-2004, 18:42
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: More automonous help:ending stuff

Barry,

Let me reply to your first question. I created an auto mode with a "sequence of events" that were timed. I created two files, 'myAuto.c' and 'myAuto.h' (not really -- I'm simplifying). (BTW, the code examples below haven't been compiled and they probably have at least typo bugs.)

In the .h file I made a list of named states using the C 'enum' construct:

Code:
// myAuto.h

#ifndef MYAUTO_H  // don't include it more than once!
#define MYAUTO_H

enum  // auton state machine states
{
  FIRST_CYCLE,
  RUN_TO_TEE,
  RAISE_ARM,
  STOP
}
C assigns each one of those state names an integer value, probably starting with zero. I show them in time-sequence but that doesn't matter. By convention, these are all UPPERCASE, but they don't have to be.

Also in myAuto.h I #define some constants.

Code:
// machine cycles at about 38 Hz.
// exact rate is 10 MHz / 2^18

#define RUN_TO_TEE_TIME  380  // about 10 seconds ( 38 cycles / sec)
#define RAISE_ARM_RUN_MOTOR_TIME  80  // ~2+ secs

void
runAndRaise( void );  // advertise the function we will create in myAUto.c
                           // important for calling this function from another file
                           // that file will need "#include myAuto.h" so it
                          // knows the form of "runAndRase"

#endif  // MYAUTO_H
(Naming constants is good practice for organizing your code. It's bad practice to put "naked constants" in your code, for at least three reasons: first, if one constant needs to appear more than once and you need to change it, you have to change it at each place (and you might forget one). Using #define you can change it in just one place and all uses change at once. Second, if you mis-type a constant, the compiler won't catch it (all numbers are valid) but if you mis-type a word you'll probably create something that the compiler can't figure out, and the compiler will catch your mistake for you. And the third reason is the word can say what the number represents (RUN_TO_TEE_TIME), so it is self-documenting.)

Okay, myAuto.c has a static int counter that counts each timed phase. Something like this:

Code:
// myAuto.c
//

#include "myAuto.h"  // pick up the enum and #defines

void
runAndRaise( void )  // call this function from 
                   // the file user_routines_fast.c 
                   //   inside  User_Autonomous_Code() ,
                   //     inside the  "if (Clock > Old_Clock)" conditional
                   // don't forget to put #include "myAuto.h" at the top of
                   // user_routines_fast.c 
{

  static int timer = 0;  // times each phase (state) of auton.
  static unsigned char state = FIRST_CYCLE;  // init to first state.

  switch( state )  // executes once every .0262144 seconds
  {
    case FIRST_CYCLE:  // let's initialize some things
      timer = 0;
      state = RUN_TO_TEE;  // next time around, jump to this state
      break; // that's all for this cycle.

    case RUN_TO_TEE:
      timer++;
      runMotorsAndStuff();  // your code goes here
      if ( timer > RUN_TO_TEE_TIME )
      {
        timer = 0;  // reset timer for use in next state
        stopMotors();  // your code goes here
        runArmWinch();  // your code goes here
        state = RAISE_ARM;  // next time around jump to this state
      }
      break;

    case RAISE_ARM:
      timer++;
      if( timer > RAISE_ARM_RUN_MOTOR_TIME )
      {
        timer = 0;  // for consistency: not actually needed by next state.
        stopArmWinch();  // your code goes here
        state = STOP; // next state is stop
      }
      break;

    case STOP:
      // nothing: just sit there until end of auto.
      break;

  }  // switch( state )

  
}
The "case" statements are shown in time sequence but it doesn't matter. Don't forget the "break" statement at the end of each case, otherwise the execution will just keep going into the code for the next case statement.

Hope that helps!

-Norm
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.

Last edited by gnormhurst : 28-03-2004 at 19:00.
  #7   Spotlight this post!  
Unread 29-03-2004, 09:41
Robohawk-master's Avatar
Robohawk-master Robohawk-master is offline
Registered User
AKA: Barry
#1109 (Robohawks)
Team Role: Operator
 
Join Date: Mar 2004
Rookie Year: 2003
Location: Lively, Ontario
Posts: 21
Robohawk-master is on a distinguished road
Re: More automonous help:ending stuff

Wow, I get a head ache reading all that... but I think I get it.

1. Put that static count in

2. Add the PWM for the winch on the timer

3. Calculate the figures

One more thing. If say at 2000 I tell PWM 13 & 15 to shut off and the at 2000 tell PWM 3 (winch) to raise till (lets say) 2750 will the timer interfere with any of the line sensors? Does the position of the timer code affect it?
__________________
- Barry -
-2004 Canadian Regional Semi-Finalists
-2004 Bruce Power Safety Award

Last edited by Robohawk-master : 29-03-2004 at 09:43.
  #8   Spotlight this post!  
Unread 29-03-2004, 11:05
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: More automonous help:ending stuff

Quote:
Originally Posted by gnormhurst
I think you'll need to declare that int as 'static' -- otherwise it won't be remembered from one cycle to the next:

Code:
static int count = 0;
The static isn't necesary if it is gloabal. Static on a gloabal variable in C is completely different, as it limits the "global" variable's scope to only the file it is declared in. However, if you aren't using it from a different file, you won't be able to tell the difference.
__________________

  #9   Spotlight this post!  
Unread 29-03-2004, 11:07
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: More automonous help:ending stuff

Quote:
Originally Posted by Robohawk-master
Wow, I get a head ache reading all that... but I think I get it.

1. Put that static count in

2. Add the PWM for the winch on the timer

3. Calculate the figures

One more thing. If say at 2000 I tell PWM 13 & 15 to shut off and the at 2000 tell PWM 3 (winch) to raise till (lets say) 2750 will the timer interfere with any of the line sensors? Does the position of the timer code affect it?
Oh, yeah...

I haven't really been following along completely, but unless the line sensors/PWM part try modify what the timer is at, you should be fine.
__________________

  #10   Spotlight this post!  
Unread 29-03-2004, 18:10
Robohawk-master's Avatar
Robohawk-master Robohawk-master is offline
Registered User
AKA: Barry
#1109 (Robohawks)
Team Role: Operator
 
Join Date: Mar 2004
Rookie Year: 2003
Location: Lively, Ontario
Posts: 21
Robohawk-master is on a distinguished road
Post Re: More automonous help:ending stuff

Here is the final code (drum roll please)

Code:
	if (rc_dig_in01 == 0 && rc_dig_in02 == 0 && rc_dig_in03 == 0)
			{
				pwm13 = 127;
				pwm15 = 127;
			}
			else if (rc_dig_in01 == 0 && rc_dig_in02 == 0 && rc_dig_in03 == 1)
			{
				pwm13 = 139;
				pwm15 = 175;
			}
			else if (rc_dig_in01 == 1 && rc_dig_in02 == 0 && rc_dig_in03 == 0)
			{
				pwm13 = 175;
				pwm15 = 139;
			}
			else if (rc_dig_in01 == 0 && rc_dig_in02 == 1 && rc_dig_in03 == 0)
			{
				pwm13 = 249;
				pwm15 = 249;
			} 
			else if (count > 1000){
				pwm13 = 127;
				pwm15 = 127;
				pwm3 = 175;
			}
			else if (count > 1005){
				pwm3 = 127;
			}                        /* Hey! There are 3435 loops in 15 minutes */
If some one sees a flaw, PLEASE tell me, the Canadian Regional is on Wednesday.

One more thing... I attached a screenshot of where I placed the counter (in the user instalizationz). I this the right spot?

Thank you all who answered my questions...This site has been our only programming resource for our team.
__________________
- Barry -
-2004 Canadian Regional Semi-Finalists
-2004 Bruce Power Safety Award

Last edited by Robohawk-master : 29-03-2004 at 18:15.
  #11   Spotlight this post!  
Unread 29-03-2004, 18:59
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: More automonous help:ending stuff

Quote:
If some one sees a flaw, PLEASE tell me, the Canadian Regional is on Wednesday.

One more thing... I attached a screenshot of where I placed the counter (in the user instalizationz). I this the right spot?
Don't think so. Instead, put it in the same function that your code above is in (where is that, BTW?) And make sure it's static:
Code:
static int count = 0;
And don't forget to increment count somewhere in the function. I can't tell you how many times I wrote code like this and pulled my hair out trying to find the bug, which turned out to be that I had forgotten to increment the counter:
Code:
  count++;
Another thing: What are those count values? 1000? 1005? You comment that
Code:
/* Hey! There are 3435 loops in 15 minutes */
but you must mean seconds, not minutes. But there are only about 570 loops in 15 seconds (38 * 15). And how long were you planning to run the arm? 1005 - 1000? That's only 5 cycles, or about 0.13 second. I doubt you'd even see it move! Especially since autonomous would be over 11 seconds before it is scheduled to start.

Let's say you want to start the arm after 12 seconds and run it for 2 seconds. Change "1000" to "456" (38 * 12) and "1005" to 532 (38 * (12+2) ).

Does that make sense?



I take it that this is a line-following routine, and the three inputs are three line sensors(?). Consider that your timing test
Code:
else if (count > 1000){
will only be tested if all of the preceding tests fail. So if any of the sensors sees something, it will prevent the arm from ever running.

How about wrapping the tests on the sensors inside an "if" statement so that they only run if the time is not yet up to a certain point:

Code:
  if ( count < 456 )  // still less than 12 seconds have elapsed
  {
    // all the if's for the sensors go here
  }
  else if ( count < 532 )  // 12 seconds have gone by, but less than 14
  {
    // stop wheels
    pwm13 = 127;
    pwm15 = 127;

    // raise arm
    pwm3 = 175;  // I think it's pwm03, not pwm3.  check it.
  }
  else
  {
    // stop raising arm
    pwm3 = 127;  // I think it's pwm03, not pwm3.  check it.
  }

BTW, your algorithm (for line following?) seems to have a case where it will stop the motors:
Code:
	if (rc_dig_in01 == 0 && rc_dig_in02 == 0 && rc_dig_in03 == 0)
			{
				pwm13 = 127;
				pwm15 = 127;
			}
Does that mean, "if you don't see the line at all, stop."? How will it ever find the line again? I'm probably not understanding what the sensors do.

-norm
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #12   Spotlight this post!  
Unread 30-03-2004, 14:07
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: More automonous help:ending stuff

Quote:
Originally Posted by Texan
The static isn't necesary if it is gloabal. Static on a gloabal variable in C is completely different, as it limits the "global" variable's scope to only the file it is declared in. However, if you aren't using it from a different file, you won't be able to tell the difference.
Thanks for that correction. That's the great thing about C: there's always some odd thing to learn.

Makes me want to cross-post this in the "Why I hate C" thread.
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #13   Spotlight this post!  
Unread 30-03-2004, 14:31
TedP TedP is offline
Registered User
#1014
 
Join Date: Mar 2004
Location: Dublin, OH
Posts: 19
TedP will become famous soon enough
Re: More automonous help:ending stuff

There was already a message here which showed how to do some simple state-based autonomous code. THIS IS THE RIGHT WAY TO DO THINGS.

Your autonomous code effectively moves through a series of states. You stay in a state until ready to transition out of this state. Each state have have specific outputs (like a "Moore machine") or have outputs which directly depend on inputs (like a "Mealey/Mealy machine"). There is no reason to have to make your outputs entirely Mealy. Add a state variable -- it really does help.

A simple example can also be found in one of our team's (1014) code:

http://www.osufirst.org/twiki/bin/vi...04RegionalCode

If you look in the user_routines_fast.c in the autonomous section, you'll see the whole thing is based on state. Because we have position feedback, we transit out of states once we reach a particular position. However, this could just as easily be time.

In general, it's nice to setup an interrupted-based timer that keeps a moderately real-time clock running on your machine and to use that as feedback timing your operations and calculating important characteristics like speed.

Keep your code in terms of state and you'll have a much nicer time coding, especially when adding states, maintaining states, and doing simple things like stopping.

Sorry that the 1014 autonomous code has little commenting -- one of the nice things about the states (and the speed controllers) is that we could write our autonomous code on the fly at competition and it was easy to debug there. We never intended to have autonomous code... we just felt it was good to add it in since so few robots at our regional had any.
  #14   Spotlight this post!  
Unread 30-03-2004, 15: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: More automonous help:ending stuff

Just looked at my code way up there and realized that i had >'s instead of <'s...sorry for the confusion.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #15   Spotlight this post!  
Unread 30-03-2004, 19:59
Robohawk-master's Avatar
Robohawk-master Robohawk-master is offline
Registered User
AKA: Barry
#1109 (Robohawks)
Team Role: Operator
 
Join Date: Mar 2004
Rookie Year: 2003
Location: Lively, Ontario
Posts: 21
Robohawk-master is on a distinguished road
Re: More automonous help:ending stuff

HI,
I don't have time to write something lengthly, so I will write one thing:

- I placed count++ whatever in user_routines. Is this the right spot?

Canadian regoinal starts THURS but we leave TOMORROW, but our hotel has internet so post the answer please
__________________
- Barry -
-2004 Canadian Regional Semi-Finalists
-2004 Bruce Power Safety Award
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
Please - stop posting needless stuff! Andy Baker Announcements 0 13-01-2004 09:13
Free stuff at FIRST Not2B General Forum 7 30-09-2003 21:23
What does everyone like to do besides FIRST stuff?? Katie_269 Chit-Chat 45 17-07-2002 17:01
who can we buy stuff from?!?! archiver 2001 7 23-06-2002 23:04
How much robotics stuff is on your resumes??? Robby O Career 14 03-08-2001 19:51


All times are GMT -5. The time now is 03:09.

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