feat: 增加can总线和步进电机指令绑定
This commit is contained in:
@@ -22,6 +22,7 @@ int64_t position_aim = 0;
|
||||
int64_t position_offset = 0;
|
||||
|
||||
uint8_t running_flag = 0;
|
||||
uint8_t reset_flag = 0;
|
||||
|
||||
// TODO 是否增加一个动作列表?一般都比较简单吧
|
||||
|
||||
@@ -50,6 +51,8 @@ void by_stepper_set_speed(stepper_speed_t speed)
|
||||
void by_stepper_run(void)
|
||||
{
|
||||
// 根据预设条件发送脉冲
|
||||
gpio_bits_write(GPIOA, GPIO_PINS_9, FALSE); // EN
|
||||
|
||||
gpio_bits_write(GPIOA, GPIO_PINS_8, by_stepper.dir ? TRUE : FALSE); // DIR
|
||||
gpio_bits_write(GPIOB, GPIO_PINS_15, !gpio_output_data_bit_read(GPIOB, GPIO_PINS_15)); // CLK
|
||||
DWT_Delay(40U * (uint16_t)by_stepper.speed);
|
||||
@@ -66,6 +69,14 @@ void by_stepper_stop(uint8_t hold)
|
||||
void by_stepper_set_position_millimeter(float distance)
|
||||
{
|
||||
if (!running_flag) {
|
||||
// 设置距离较小时直接复位,减少累计定位误差
|
||||
if (distance < 3 && distance > -3) {
|
||||
reset_flag = 1;
|
||||
running_flag = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
gpio_bits_write(GPIOA, GPIO_PINS_9, FALSE); // EN
|
||||
position_aim = (int64_t)(distance * ENCODER_PULSE_PER_MILLIMETER);
|
||||
position_offset = position_aim - position;
|
||||
running_flag = 1;
|
||||
@@ -75,6 +86,7 @@ void by_stepper_set_position_millimeter(float distance)
|
||||
void by_stepper_add_position_millimeter(float distance)
|
||||
{
|
||||
if (!running_flag) {
|
||||
gpio_bits_write(GPIOA, GPIO_PINS_9, FALSE); // EN
|
||||
position_aim = position + (int64_t)(distance * ENCODER_PULSE_PER_MILLIMETER);
|
||||
position_offset = position_aim - position;
|
||||
running_flag = 1;
|
||||
@@ -112,7 +124,9 @@ void by_stepper_init(void)
|
||||
DWT_Delay(40U * (uint16_t)by_stepper.speed);
|
||||
}
|
||||
|
||||
position = 0; // 位置归零
|
||||
position = 0; // 位置归零
|
||||
position_aim = 0;
|
||||
position_offset = 0;
|
||||
tmr_counter_value_set(TMR3, 0);
|
||||
|
||||
LOGD("by_stepper init ok");
|
||||
@@ -125,21 +139,31 @@ void by_stepper_loop(void)
|
||||
tmr_counter_value_set(TMR3, 0);
|
||||
|
||||
if (running_flag) {
|
||||
if (position_offset > 0) {
|
||||
by_stepper_set_dir(0);
|
||||
by_stepper_run();
|
||||
} else if (position_offset < 0) {
|
||||
by_stepper_set_dir(1);
|
||||
by_stepper_run();
|
||||
|
||||
if (reset_flag) // 复位模式
|
||||
{
|
||||
by_stepper_init();
|
||||
running_flag = 0;
|
||||
reset_flag = 0;
|
||||
} else // 正常模式
|
||||
{
|
||||
if (position_offset > 0) {
|
||||
by_stepper_set_dir(0);
|
||||
by_stepper_run();
|
||||
} else if (position_offset < 0) {
|
||||
by_stepper_set_dir(1);
|
||||
by_stepper_run();
|
||||
}
|
||||
|
||||
// 当定位误差小于阈值时,停止运动。否则重新计算定位误差
|
||||
if (abs_s64(position_aim - position) < ENCODER_ERROR_THRESHOLD) {
|
||||
running_flag = 0;
|
||||
position_offset = 0;
|
||||
} else {
|
||||
position_offset = position_aim - position;
|
||||
}
|
||||
}
|
||||
|
||||
// 当定位误差小于阈值时,停止运动。否则重新计算定位误差
|
||||
if (abs_s64(position_aim - position) < ENCODER_ERROR_THRESHOLD) {
|
||||
running_flag = 0;
|
||||
position_offset = 0;
|
||||
} else {
|
||||
position_offset = position_aim - position;
|
||||
}
|
||||
} else {
|
||||
by_stepper_stop(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user