Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Simple Autonomous Mode Example (http://www.chiefdelphi.com/forums/showthread.php?t=25965)

deltacoder1020 24-02-2004 16:41

Simple Autonomous Mode Example
 
Although you cannot just drop this in without any modifications (it wouldn't really do anything of use in its current form), this code does provide a good foundation for a simple blind-reckoning (no sensor input) autonomous mode. It is expandable to have as many steps as wanted, and simple to use for timing. It doesn't use interrupts for timing, if you want to do that instead, you'll have to implement it yourself. This code is only for teams that need to get off the ground because they don't quite understand autonomous, and want a working example they can play with. Essentially, this is just a simple state machine and timer combo.

Code:

//define as many states as you want here
//each state is a "step" of the program
#define AUTO_FIRST_STATE        1
#define AUTO_SECOND_STATE        2
#define AUTO_THIRD_STATE        3
#define AUTO_FINISHED                4

//A handy-dandy macro to convert a number of seconds into a number of 26.2ms ticks
#define SECS_TO_TICKS(a)        ( ((int)(a)) * 10000 / 262 )
//And milliseconds too:
#define MSECS_TO_TICKS(a)        ( ((int)(a)) * 10 / 262 )

void User_Autonomous_Code(void)
{
        static char AUTO_STATE = AUTO_FIRST_STATE;
        static long tickCount = 0;

        while (autonomous_mode)
        {
                  if (statusflag.NEW_SPI_DATA)
                    {
                        Getdata(&rxdata);
                        tickCount++;        //tickCount contains the number of
                                        //26.2ms "ticks" since the start of the current step

                        switch (AUTO_STATE)
                        {
                                case AUTO_FIRST_STATE:
                                        //for demostration purposes, make the robot go forward
                                        pwm01 = pwm02 = 254;

                                        //are we done with this step yet?
                                        //(in this example, the first step goes for 2 seconds)
                                        if(tickCount >= SECS_TO_TICKS(2))
                                        {
                                                //make us go to the next step
                                                AUTO_STATE = AUTO_SECOND_STATE;
                                                //reset the step timer
                                                tickCount = 0;
                                        }
                                        break;
                                case AUTO_SECOND_STATE:
                                        //in this demo, the second step will be to spin
                                        pwm01 = 254; pwm02 = 0;

                                        //are we done with this step yet?
                                        //this time, we'll spin for 0.7 seconds = 700ms
                                        if(tickCount >= MSECS_TO_TICKS(700))
                                        {
                                                AUTO_STATE = AUTO_THIRD_STATE;
                                                tickCount = 0;
                                        }
                                        break;
                                case AUTO_THIRD_STATE:
                                        //finally, we'll go forward again
                                        pwm01 = pwm02 = 254;

                                        //done yet?
                                        //going forward for 4 seconds this time
                                        if(tickCount >= SECS_TO_TICKS(4))
                                        {
                                                AUTO_STATE = AUTO_FINISHED;
                                                tickCount = 0;
                                        }
                                        break;
                                case AUTO_FINISHED:
                                        pwm01 = pwm02 = 127;
                                        break;
                                default:
                                        pwm01 = pwm02 = 127;
                        }
                        Putdata(&txdata);
                        Generate_Pwms(pwm13,pwm14,pwm15,pwm16);
                }
        }
}


Astronouth7303 24-02-2004 16:45

Re: Simple Autonomous Mode Example
 
You know, I'm doing basically the same thing using simpler code. Plus, You can write REALLY LONG scripts without REALLY lengthening the code. I'll Release my source about a week or 2 after regionals.

Hint: Arrays

deltacoder1020 24-02-2004 16:50

Re: Simple Autonomous Mode Example
 
Quote:

Originally Posted by Astronouth7303
You know, I'm doing basically the same thing using simpler code. Plus, You can write REALLY LONG scripts without REALLY lengthening the code. I'll Release my source about a week or 2 after regionals.

Hint: Arrays

arrays indeed - if i myself were actually using dead reckoning, chances are I'd be using the approach I'm guessing you're hinting at. In this instance, however, I'm really just trying to make the code as straightforward as possible, so this seemed like a better idea. :)

deltacoder1020 08-03-2004 20:13

Re: Simple Autonomous Mode Example
 
A NOTE TO ANYONE USING THE ABOVE CODE:

I made a slight error in the definitions of the SECS_TO_TICKS and MSECS_TO_TICKS macros. You'll need to change them to the following:

Code:

#define SECS_TO_TICKS(a)        ( ((long)(a)) * 10000 / 262 )
//And milliseconds too:
#define MSECS_TO_TICKS(a)        ( ((long)(a)) * 10 / 262 )

otherwise, the values will overflow for a value greater than about 3 seconds.

Ryan M. 08-03-2004 20:16

Re: Simple Autonomous Mode Example
 
I'm sure rookie teams without much programming experience will find that helpful. :)

jacob_dilles 08-03-2004 20:22

Re: Simple Autonomous Mode Example
 
another simple auton example is our code... basicly it goes strait. then it turns about 75degrees. then it goes foward, opens its arms, and swings and knocks off the ball. it is based on a wheel counter inturpt... so you can make it go as fast or as slow as you want.
Code:

/********************************************************
************jacobs autonoumus code....********************
*******************************************************/
                  relay8_fwd = !rc_dig_in18;                          // Power pump only if pressure switch is on.
                  relay8_rev = 0;
//this start start it moving... ladies and gents start your engins
                if(count >= 0 && count < 2) {
                        pwm15 = 127 + 50;                        //drive strait
                        pwm16 = 127 + 50;                        //drive strait
//turn to get the ball
        if(count> 12 && count < 14) {
                foward = 0;                                        //turn off correction algoritiom (cuz were turning duh)
                        pwm15 = 127 + 50;                        //make right wheel turn fowards
                        pwm16 = 127 - 50;                        //make left wheel turn backwards
                        pwm01 = 254;                                //make arm go up
                }
                if(rc_dig_in16 == 1) {                                //make sure arm doesnt go to high!!!
                        pwm01 = 127;
                }
//then we have to go strait slightly to get to the ball
                if(count > 14 && count < 16) {
                        pwm15 = 127 + 50;                        //go foward
                        pwm16 = 127 + 50;                        //go foward
                        relay3_fwd = 1;                                //open the gripers...
                        relay3_rev = 0;                                //open the grpiers...
                }
//now turn to AND close the gippers to knock/grab ball off... combo move HI-YAH
                if(count > 18 && count < 23) {
                        pwm15 = 127 - 50;                        //turn the other way
                        pwm16 = 127 + 50;                        //turn the other way
                        relay3_fwd = 0;                                //close grippers
                        relay3_rev = 1;                                //close grippers
                       
                }

//NOW we shut down motors
        if(count > 24 && count < 100) {
                pwm15 = 127;                                        //turn the other way
                pwm16 = 127;                                        //turn the other way
                }       
/***********************************************************
*******************end jacobs auton code******************
***********************************************************/

as for the inturpst... that is not as hard as every one is making them out to be. there is a great white paper called inturpts for dummies, i sugjest you read it if your intrested. ive never done them before and it worked very well.


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

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