robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Wed Jan 25 02:17:28 2017 +0000
Revision:
53:b7f824360fe3
Parent:
52:fd3d8df99287
Child:
54:25db122c05f0
error logic fixes

Who changed what in which revision?

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