コード例 #1
0
ファイル: stepper.c プロジェクト: Nic2012/stepper-brick
void tick_task(uint8_t tick_type) {
	if(tick_type == TICK_TASK_TYPE_CALCULATION) {
		stepper_tick_calc_counter++;
		stepper_current_sum += adc_channel_get_data(STEPPER_CURRENT_CHANNEL);
		if(stepper_tick_calc_counter % 100 == 0) {
			stepper_current = stepper_current_sum/100;
			stepper_current_sum = 0;
		}
		// Switch Output Voltage between extern and stack
		if(stepper_get_external_voltage() < STEPPER_VOLTAGE_EPSILON) {
			PIO_Set(&pin_voltage_switch);
		} else {
			PIO_Clear(&pin_voltage_switch);
		}
	} else if(tick_type == TICK_TASK_TYPE_MESSAGE) {
		stepper_tick_counter++;

		if(stepper_position_reached) {
			stepper_position_reached = false;
			stepper_position_reached_signal();
		}

		stepper_check_error_signals();
	}
}
コード例 #2
0
ファイル: stepper.c プロジェクト: Nic2012/stepper-brick
uint16_t stepper_get_stack_voltage(void) {
    return adc_channel_get_data(VOLTAGE_STACK_CHANNEL) *
           VOLTAGE_STACK_REFERENCE *
           VOLTAGE_STACK_MULTIPLIER /
           VOLTAGE_MAX_VALUE;
}
コード例 #3
0
ファイル: stepper.c プロジェクト: Nic2012/stepper-brick
uint16_t stepper_get_external_voltage(void) {
    return adc_channel_get_data(VOLTAGE_EXTERN_CHANNEL) *
           VOLTAGE_EXTERN_REFERENCE *
           VOLTAGE_EXTERN_MULTIPLIER /
           VOLTAGE_MAX_VALUE;
}
コード例 #4
0
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();
	}
}