1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2025-01-01 21:42:14 +00:00
stmbl/shared/comps/fb_switch.c

199 lines
5.3 KiB
C
Raw Permalink Normal View History

2017-06-30 23:38:26 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(fb_switch);
2017-07-02 18:55:08 +00:00
HAL_PIN(polecount);
2017-06-30 23:38:26 +00:00
HAL_PIN(pos_fb);
HAL_PIN(vel_fb);
HAL_PIN(com_fb);
HAL_PIN(joint_fb);
2017-09-06 02:20:06 +00:00
HAL_PIN(state); // 0 = disabled, 1 = enabled
2017-06-30 23:38:26 +00:00
HAL_PIN(cmd_pos);
2017-07-02 18:55:08 +00:00
HAL_PIN(mot_pos);
HAL_PIN(mot_abs_pos);
HAL_PIN(mot_polecount);
HAL_PIN(mot_offset);
2017-09-06 02:20:06 +00:00
HAL_PIN(mot_state); // 0 = disabled, 1 = inc, 2 = start abs, 3 = abs
HAL_PIN(mot_rev);
2017-10-02 23:00:47 +00:00
HAL_PIN(mot_fb_no_offset);
2017-10-23 16:17:59 +00:00
HAL_PIN(mot_abs_fb_no_offset);
2017-06-30 23:38:26 +00:00
2017-07-02 18:55:08 +00:00
HAL_PIN(com_pos);
HAL_PIN(com_abs_pos);
HAL_PIN(com_polecount);
HAL_PIN(com_offset);
HAL_PIN(com_state);
HAL_PIN(com_rev);
2017-06-30 23:38:26 +00:00
HAL_PIN(joint_pos);
HAL_PIN(joint_abs_pos);
HAL_PIN(joint_offset);
HAL_PIN(joint_state);
HAL_PIN(joint_rev);
2017-10-02 23:00:47 +00:00
HAL_PIN(joint_fb_no_offset);
2017-06-30 23:38:26 +00:00
HAL_PIN(mot_joint_ratio);
HAL_PIN(phase_time);
HAL_PIN(phase_cur);
2017-10-02 22:30:42 +00:00
HAL_PIN(id);
2017-06-30 23:38:26 +00:00
2017-07-14 03:41:24 +00:00
HAL_PIN(current_com_pos);
2017-06-30 23:38:26 +00:00
HAL_PIN(en);
2017-09-06 02:20:06 +00:00
struct fb_switch_ctx_t {
int32_t current_com_pos;
float cmd_offset;
float com_offset;
2017-10-02 22:30:42 +00:00
float phase_timer;
int32_t phase_state;
2017-06-30 23:38:26 +00:00
};
2017-09-06 02:20:06 +00:00
static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
2017-10-23 16:17:59 +00:00
struct fb_switch_ctx_t *ctx = (struct fb_switch_ctx_t *)ctx_ptr;
struct fb_switch_pin_ctx_t *pins = (struct fb_switch_pin_ctx_t *)pin_ptr;
2017-09-06 02:20:06 +00:00
ctx->current_com_pos = 10;
ctx->cmd_offset = 0.0;
ctx->com_offset = 0.0;
2017-10-23 16:17:59 +00:00
PIN(phase_cur) = 1.0;
PIN(phase_time) = 1.0;
2017-06-30 23:38:26 +00:00
}
2017-09-06 02:20:06 +00:00
static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
struct fb_switch_ctx_t *ctx = (struct fb_switch_ctx_t *)ctx_ptr;
struct fb_switch_pin_ctx_t *pins = (struct fb_switch_pin_ctx_t *)pin_ptr;
2017-10-23 16:17:59 +00:00
float mot_pos = PIN(mot_pos);
float com_pos = PIN(com_pos);
float joint_pos = PIN(joint_pos);
2017-10-23 16:17:59 +00:00
float mot_abs_pos = PIN(mot_abs_pos);
float com_abs_pos = PIN(com_abs_pos);
float joint_abs_pos = PIN(joint_abs_pos);
2017-10-23 16:17:59 +00:00
float mot_offset = PIN(mot_offset);
float com_offset = PIN(com_offset);
2017-10-02 20:27:34 +00:00
float joint_offset = PIN(joint_offset);
2017-10-23 16:17:59 +00:00
if(PIN(mot_rev) > 0.0) {
mot_pos *= -1.0;
mot_abs_pos *= -1.0;
2017-10-02 20:27:34 +00:00
mot_offset *= -1.0;
}
2017-10-23 16:17:59 +00:00
if(PIN(com_rev) > 0.0) {
com_pos *= -1.0;
com_abs_pos *= -1.0;
2017-10-02 20:27:34 +00:00
com_offset *= -1.0;
}
2017-10-23 16:17:59 +00:00
if(PIN(joint_rev) > 0.0) {
joint_pos *= -1.0;
joint_abs_pos *= -1.0;
2017-10-02 20:27:34 +00:00
joint_offset *= -1.0;
}
2017-10-29 21:42:31 +00:00
PIN(mot_fb_no_offset) = mot_pos;
2017-10-23 16:17:59 +00:00
PIN(mot_abs_fb_no_offset) = mot_abs_pos;
2017-10-29 21:42:31 +00:00
PIN(joint_fb_no_offset) = joint_pos;
2017-10-23 16:17:59 +00:00
PIN(pos_fb) = mod(mot_pos + ctx->cmd_offset);
PIN(vel_fb) = mot_pos;
2017-09-06 02:20:06 +00:00
if(PIN(en) <= 0.0) {
PIN(state) = 0.0;
ctx->current_com_pos = 10;
ctx->com_offset = 0.0;
ctx->cmd_offset = minus(PIN(cmd_pos), mot_pos);
PIN(pos_fb) = mod(mot_pos);
2017-10-23 16:17:59 +00:00
PIN(id) = 0.0;
2017-09-06 02:20:06 +00:00
} else {
PIN(state) = 1.0;
if(PIN(joint_state) >= 2.0 && ctx->current_com_pos > 3.0) {
ctx->current_com_pos = 3;
2017-10-02 20:27:34 +00:00
ctx->com_offset = minus(mod((joint_abs_pos + joint_offset) * PIN(polecount) / PIN(mot_joint_ratio)), mod(mot_pos * PIN(polecount) / PIN(mot_polecount)));
2017-09-06 02:20:06 +00:00
}
if(PIN(com_state) >= 2.0 && ctx->current_com_pos > 2.0) {
ctx->current_com_pos = 2;
2017-10-02 20:27:34 +00:00
ctx->com_offset = minus(mod((com_abs_pos + com_offset) * PIN(polecount) / PIN(com_polecount)), mod(mot_pos * PIN(polecount) / PIN(mot_polecount)));
2017-09-06 02:20:06 +00:00
}
if(PIN(mot_state) >= 2.0 && ctx->current_com_pos > 1.0) {
ctx->current_com_pos = 1;
ctx->com_offset = 0.0;
}
if(ctx->current_com_pos > 4.0) {
2017-10-02 22:30:42 +00:00
PIN(com_fb) = 0.0;
2017-10-23 16:17:59 +00:00
2017-10-02 22:30:42 +00:00
ctx->phase_timer += period;
PIN(id) = CLAMP(ctx->phase_timer / (MAX(PIN(phase_time), 0.1) / 3.0) * PIN(phase_cur), 0.0, PIN(phase_cur));
2017-10-23 16:17:59 +00:00
if(ctx->phase_timer >= MAX(PIN(phase_time), 0.1)) {
ctx->phase_timer = 0.0;
ctx->com_offset = -mod(mot_pos * PIN(polecount) / PIN(mot_polecount));
ctx->cmd_offset = minus(PIN(cmd_pos), mot_pos);
PIN(id) = 0.0;
2017-10-02 22:30:42 +00:00
ctx->current_com_pos = 4.0;
}
2017-09-06 02:20:06 +00:00
}
PIN(current_com_pos) = ctx->current_com_pos;
switch(ctx->current_com_pos) {
case 4:
2017-10-02 16:56:34 +00:00
PIN(com_fb) = mod(mot_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset);
2017-09-06 02:20:06 +00:00
break;
case 3:
if(PIN(joint_state) != 3.0) {
2017-10-02 16:56:34 +00:00
PIN(com_fb) = mod(mot_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset);
2017-09-06 02:20:06 +00:00
} else {
2017-10-02 20:27:34 +00:00
PIN(com_fb) = mod((joint_abs_pos + joint_offset) * PIN(polecount));
2017-09-06 02:20:06 +00:00
}
break;
case 2:
if(PIN(com_state) != 3.0) {
2017-10-02 16:56:34 +00:00
PIN(com_fb) = mod(mot_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset);
2017-09-06 02:20:06 +00:00
} else {
2017-10-02 20:27:34 +00:00
PIN(com_fb) = mod((com_abs_pos + com_offset) * PIN(polecount) / PIN(com_polecount));
2017-09-06 02:20:06 +00:00
}
break;
case 1:
if(PIN(mot_state) != 3.0) {
PIN(state) = 0.0;
} else {
2017-10-02 20:27:34 +00:00
PIN(com_fb) = mod((mot_abs_pos + mot_offset) * PIN(polecount) / PIN(mot_polecount));
2017-09-06 02:20:06 +00:00
}
break;
default:
PIN(state) = 0.0;
}
}
2017-06-30 23:38:26 +00:00
}
hal_comp_t fb_switch_comp_struct = {
2017-09-06 02:20:06 +00:00
.name = "fb_switch",
.nrt = 0,
.rt = rt_func,
.frt = 0,
.nrt_init = nrt_init,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = sizeof(struct fb_switch_ctx_t),
.pin_count = sizeof(struct fb_switch_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
2017-06-30 23:38:26 +00:00
};