/** \brief This is the task function for the master task. * \details This function decides which state the system is in, based on the previous * state, and the current values of certain shared variables. The seven possible * system states are as follows: * * AWAITING_CAL - Awaiting Calibration - The system is waiting for the user to * press the target set button, to initialize the zero reference position. * Motor power: 0 * * CALIBRATION - Calibration State - The user is holding the target set button, * and controlling the mirror array with the joystick. Once the mirror array is * in the zero reference position, the user can release the button. * Motor power: Joystick ADC Reading * * AWAITING_TARG - Awaiting Target - The system is waiting for the user to * press the target set button, to initialize the first target. * Motor power: 0 * * SELECT_TARG - Target Selection - The user is holding the target set button, * and controlling the mirror array with the joystick. Once the sun is correctly * being redirected to the target, the user can release the button. * Motor power: Joystick ADC Reading * * TRACK_TARG - The mirror array is actively tracking the sun, and redirecting * the beam of light to the target. * Motor power: PI controller * * ERROR - The safety task has decided that an error has occurred, and the motors * are powered down in response. * Motor power: 0 * * IDLE - state which allows the user to reset the system from the ERROR state to * the SELECT_TARG state. * Motor power: 0 */ void task_master(void* pvParameters){ state_SHARED = AWAITING_CAL; uint8_t btn = 0; uint8_t error = 0; while(!btn) { vTaskDelay(configMS_TO_TICKS (200)); taskENTER_CRITICAL(); state_SHARED = AWAITING_CAL; btn = btn_SHARED; taskEXIT_CRITICAL(); } while(btn) { vTaskDelay(configMS_TO_TICKS (200)); taskENTER_CRITICAL(); state_SHARED = CALIBRATION; btn = btn_SHARED; taskEXIT_CRITICAL(); } while(!btn) { vTaskDelay(configMS_TO_TICKS (200)); taskENTER_CRITICAL(); state_SHARED = AWAITING_TARG; btn = btn_SHARED; taskEXIT_CRITICAL(); } portTickType xLastWakeTime; xLastWakeTime = xTaskGetTickCount(); while(1) { taskENTER_CRITICAL(); btn = btn_SHARED; error = safety_error_SHARED; taskEXIT_CRITICAL(); if(!error){ if(btn) { taskENTER_CRITICAL(); state_SHARED = SELECT_TARG; taskEXIT_CRITICAL(); } else { taskENTER_CRITICAL(); state_SHARED = TRACK_TARG; taskEXIT_CRITICAL(); } } else { state_SHARED = ERROR; } vTaskDelayUntil(&xLastWakeTime, 200/portTICK_RATE_MS); } }
void task_master::run (void) { // Make a variable which will hold times to use for precise task scheduling portTickType previousTicks = xTaskGetTickCount (); //shared_adc0_state.put(OFF); shared_adc1_state.put(READ_ONCE); //shared_adc2_state.put(OFF); shared_adc3_state.put(READ_ONCE); shared_motor1_state.put( RUN ); shared_motor2_state.put( RUN ); for (;;) { shared_motor1_power.put( shared_adc3_value.get() - 512); shared_motor2_power.put( shared_adc1_value.get() - 512); delay_from_to (previousTicks, configMS_TO_TICKS (100)); } }