/******************************************************************************* * FILE NAME: user_routines.c * * DESCRIPTION: * This file contains the default mappings of inputs * (like switches, joysticks, and buttons) to outputs on the RC. * * USAGE: * You can either modify this file to fit your needs, or remove it from your * project and replace it with a modified copy. * *******************************************************************************/ #include "ifi_aliases.h" #include "ifi_default.h" #include "ifi_utilities.h" #include "user_routines.h" #include "printf_lib.h" /*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/ /* EXAMPLES: (see MPLAB C18 User's Guide, p.9 for all types) unsigned char wheel_revolutions = 0; (can vary from 0 to 255) unsigned int delay_count = 7; (can vary from 0 to 65,535) int angle_deviation = 142; (can vary from -32,768 to 32,767) unsigned long very_big_counter = 0; (can vary from 0 to 4,294,967,295) */ static int ws_p1_y; static int ws_p1_x; static int ws_p2_y; static int ws_p2_x; static int ws_pwm01; static int ws_pwm02; static int ws_pwm03; static int ws_pwm04; #define MAX(variable, max_val) if (variable > (max_val)) variable = (max_val) #define MIN(variable, min_val) if (variable < (min_val)) variable = (min_val) #define MAX_RETURN(value, max_val) ((value) > (max_val) ? (max_val) : (value)) #define MIN_RETURN(value, min_val) ((value) < (min_val) ? (min_val) : (value)) static int labNum = 1; /* * Inputs: * Joystick 1 x => ws_p1_x * Joystick 1 y => ws_p1_y * Joystick 2 x => ws_p2_x * Joystick 2 y => ws_p2_y * * Joystick 1 trigger => p1_sw_trig * Joystick 1 top => p1_sw_top * Joystick 2 trigger => p2_sw_trig * Joystick 2 top => p2_sw_top * * Outputs: * PWM 1 => ws_pwm01 * PWM 2 => ws_pwm02 * PWM 3 => ws_pwm03 * PWM 4 => ws_pwm04 * * LED 1 => relay1_fwd * LED 2 => relay2_fwd * LED 3 => relay3_fwd * LED 4 => relay4_fwd * * PWM 1 GREEN LED => Pwm1_green * PWM 1 RED LED => Pwm1_red * PWM 2 GREEN LED => Pwm2_green * PWM 2 RED LED => Pwm2_red * SWITCH 1 LED => Switch1_LED * SWITCH 2 LED => Switch2_LED * SWITCH 3 LED => Switch3_LED */ void run_Lab1(void) { /**************************************** * Directly map PORT 1 Y to PWM 1 * Directly map PORT 2 X to PWM 2 * Directly map PORT 2 Y to PWM 3 ***************************************/ /* Add code here */ } void run_Lab2(void) { /**************************************** * Directly map PORT 2 X to PWM 1 * Map PORT 2 X to PWM 2 - Full range on stick is half range of output * Map PORT 2 X to PWM 3 - Half range on stick is full range of output ***************************************/ /* Add code here */ } void run_Lab3(void) { /**************************************** * Directly map PORT 1 Y to PWM1 * Directly map PORT 1 Y to PWM2 - PWM2 should never exceed 50% of max value * in either direction ***************************************/ /* Add code here */ } void run_Lab4(void) { /**************************************** * Turn PWM 1 GREEN LED on while PORT 1 trigger is held. Should be off * otherwise. * Turn PWM 1 RED LED off while PORT 1 top is held. Should be on otherwise. * Turn PWM 2 GREEN LED off while PORT 1 trigger is held. Should be on * otherwise. * Turn PWM 2 RED LED on while PORT 1 top is held. Should be off otherwise. ***************************************/ /* Add code here */ } void run_Lab5(void) { /**************************************** * Toggle LED 1 when PORT 1 top is pressed (press = only the instant the * button is first pushed) ***************************************/ /* Add code here */ } void run_Lab6(void) { /**************************************** * Add 10 to PWM 3 when PORT 2 trigger is pressed * Subtract 20 from PWM 3 when PORT 2 top is pressed * BONUS: Set PWM 3 to center when PORT 2 top and trigger are pressed at the * same time, reset on the release. ***************************************/ /* Add code here */ } void run_Lab7(void) { /**************************************** * Directly map PORT 1 Y to PWM1 * Directly map PORT 1 Y to PWM2 * If PORT 1 Trigger is pressed, PWM2 should change directions ***************************************/ /* Add code here */ } void run_Lab8(void) { /**************************************** * Directly map PORT 1 Y to PWM1 * Directly map PORT 1 Y to PWM2 - Should scale output by 50% unless PORT 1 * Top is pressed * Directly map PORT 2 Y to PWM3 * Directly map PORT 2 Y to PWM4 - Should scale output by 50% if PORT 2 * Top is pressed ***************************************/ /* Add code here */ } void run_Lab9(void) { /**************************************** * STATE A: SWITCH LED 1 ON * STATE B: SWITCH LED 2 ON * STATE C: SWITCH LED 3 ON * * INITIAL STATE = STATE A * Pressing PORT 1 trigger cycles through states * A->B->C->A ***************************************/ /* Add code here */ } void run_nothing(void) { ws_pwm01 = 0; ws_pwm02 = 0; ws_pwm03 = 0; ws_pwm04 = 0; ws_pwm04 = 0; relay1_fwd = 0; relay2_fwd = 0; relay3_fwd = 0; relay4_fwd = 0; Pwm1_green = 0; Pwm1_red = 0; Pwm2_green = 0; Pwm2_red = 0; Switch1_LED = 0; Switch2_LED = 0; Switch3_LED = 0; } /******************************************************************************* * FUNCTION NAME: User_Initialization * PURPOSE: This routine is called first (and only once) in the Main function. * You may modify and add to this function. * CALLED FROM: main.c * ARGUMENTS: none * RETURNS: void *******************************************************************************/ void User_Initialization (void) { rom const char *strptr = "IFI User Processor Initialized ..."; Set_Number_of_Analog_Channels(SIXTEEN_ANALOG); /* DO NOT CHANGE! */ /* FIRST: Set up the I/O pins you want to use as digital INPUTS. */ digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = INPUT; digital_io_05 = digital_io_06 = digital_io_07 = digital_io_08 = INPUT; digital_io_09 = digital_io_10 = digital_io_11 = digital_io_12 = INPUT; digital_io_13 = digital_io_14 = digital_io_15 = digital_io_16 = INPUT; digital_io_18 = INPUT; /* Used for pneumatic pressure switch. */ /* Note: digital_io_01 = digital_io_02 = ... digital_io_04 = INPUT; is the same as the following: digital_io_01 = INPUT; digital_io_02 = INPUT; ... digital_io_04 = INPUT; */ /* SECOND: Set up the I/O pins you want to use as digital OUTPUTS. */ digital_io_17 = OUTPUT; /* Example - Not used in Default Code. */ /* THIRD: Initialize the values on the digital outputs. */ rc_dig_out17 = 0; /* FOURTH: Set your initial PWM values. Neutral is 127. */ pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127; pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127; /* FIFTH: Set your PWM output types for PWM OUTPUTS 13-16. /* Choose from these parameters for PWM 13-16 respectively: */ /* IFI_PWM - Standard IFI PWM output generated with Generate_Pwms(...) */ /* USER_CCP - User can use PWM pin as digital I/O or CCP pin. */ Setup_PWM_Output_Type(IFI_PWM,IFI_PWM,IFI_PWM,IFI_PWM); /* Example: The following would generate a 40KHz PWM with a 50% duty cycle on the CCP2 pin: CCP2CON = 0x3C; PR2 = 0xF9; CCPR2L = 0x7F; T2CON = 0; T2CONbits.TMR2ON = 1; Setup_PWM_Output_Type(USER_CCP,IFI_PWM,IFI_PWM,IFI_PWM); */ /* Add any other initialization code here. */ Initialize_Serial_Comms(); Putdata(&txdata); /* DO NOT CHANGE! */ printf("%s\n", strptr); /* Optional - Print initialization message. */ User_Proc_Is_Ready(); /* DO NOT CHANGE! - last line of User_Initialization */ } /******************************************************************************* * FUNCTION NAME: Process_Data_From_Master_uP * PURPOSE: Executes every 26.2ms when it gets new data from the master * microprocessor. * CALLED FROM: main.c * ARGUMENTS: none * RETURNS: void *******************************************************************************/ void Process_Data_From_Master_uP(void) { Getdata(&rxdata); /* Get fresh data from the master microprocessor. */ /* Default_Routine(); */ /* shift input values so range is -127 to 127 and 0 is the middle */ ws_p1_y = (int)p1_y - 127; ws_p1_x = (int)p1_x - 127; ws_p2_y = (int)p2_y - 127; ws_p2_x = (int)p2_x - 127; switch (labNum) { case 1: run_Lab1(); break; case 2: run_Lab2(); break; case 3: run_Lab3(); break; case 4: run_Lab4(); break; case 5: run_Lab5(); break; case 6: run_Lab6(); break; case 7: run_Lab7(); break; case 8: run_Lab8(); break; case 9: run_Lab9(); break; default: run_nothing(); break; } /* make sure the outputs don't overflow */ MIN(ws_pwm01, (int)-127); MAX(ws_pwm01, (int)127); MIN(ws_pwm02, (int)-127); MAX(ws_pwm02, (int)127); MIN(ws_pwm03, (int)-127); MAX(ws_pwm03, (int)127); MIN(ws_pwm04, (int)-127); MAX(ws_pwm04, (int)127); /* shift output to match IFI pwm specs of 0 to 255 range */ pwm01 = ws_pwm01 + 127; pwm02 = ws_pwm02 + 127; pwm03 = ws_pwm03 + 127; pwm04 = ws_pwm04 + 127; /* printf("Port1 Y %3d, X %3d, Fire %d, Top %d\n",(int)p1_y,(int)p1_x,(int)p1_sw_trig,(int)p1_sw_top); */ Generate_Pwms(pwm13,pwm14,pwm15,pwm16); Putdata(&txdata); /* DO NOT CHANGE! */ } /******************************************************************************/ /******************************************************************************/ /******************************************************************************/