2020-09-23 18:59:59 +00:00
|
|
|
#include "fb_switch_comp.h"
|
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);
|
2018-06-24 16:11:20 +00:00
|
|
|
HAL_PIN(track_fb);
|
2018-12-16 17:34:51 +00:00
|
|
|
HAL_PIN(offset_first_enable);
|
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
|
2017-10-02 19:13:59 +00:00
|
|
|
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
|
|
|
|
2018-06-24 16:11:20 +00:00
|
|
|
HAL_PIN(plot_fb_pos);
|
|
|
|
|
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);
|
2017-10-02 19:13:59 +00:00
|
|
|
HAL_PIN(com_rev);
|
2018-05-14 15:18:13 +00:00
|
|
|
HAL_PIN(com_fb_no_offset);
|
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);
|
2017-10-02 19:13:59 +00:00
|
|
|
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);
|
2018-12-28 23:00:31 +00:00
|
|
|
HAL_PIN(phase_gain);
|
|
|
|
HAL_PIN(offset);
|
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;
|
2018-12-28 23:00:31 +00:00
|
|
|
float phase_start_pos;
|
2020-07-09 17:45:13 +00:00
|
|
|
float cmd_com_offset;
|
|
|
|
float cmd_mot_offset;
|
|
|
|
float cmd_joint_offset;
|
2017-09-06 02:20:06 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-04-29 20:08:54 +00:00
|
|
|
static void nrt_init(void *ctx_ptr, 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;
|
2020-07-09 17:45:13 +00:00
|
|
|
ctx->cmd_mot_offset = 0.0;
|
2017-09-06 02:20:06 +00:00
|
|
|
ctx->com_offset = 0.0;
|
2017-10-23 16:17:59 +00:00
|
|
|
PIN(phase_cur) = 1.0;
|
|
|
|
PIN(phase_time) = 1.0;
|
2018-12-28 23:00:31 +00:00
|
|
|
PIN(phase_gain) = 100.0;
|
2018-12-16 17:34:51 +00:00
|
|
|
PIN(offset_first_enable) = 1.0;
|
2017-06-30 23:38:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:08:54 +00:00
|
|
|
static void rt_func(float period, void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
|
2017-09-06 02:20:06 +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-10-23 16:17:59 +00:00
|
|
|
float mot_pos = PIN(mot_pos);
|
|
|
|
float com_pos = PIN(com_pos);
|
2017-10-02 19:13:59 +00:00
|
|
|
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);
|
2017-10-02 19:13:59 +00:00
|
|
|
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) {
|
2017-10-02 19:13:59 +00:00
|
|
|
mot_pos *= -1.0;
|
|
|
|
mot_abs_pos *= -1.0;
|
2017-10-02 20:27:34 +00:00
|
|
|
mot_offset *= -1.0;
|
2017-10-02 19:13:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 16:17:59 +00:00
|
|
|
if(PIN(com_rev) > 0.0) {
|
2017-10-02 19:13:59 +00:00
|
|
|
com_pos *= -1.0;
|
|
|
|
com_abs_pos *= -1.0;
|
2017-10-02 20:27:34 +00:00
|
|
|
com_offset *= -1.0;
|
2017-10-02 19:13:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 16:17:59 +00:00
|
|
|
if(PIN(joint_rev) > 0.0) {
|
2017-10-02 19:13:59 +00:00
|
|
|
joint_pos *= -1.0;
|
|
|
|
joint_abs_pos *= -1.0;
|
2017-10-02 20:27:34 +00:00
|
|
|
joint_offset *= -1.0;
|
2017-10-02 19:13:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 15:18:13 +00:00
|
|
|
PIN(com_fb_no_offset) = com_pos;
|
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
|
|
|
|
2018-12-28 23:00:31 +00:00
|
|
|
PIN(id) = 0.0;
|
|
|
|
|
2020-07-09 17:45:13 +00:00
|
|
|
if(PIN(joint_state) == 1.0 || PIN(joint_state) == 3.0){
|
|
|
|
PIN(pos_fb) = mod(joint_pos + ctx->cmd_joint_offset);
|
|
|
|
PIN(plot_fb_pos) = PIN(joint_fb);
|
|
|
|
}
|
|
|
|
else if(PIN(mot_state) == 1.0 || PIN(mot_state) == 3.0){
|
|
|
|
PIN(pos_fb) = mod(mot_pos + ctx->cmd_mot_offset);
|
|
|
|
PIN(plot_fb_pos) = PIN(pos_fb);
|
|
|
|
}
|
|
|
|
else if(PIN(com_state) == 1.0 || PIN(com_state) == 3.0){
|
|
|
|
PIN(pos_fb) = mod(com_pos + ctx->cmd_com_offset);
|
|
|
|
PIN(plot_fb_pos) = PIN(pos_fb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PIN(mot_state) == 1.0 || PIN(mot_state) == 3.0){
|
|
|
|
PIN(vel_fb) = mot_pos;
|
|
|
|
}
|
|
|
|
else if(PIN(com_state) == 1.0 || PIN(com_state) == 3.0){
|
|
|
|
PIN(vel_fb) = com_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;
|
2018-12-16 17:34:51 +00:00
|
|
|
if((PIN(track_fb) <= 0.0 || PIN(offset_first_enable) > 0) && PIN(mot_state) > 0.0){
|
|
|
|
PIN(offset_first_enable) = 0;
|
2020-07-09 17:45:13 +00:00
|
|
|
ctx->cmd_com_offset = minus(PIN(cmd_pos), com_pos);
|
|
|
|
ctx->cmd_mot_offset = minus(PIN(cmd_pos), mot_pos);
|
|
|
|
ctx->cmd_joint_offset = minus(PIN(cmd_pos), joint_pos);
|
2018-06-24 16:11:20 +00:00
|
|
|
}
|
|
|
|
PIN(plot_fb_pos) = mod(mot_pos);
|
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) {
|
2018-04-11 00:24:36 +00:00
|
|
|
ctx->current_com_pos = 3; // joint fb absolute
|
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) {
|
2018-04-11 00:24:36 +00:00
|
|
|
ctx->current_com_pos = 2; // com fb absolute
|
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) {
|
2018-04-11 00:24:36 +00:00
|
|
|
ctx->current_com_pos = 1; // mot fb absolute
|
2017-09-06 02:20:06 +00:00
|
|
|
ctx->com_offset = 0.0;
|
|
|
|
}
|
2018-04-11 00:24:36 +00:00
|
|
|
if(ctx->current_com_pos > 4.0) { // autophasing
|
2018-12-28 23:00:31 +00:00
|
|
|
switch(ctx->current_com_pos) {
|
|
|
|
case 5: // phasing
|
|
|
|
ctx->phase_timer += period;
|
|
|
|
PIN(id) = CLAMP(ctx->phase_timer / (MAX(PIN(phase_time), 0.1) / 4.0) * PIN(phase_cur), 0.0, PIN(phase_cur)); // ramp up id
|
|
|
|
ctx->com_offset += minus(ctx->phase_start_pos, mot_pos) * PIN(phase_gain) * period;
|
|
|
|
|
|
|
|
if(ctx->phase_timer >= MAX(PIN(phase_time), 0.1)) { // end
|
|
|
|
ctx->phase_timer = 0.0;
|
|
|
|
//ctx->com_offset = -mod(mot_pos * PIN(polecount) / PIN(mot_polecount));
|
2020-07-09 17:45:13 +00:00
|
|
|
ctx->cmd_mot_offset = minus(PIN(cmd_pos), mot_pos);
|
2018-12-28 23:00:31 +00:00
|
|
|
ctx->current_com_pos = 4.0;
|
|
|
|
}
|
|
|
|
break;
|
2017-10-02 22:30:42 +00:00
|
|
|
|
2018-12-28 23:00:31 +00:00
|
|
|
default: // init
|
|
|
|
ctx->phase_start_pos = mot_pos; // safe start pos
|
|
|
|
PIN(com_fb) = 0.0;
|
|
|
|
ctx->com_offset = 0.0;
|
|
|
|
ctx->current_com_pos = 5.0;
|
2017-10-02 22:30:42 +00:00
|
|
|
}
|
2017-09-06 02:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(ctx->current_com_pos) {
|
2018-10-05 17:53:54 +00:00
|
|
|
case 4: // autophasing + mot fb -> com fb
|
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;
|
|
|
|
|
2018-10-05 17:53:54 +00:00
|
|
|
case 3: // joint fb -> com fb
|
2017-09-06 02:20:06 +00:00
|
|
|
if(PIN(joint_state) != 3.0) {
|
2018-04-11 00:24:36 +00:00
|
|
|
PIN(com_fb) = mod(mot_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset); // tracking
|
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;
|
|
|
|
|
2018-10-05 17:53:54 +00:00
|
|
|
case 2: // com fb -> com fb
|
2017-09-06 02:20:06 +00:00
|
|
|
if(PIN(com_state) != 3.0) {
|
2018-04-11 00:24:36 +00:00
|
|
|
PIN(com_fb) = mod(mot_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset); // tracking
|
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;
|
|
|
|
|
2018-10-05 17:53:54 +00:00
|
|
|
case 1: // mot fb -> com fb
|
2017-09-06 02:20:06 +00:00
|
|
|
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;
|
2018-12-28 23:00:31 +00:00
|
|
|
PIN(com_fb) = mod(ctx->phase_start_pos * PIN(polecount) / PIN(mot_polecount) + ctx->com_offset);
|
2017-09-06 02:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-11 00:24:36 +00:00
|
|
|
PIN(current_com_pos) = ctx->current_com_pos;
|
2018-12-28 23:00:31 +00:00
|
|
|
PIN(offset) = ctx->com_offset;
|
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
|
|
|
};
|