Bayley Wang / foc-ed_in_the_bot_compact

robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Tue Feb 28 03:32:21 2017 +0000
Revision:
79:d0b1bb3dcf68
Parent:
75:591556ce033d
Child:
77:66a9b53131f8
Child:
82:5e741c5ffd9f
Child:
88:703780322d5b
added FlashWriter class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang0:bac9c3a3a6ca 1#include "mbed.h"
bwang0:bac9c3a3a6ca 2#include "math.h"
bwang19:a6cf15f89f3d 3
bwang0:bac9c3a3a6ca 4#include "PositionSensor.h"
bwang0:bac9c3a3a6ca 5#include "FastPWM.h"
bwang18:3863ca45cf26 6#include "PwmIn.h"
bwang19:a6cf15f89f3d 7#include "MathHelpers.h"
bwang42:030e0ec4eac5 8#include "Transforms.h"
bwang42:030e0ec4eac5 9#include "DQMapper.h"
bwang42:030e0ec4eac5 10#include "ThrottleMapper.h"
bwang42:030e0ec4eac5 11
bwang42:030e0ec4eac5 12#include "BREMSStructs.h"
bwang42:030e0ec4eac5 13#include "BREMSConfig.h"
bwang30:c25c5bf0d951 14
bwang15:b583cd30b063 15#include "config_motor.h"
bwang15:b583cd30b063 16#include "config_loop.h"
bwang19:a6cf15f89f3d 17#include "config_pins.h"
bwang15:b583cd30b063 18#include "config_inverter.h"
bwang29:50e6e4e46580 19#include "config_driving.h"
bwang72:5f1da97d62e1 20#include "config_logging.h"
bwang0:bac9c3a3a6ca 21
bwang42:030e0ec4eac5 22#include "main.h"
bwang0:bac9c3a3a6ca 23
bwang42:030e0ec4eac5 24IOStruct io;
bwang42:030e0ec4eac5 25ReadDataStruct read;
bwang42:030e0ec4eac5 26FOCStruct foc;
bwang42:030e0ec4eac5 27ControlStruct control;
bwang0:bac9c3a3a6ca 28
bwang42:030e0ec4eac5 29DQMapper *dq;
bwang42:030e0ec4eac5 30ThrottleMapper *th;
bwang2:eabe8feaaabb 31
bwang44:3fd6a43b91f0 32int loop_counter = 0;
bwang25:3f2b585ae72d 33bool control_enabled = false;
dicarloj13:41d102a53caf 34
bwang42:030e0ec4eac5 35void update_velocity() {
bwang42:030e0ec4eac5 36 read.last_p_mech = read.p_mech;
bwang42:030e0ec4eac5 37 read.p_mech = io.pos->GetMechPosition();
bwang44:3fd6a43b91f0 38
bwang42:030e0ec4eac5 39 float dp_mech = read.p_mech - read.last_p_mech;
bwang42:030e0ec4eac5 40 if (dp_mech < -PI) dp_mech += 2 * PI;
bwang42:030e0ec4eac5 41 if (dp_mech > PI) dp_mech -= 2 * PI;
bwang44:3fd6a43b91f0 42
bwang42:030e0ec4eac5 43 float w_raw = dp_mech * F_SW; //rad/s
bwang42:030e0ec4eac5 44 if (w_raw > W_CRAZY) w_raw = read.w; //with this limiting scheme noise < 0
bwang42:030e0ec4eac5 45 if (w_raw < -W_CRAZY) w_raw = read.w; //so we need to throw out the large deltas first
bwang44:3fd6a43b91f0 46
bwang44:3fd6a43b91f0 47 read.w = update_filter(read.w, w_raw, W_FILTER_STRENGTH);
bwang42:030e0ec4eac5 48}
bwang2:eabe8feaaabb 49
bwang75:591556ce033d 50void commutate() {
bwang56:c681001dfa46 51 /*safety checks, do we do anything this cycle?*/
bwang56:c681001dfa46 52 if (!control_enabled && io.throttle_in->get_enabled() && io.pos->IsValid() && is_driving()) {
bwang52:fd3d8df99287 53 go_enabled();
bwang52:fd3d8df99287 54 }
bwang42:030e0ec4eac5 55
bwang42:030e0ec4eac5 56 /*update velocity, references*/
bwang42:030e0ec4eac5 57 update_velocity();
bwang44:3fd6a43b91f0 58 if (loop_counter % SLOW_LOOP_COUNTER == 0) {
bwang44:3fd6a43b91f0 59 loop_counter = 0;
bwang44:3fd6a43b91f0 60 slow_loop();
bwang44:3fd6a43b91f0 61 }
bwang44:3fd6a43b91f0 62 loop_counter++;
bwang42:030e0ec4eac5 63
bwang42:030e0ec4eac5 64 /*update position, sin, cos*/
bwang42:030e0ec4eac5 65 foc.p = io.pos->GetElecPosition() - POS_OFFSET;
bwang42:030e0ec4eac5 66 float sin_p = sinf(foc.p);
bwang42:030e0ec4eac5 67 float cos_p = cosf(foc.p);
bwang42:030e0ec4eac5 68
bwang49:da8604278d76 69 /*scale and offset currents (adval1, 2 are updated in ISR)*/
bwang42:030e0ec4eac5 70 foc.ia = ((float) read.adval1 / 4096.0f * AVDD - I_OFFSET - read.ia_supp_offset) / I_SCALE;
bwang42:030e0ec4eac5 71 foc.ib = ((float) read.adval2 / 4096.0f * AVDD - I_OFFSET - read.ib_supp_offset) / I_SCALE;
bwang42:030e0ec4eac5 72
bwang42:030e0ec4eac5 73 /*compute d, q*/
bwang42:030e0ec4eac5 74 clarke(foc.ia, foc.ib, &foc.alpha, &foc.beta);
bwang42:030e0ec4eac5 75 park(foc.alpha, foc.beta, sin_p, cos_p, &foc.d, &foc.q);
bwang42:030e0ec4eac5 76
bwang42:030e0ec4eac5 77 /*PI controller*/
bwang44:3fd6a43b91f0 78 control.d_filtered = update_filter(control.d_filtered, foc.d, DQ_FILTER_STRENGTH);
bwang44:3fd6a43b91f0 79 control.q_filtered = update_filter(control.q_filtered, foc.q, DQ_FILTER_STRENGTH);
bwang42:030e0ec4eac5 80
bwang42:030e0ec4eac5 81 float d_err = control.d_ref - control.d_filtered;
bwang42:030e0ec4eac5 82 float q_err = control.q_ref - control.q_filtered;
bwang42:030e0ec4eac5 83
bwang58:7316c5a4c417 84 control.d_integral += d_err * KI_D;
bwang58:7316c5a4c417 85 control.q_integral += q_err * KI_Q;
bwang42:030e0ec4eac5 86
bwang64:b4175385d718 87 constrain_norm(&control.d_integral, &control.q_integral, 0.5f, 1.0f, 1.0f);
bwang42:030e0ec4eac5 88
bwang58:7316c5a4c417 89 foc.vd = KP_D * d_err + control.d_integral;// - Lq * POLE_PAIRS * read.w * foc.q / BUS_VOLTAGE / 2.0f;
bwang58:7316c5a4c417 90 foc.vq = KP_Q * q_err + control.q_integral;// + Ld * POLE_PAIRS * read.w * foc.d / BUS_VOLTAGE / 2.0f;
bwang42:030e0ec4eac5 91
bwang64:b4175385d718 92 constrain_norm(&foc.vd, &foc.vq, 1.0f, 1.0f, 1.0f);
bwang42:030e0ec4eac5 93
bwang42:030e0ec4eac5 94 if (!control_enabled) {
bwang42:030e0ec4eac5 95 foc.vd = 0.0f;
bwang42:030e0ec4eac5 96 foc.vq = 0.0f;
bwang42:030e0ec4eac5 97 }
bwang42:030e0ec4eac5 98
bwang42:030e0ec4eac5 99 /*inverse transforms*/
bwang42:030e0ec4eac5 100 invpark(foc.vd, foc.vq, sin_p, cos_p, &foc.valpha, &foc.vbeta);
bwang42:030e0ec4eac5 101
bwang42:030e0ec4eac5 102 float va, vb, vc, voff;
bwang42:030e0ec4eac5 103
bwang42:030e0ec4eac5 104 invclarke(foc.valpha, foc.vbeta, &va, &vb);
bwang42:030e0ec4eac5 105 vc = -va - vb;
bwang42:030e0ec4eac5 106
bwang42:030e0ec4eac5 107 /*SVPWM*/
bwang44:3fd6a43b91f0 108 voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;//don't think about it
bwang42:030e0ec4eac5 109 va = va - voff;
bwang42:030e0ec4eac5 110 vb = vb - voff;
bwang42:030e0ec4eac5 111 vc = vc - voff;
bwang42:030e0ec4eac5 112
bwang56:c681001dfa46 113 /*safety checks, reset integral*/
bwang56:c681001dfa46 114 if (!io.throttle_in->get_enabled() || !io.pos->IsValid() || !is_driving()) {
bwang56:c681001dfa46 115 /*do this even in disabled state, to keep integral down*/
bwang56:c681001dfa46 116 go_disabled();
bwang56:c681001dfa46 117 }
bwang56:c681001dfa46 118
bwang42:030e0ec4eac5 119 /*output to timers*/
bwang47:1c9868e226d0 120 set_dtc(io.a, 0.5f + 0.5f * va);
bwang47:1c9868e226d0 121 set_dtc(io.b, 0.5f + 0.5f * vb);
bwang47:1c9868e226d0 122 set_dtc(io.c, 0.5f + 0.5f * vc);
bwang42:030e0ec4eac5 123}
bwang42:030e0ec4eac5 124
bwang44:3fd6a43b91f0 125void slow_loop() {
bwang74:f10cb573d7ca 126 float x = io.throttle_in->get_throttle();
bwang75:591556ce033d 127 control.torque_percent = update_filter(control.torque_percent, th->map(io.throttle_in->get_throttle(), read.w), THROTTLE_FILTER_STRENGTH);
bwang75:591556ce033d 128
bwang70:5e39beeb4a21 129 dq->map(control.torque_percent, read.w, &control.d_ref, &control.q_ref);
bwang44:3fd6a43b91f0 130}
bwang44:3fd6a43b91f0 131
bwang42:030e0ec4eac5 132void go_enabled() {
bwang42:030e0ec4eac5 133 control_enabled = true;
bwang42:030e0ec4eac5 134 io.en->write(1);
bwang42:030e0ec4eac5 135}
bwang42:030e0ec4eac5 136
bwang42:030e0ec4eac5 137void go_disabled() {
bwang52:fd3d8df99287 138 control.d_integral = 0.0f;
bwang52:fd3d8df99287 139 control.q_integral = 0.0f;
bwang42:030e0ec4eac5 140 control_enabled = false;
bwang42:030e0ec4eac5 141 io.en->write(0);
bwang42:030e0ec4eac5 142}
bwang42:030e0ec4eac5 143
bwang52:fd3d8df99287 144bool is_driving() {
bwang55:3568b78dc203 145 return io.throttle_in->get_throttle() > 0.01f || fabsf(read.w) > W_SAFE;
bwang52:fd3d8df99287 146}
bwang52:fd3d8df99287 147
bwang44:3fd6a43b91f0 148float update_filter(float old, float x, float str) {
bwang42:030e0ec4eac5 149 return str * old + (1.0f - str) * x;
bwang42:030e0ec4eac5 150}
dicarloj13:41d102a53caf 151
bwang72:5f1da97d62e1 152void log() {
bwang76:c66014e50384 153 io.pc->printf("%d,%d,%d,%d,%d,%d,%d,%d\n", (int) read.w, (int) control.d_ref, (int) control.d_filtered, (int) control.q_ref, (int) control.q_filtered, (int) (255 * control.torque_percent),
bwang76:c66014e50384 154 (int) (255 * foc.vd), (int) (255 * foc.vq));
bwang76:c66014e50384 155 //io.pc->printf("%d\n", io.throttle_in->get_usecs());
bwang72:5f1da97d62e1 156 wait(1.0f / LOG_FREQUENCY);
bwang70:5e39beeb4a21 157}
bwang70:5e39beeb4a21 158
bwang70:5e39beeb4a21 159
bwang1:7b61790f6be9 160extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
bwang75:591556ce033d 161 int start_state = io.throttle_in->state();
bwang1:7b61790f6be9 162 if (TIM1->SR & TIM_SR_UIF ) {
bwang4:a6669248ce4d 163 ADC1->CR2 |= 0x40000000;
bwang4:a6669248ce4d 164 volatile int delay;
bwang4:a6669248ce4d 165 for (delay = 0; delay < 35; delay++);
bwang42:030e0ec4eac5 166 read.adval1 = ADC1->DR;
bwang42:030e0ec4eac5 167 read.adval2 = ADC2->DR;
bwang4:a6669248ce4d 168 commutate();
bwang1:7b61790f6be9 169 }
bwang1:7b61790f6be9 170 TIM1->SR = 0x00;
bwang75:591556ce033d 171 int end_state = io.throttle_in->state();
bwang75:591556ce033d 172 if (start_state != end_state) io.throttle_in->block();
bwang1:7b61790f6be9 173}
bwang1:7b61790f6be9 174
bwang0:bac9c3a3a6ca 175int main() {
bwang59:0416da6c3060 176 dq = new LutMapper();
bwang45:cf8ad81fb0f0 177 th = new NullThrottleMapper();
bwang47:1c9868e226d0 178 BREMSInit(&io, &read, &foc, &control, false);
bwang48:a1a09c83d42c 179
bwang0:bac9c3a3a6ca 180 for (;;) {
bwang72:5f1da97d62e1 181 if (ENABLE_LOGGING) {
bwang72:5f1da97d62e1 182 log();
bwang72:5f1da97d62e1 183 }
bwang0:bac9c3a3a6ca 184 }
bwang42:030e0ec4eac5 185}