2020-09-23 18:59:59 +00:00
|
|
|
#include "uvw_comp.h"
|
2017-06-30 21:18:18 +00:00
|
|
|
#include "commands.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "math.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "angle.h"
|
|
|
|
|
|
|
|
HAL_COMP(uvw);
|
|
|
|
|
|
|
|
//u,v,w inputs
|
|
|
|
HAL_PIN(u);
|
|
|
|
HAL_PIN(v);
|
|
|
|
HAL_PIN(w);
|
|
|
|
|
2019-12-11 09:22:44 +00:00
|
|
|
HAL_PIN(amp);
|
|
|
|
HAL_PIN(timer);
|
|
|
|
HAL_PIN(en_time);
|
|
|
|
HAL_PIN(mode); // 0 = uvw, 1 = uvw line saving
|
|
|
|
|
2017-09-12 21:17:53 +00:00
|
|
|
HAL_PIN(led);
|
|
|
|
|
2017-07-17 17:51:08 +00:00
|
|
|
HAL_PIN(p0);
|
|
|
|
HAL_PIN(p1);
|
|
|
|
HAL_PIN(p2);
|
|
|
|
HAL_PIN(p3);
|
|
|
|
HAL_PIN(p4);
|
|
|
|
HAL_PIN(p5);
|
|
|
|
HAL_PIN(p6);
|
|
|
|
HAL_PIN(p7);
|
|
|
|
|
2017-06-30 21:18:18 +00:00
|
|
|
//rotor position output
|
|
|
|
HAL_PIN(pos);
|
2017-07-17 17:51:08 +00:00
|
|
|
HAL_PIN(rpos);
|
2017-10-02 19:13:59 +00:00
|
|
|
HAL_PIN(state);
|
2019-12-11 09:22:44 +00:00
|
|
|
HAL_PIN(error);
|
2017-07-17 17:51:08 +00:00
|
|
|
|
2019-04-29 20:08:54 +00:00
|
|
|
static void nrt_init(void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
|
2017-07-17 17:51:08 +00:00
|
|
|
// struct uvw_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr;
|
2017-09-06 02:20:06 +00:00
|
|
|
struct uvw_pin_ctx_t *pins = (struct uvw_pin_ctx_t *)pin_ptr;
|
|
|
|
|
2019-12-11 09:22:44 +00:00
|
|
|
PIN(p0) = -1; //fault
|
2017-10-29 21:42:31 +00:00
|
|
|
PIN(p1) = 0; //u = 0
|
|
|
|
PIN(p2) = 2; //v = 2.094395
|
|
|
|
PIN(p3) = 1; //u + v = 1.047198
|
|
|
|
PIN(p4) = 4; //w = -2.094395
|
|
|
|
PIN(p5) = 5; //u + w = -1.047198
|
|
|
|
PIN(p6) = 3; //v + w = -3.141593
|
2019-12-11 09:22:44 +00:00
|
|
|
PIN(p7) = -1; //fault
|
2020-02-23 17:39:20 +00:00
|
|
|
PIN(en_time) = 0.01;
|
2017-07-17 17:51:08 +00:00
|
|
|
}
|
2017-06-30 21:18:18 +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 uvw_ctx_t * ctx = (struct uvw_ctx_t *)ctx_ptr;
|
|
|
|
struct uvw_pin_ctx_t *pins = (struct uvw_pin_ctx_t *)pin_ptr;
|
2017-06-30 21:18:18 +00:00
|
|
|
|
2017-09-06 02:20:06 +00:00
|
|
|
uint32_t rpos = (PIN(u) > 0.0) * 1.0 + (PIN(v) > 0.0) * 2.0 + (PIN(w) > 0.0) * 4.0;
|
2017-09-13 12:43:23 +00:00
|
|
|
PIN(led) = (PIN(u) > 0.0) ^ (PIN(v) > 0.0) ^ (PIN(w) > 0.0);
|
2017-09-06 02:20:06 +00:00
|
|
|
//TODO: make this const, fault output
|
2019-12-11 09:22:44 +00:00
|
|
|
int32_t t[8];
|
2017-09-06 02:20:06 +00:00
|
|
|
t[0] = PIN(p0);
|
|
|
|
t[1] = PIN(p1);
|
|
|
|
t[2] = PIN(p2);
|
|
|
|
t[3] = PIN(p3);
|
|
|
|
t[4] = PIN(p4);
|
|
|
|
t[5] = PIN(p5);
|
|
|
|
t[6] = PIN(p6);
|
|
|
|
t[7] = PIN(p7);
|
|
|
|
PIN(rpos) = rpos;
|
2020-02-24 14:21:24 +00:00
|
|
|
if(PIN(amp) < 0.75){ // fix wire saving
|
2019-12-11 09:22:44 +00:00
|
|
|
PIN(error) = 1.0;
|
|
|
|
PIN(state) = 0.0;
|
|
|
|
PIN(timer) = 0.0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch((int) PIN(mode)){
|
|
|
|
case 0:
|
2020-03-22 13:06:44 +00:00
|
|
|
if(t[rpos] >= 0.0){
|
2020-02-23 17:39:20 +00:00
|
|
|
PIN(state) = 3.0;
|
|
|
|
PIN(error) = 0.0;
|
|
|
|
PIN(pos) = mod((float)t[rpos] / 6.0 * 2.0 * M_PI);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
PIN(error) = 1.0;
|
|
|
|
PIN(state) = 0.0;
|
|
|
|
}
|
2019-12-11 09:22:44 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(PIN(timer) < PIN(en_time) / 2.0){
|
|
|
|
PIN(pos) = mod((float)t[rpos] / 6.0 * 2.0 * M_PI);
|
2020-02-23 17:39:20 +00:00
|
|
|
PIN(timer) += period;
|
2019-12-11 09:22:44 +00:00
|
|
|
}
|
2020-02-23 17:39:20 +00:00
|
|
|
else{
|
2019-12-11 09:22:44 +00:00
|
|
|
PIN(state) = 2.0;
|
|
|
|
PIN(error) = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hal_comp_t uvw_comp_struct = {
|
2017-09-06 02:20:06 +00:00
|
|
|
.name = "uvw",
|
|
|
|
.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 = 0,
|
|
|
|
.pin_count = sizeof(struct uvw_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
|
2017-06-30 21:18:18 +00:00
|
|
|
};
|