Log in

View Full Version : Help Autonomous code not working


team550
14-01-2008, 09:43
/************************************************** *****************************
* FILE NAME: user_routines_fast.c <FRC VERSION>
*
* DESCRIPTION:
* This file is where the user can add their custom code within the framework
* of the routines below.
*
* USAGE:
* You can either modify this file to fit your needs, or remove it from your
* project and replace it with a modified copy.
*
* OPTIONS: Interrupts are disabled and not used by default.
*
************************************************** *****************************/

#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "user_routines.h"
#include "user_Serialdrv.h"


/*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/

#define FORWARD 0

#define RIGHT 1

#define LEFT 2

#define BACKWARD 3

#define SLOW_FORWARD 5

static unsigned int delay;

static unsigned int direction;

/************************************************** *****************************
* FUNCTION NAME: InterruptVectorLow
* PURPOSE: Low priority interrupt vector
* CALLED FROM: nowhere by default
* ARGUMENTS: none
* RETURNS: void
* DO NOT MODIFY OR DELETE THIS FUNCTION
************************************************** *****************************/
#pragma code InterruptVectorLow = LOW_INT_VECTOR
void InterruptVectorLow (void)
{
_asm
goto InterruptHandlerLow /*jump to interrupt routine*/
_endasm
}


/************************************************** *****************************
* FUNCTION NAME: InterruptHandlerLow
* PURPOSE: Low priority interrupt handler
* If you want to use these external low priority interrupts or any of the
* peripheral interrupts then you must enable them in your initialization
* routine. Innovation First, Inc. will not provide support for using these
* interrupts, so be careful. There is great potential for glitchy code if good
* interrupt programming practices are not followed. Especially read p. 28 of
* the "MPLAB(R) C18 C Compiler User's Guide" for information on context saving.
* CALLED FROM: this file, InterruptVectorLow routine
* ARGUMENTS: none
* RETURNS: void
************************************************** *****************************/
#pragma code
#pragma interruptlow InterruptHandlerLow save=PROD /* You may want to save additional symbols. */

void InterruptHandlerLow ()
{
unsigned char int_byte;
if (INTCON3bits.INT2IF && INTCON3bits.INT2IE) /* The INT2 pin is RB2/DIG I/O 1. */
{
INTCON3bits.INT2IF = 0;
}
else if (INTCON3bits.INT3IF && INTCON3bits.INT3IE) /* The INT3 pin is RB3/DIG I/O 2. */
{
INTCON3bits.INT3IF = 0;
}
else if (INTCONbits.RBIF && INTCONbits.RBIE) /* DIG I/O 3-6 (RB4, RB5, RB6, or RB7) changed. */
{
int_byte = PORTB; /* You must read or write to PORTB */
INTCONbits.RBIF = 0; /* and clear the interrupt flag */
} /* to clear the interrupt condition. */
else
{
CheckUartInts(); /* For Dynamic Debug Tool or buffered printf features. */
}
}


/************************************************** *****************************
* FUNCTION NAME: User_Autonomous_Code
* PURPOSE: Execute user's code during autonomous robot operation.
* You should modify this routine by adding code which you wish to run in
* autonomous mode. It will be executed every program loop, and not
* wait for or use any data from the Operator Interface.
* CALLED FROM: main.c file, main() routine when in Autonomous mode
* ARGUMENTS: none
* RETURNS: void
************************************************** *****************************/
void User_Autonomous_Code(void)

// Base Level
{
/* Initialize all PWMs and Relays when entering Autonomous mode, or else it
will be stuck with the last values mapped from the joysticks. Remember,
even when Disabled it is reading inputs from the Operator Interface.
*/
pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;
relay1_fwd = relay1_rev = relay2_fwd = relay2_rev = 0;
relay3_fwd = relay3_rev = relay4_fwd = relay4_rev = 0;
relay5_fwd = relay5_rev = relay6_fwd = relay6_rev = 0;
relay7_fwd = relay7_rev = relay8_fwd = relay8_rev = 0;

while (autonomous_mode) /* DO NOT CHANGE! */
// Autonnmous mode Level
{
direction = FORWARD; /* initial jump for switch */
delay = 191; /* 5 second delay (191 * .0262ms = ~5s) */

if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
// Get and Send Data Level
{

Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */

/* Add your own autonomous code here. */

/************************************************** *****************************

* FUNCTION NAME: User_Autonomous_Code

* PURPOSE: Execute user's code during autonomous robot operation.

* You should modify this routine by adding code which you wish to run in

* autonomous mode. It will be executed every program loop, and not

* wait for or use any data from the Operator Interface.

* CALLED FROM: main.c file, main() routine when in Autonomous mode

* ARGUMENTS: none

* RETURNS: void

************************************************** *****************************/



/*
This code demostrates how to move a robot in autonomous mode. When invoked,

the robot will move forward for 5 seconds, turn right for 1 second, backup

for 2 seconds, turn left for 1 second then continue forward at a slow speed

until autonomous mode is deactivated.



Two motors should be hooked up to pwms 4 and 3. The wiring on one motor should

be swapped in reference to the other motor (on the victor output terminals).

When autonomous mode is complete, pwm 4 and pwm 3 are set to neutral.

*/

switch (direction)

// Switch Level
{

case FORWARD:

do {pwm04 = pwm03 = 127 + 60;
delay --;}

while (delay !=0)

direction = RIGHT;

delay = 38; /* 1 second delay (38 * .0262ms = ~1s) */

break;

case RIGHT:

pwm04 = 127 - 40; /* do a slow right turn */

pwm03 = 127 + 40;

delay--;

if (delay == 0)

{

direction = BACKWARD;

delay = 76; /* 2 second delay (76 * .0262ms = ~2s) */

}

break;

case BACKWARD:

pwm04 = 127 - 50; /* go backwards for 2 seconds */

pwm03 = 127 - 50;

delay--;

if (delay == 0)

{

direction = LEFT;

delay = 38; /* 1 second delay (38 * .0262ms = ~1s) */

}

break;

case LEFT:

pwm04 = 127 + 40; /* do a slow left turn */

pwm03 = 127 - 40;

delay--;

if (delay == 0)

{

direction = SLOW_FORWARD;

delay = 1; /* immediately go forward at a slow rate */

}

break;

case SLOW_FORWARD:

pwm04 = pwm03 = 127 + 30; /* go forward slowly */

break;

} /* end SWITCH secton */

/* Generate_Pwms(pwm13,pwm14,pwm15,pwm16); - not needed if your not using pwms 13-16 */

Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */

} /* status flag section */

pwm04 = 127; /* set to neutral after autonomous mode */

pwm03 = 127;

}

Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */

}

/* Putdata(&txdata);*/ /* DO NOT DELETE, or you will get no PWM outputs! */


/************************************************** *****************************
* FUNCTION NAME: Process_Data_From_Local_IO
* PURPOSE: Execute user's realtime code.
* You should modify this routine by adding code which you wish to run fast.
* It will be executed every program loop, and not wait for fresh data
* from the Operator Interface.
* CALLED FROM: main.c
* ARGUMENTS: none
* RETURNS: void
************************************************** *****************************/
void Process_Data_From_Local_IO(void)
{
/* Add code here that you want to be executed every program loop. */

}

/************************************************** *****************************
* FUNCTION NAME: Serial_Char_Callback
* PURPOSE: Interrupt handler for the TTL_PORT.
* CALLED FROM: user_SerialDrv.c
* ARGUMENTS:
* Argument Type IO Description
* -------- ---- -- -----------
* data unsigned char I Data received from the TTL_PORT
* RETURNS: void
************************************************** *****************************/

void Serial_Char_Callback(unsigned char data)
{
/* Add code to handle incomming data (remember, interrupts are still active) */
}


/************************************************** ****************************/
/************************************************** ****************************/
/************************************************** ****************************/

that is our autonomus code and it appears it should work fine but doesnt
any help or suggestions is greatly appriciated

Alan Anderson
14-01-2008, 09:55
1: You say it doesn't work, but that isn't enough information. Tell us what you expect it to do, and tell us what it's doing instead.

2: Put code between [ code ] and [ /code ] tags when you post it here. It will preserve the formatting.

3: Don't use delay loops the way it looks like you're trying to. If you take too long before calling the Putdata() function, the master CPU assumes your code is hung and signals a code error. You'll need to figure out how to execute the complete while (autonomous_mode) loop without pausing in the middle.

team550
14-01-2008, 09:58
we are trying to make the bot go forward constantly but have the ir have control over the autonomus code

Kevin Sevcik
14-01-2008, 10:10
I'm a little confused, or possibly you are. All of your cases in your big switch statement there are appropriately written to let the robot framework do your looping and timing.... Except the very first case, FORWARD. You have a do-while loop there that will process in about 1ms and then kick you straight into the right turn. If you change this first case from using a fruitless do-while to the proper if(delay == 0) structure of all your other cases, I think your code might work. At the very least the FORWARD case would work.

seanl
14-01-2008, 10:33
you cant do the delay like that it would delay the 26 ms. loop time which in turn would cause a code error on the robot. i figured that out in the summer. the only way you could pull off a delay is by adding a global vaiable that counts the number of iterations of the while loop in the autonomous code.

1. declare a variable at the top
2. put a (variable)++ in the loop
3. use the variable as a counter for your delay.

team550
14-01-2008, 10:35
i thank you we moved the initial if statement above the while and that fixed the code