void tick_task(const uint8_t tick_type) { static int8_t message_counter = 0; if(tick_type == TICK_TASK_TYPE_CALCULATION) { update_sensor_data(); if(update_sensor_counter == 5) { imu_blinkenlights(); } for(uint8_t i = 0; i < IMU_PERIOD_NUM; i++) { if(imu_period_counter[i] < UINT32_MAX) { imu_period_counter[i]++; } } } else if(tick_type == TICK_TASK_TYPE_MESSAGE) { if(usb_first_connection && !usbd_hal_is_disabled(IN_EP)) { message_counter++; if(message_counter >= 100) { message_counter = 0; if(brick_init_enumeration(COM_USB)) { com_info.current = COM_USB; usb_first_connection = false; message_counter = 0; } } } for(uint8_t i = 0; i < IMU_PERIOD_NUM; i++) { if((imu_period[i] != 0) && (imu_period[i] <= imu_period_counter[i])) { // Test if we are totally out of time (lost a whole // period), in this case we don't send the callback again. // This is needed for the wireless extensions if(imu_period[i]*2 <= imu_period_counter[i]) { imu_period_counter[i] = imu_period[i]; } make_period_callback(i); } } } }
void tick_task(const uint8_t tick_type) { static int8_t message_counter = 0; if(tick_type == TICK_TASK_TYPE_MESSAGE) { if(usb_first_connection && !usbd_hal_is_disabled(IN_EP)) { message_counter++; if(message_counter >= 100) { message_counter = 0; if(brick_init_enumeration(COM_USB)) { com_info.current = COM_USB; usb_first_connection = false; message_counter = 0; } } } } if(tick_type == TICK_TASK_TYPE_CALCULATION) { dc_tick_calc_counter++; dc_current_sum += adc_channel_get_data(CURRENT_CONSUMPTION_CHANNEL); if(dc_tick_calc_counter >= 100) { dc_current = dc_current_sum/100; dc_current_sum = 0; dc_tick_calc_counter = 0; } dc_tick_counter++; // Switch Output Voltage between extern and stack if(dc_get_external_voltage() < DC_VOLTAGE_EPSILON) { PIO_Set(&pin_voltage_switch); } else { PIO_Clear(&pin_voltage_switch); } if(!dc_enabled) { return; } // Emit current velocity callback if necessary if((dc_current_velocity_period != 0) && ((dc_tick_counter % dc_current_velocity_period) == 0)) { dc_current_velocity = true; } //if(!encoder_enabled) { if(dc_velocity_goal == dc_velocity) { return; } //} if(dc_acceleration == 0) { dc_velocity = dc_velocity_goal; } else { if(dc_velocity < dc_velocity_goal) { dc_velocity = MIN(dc_velocity + dc_acceleration, dc_velocity_goal); } else { dc_velocity = MAX(dc_velocity - dc_acceleration, dc_velocity_goal); } } #ifdef ENCODER if(encoder_enabled) { if(encoder_tick()) { float setpoint = (dc_velocity/DC_VELOCITY_MULTIPLIER)*encoder_counts_per_revolution*pid.sample_time/(1000*60); float out; if(pid_compute(&pid, setpoint, (float)ABS(encoder_count_last), &out)) { pid_velocity = (int32_t)out; // pid_print(&pid); } } } #endif // Set new velocity dc_velocity_to_pwm(); // Emit velocity reachead callback if(dc_velocity_goal == dc_velocity /*&& !encoder_enabled*/) { dc_velocity_reached = true; } } else if(tick_type == TICK_TASK_TYPE_MESSAGE) { dc_message_tick_counter++; if(dc_velocity_reached) { dc_velocity_reached = false; dc_velocity_reached_callback(); } if(dc_current_velocity) { dc_current_velocity = false; dc_current_velocity_callback(); } dc_check_error_callbacks(); } }