View Single Post
  #1   Spotlight this post!  
Unread 24-02-2004, 16:41
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
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);
		}
	}
}
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)