![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
robot
main.cpp@167:7f1f7dd969e2, 2017-10-01 (annotated)
- Committer:
- bwang
- Date:
- Sun Oct 01 07:58:19 2017 +0000
- Revision:
- 167:7f1f7dd969e2
- Parent:
- 166:4637785ba01e
- Child:
- 168:64a98ff3fb63
stall testing code, incorporates previous throttle fixes
Who changed what in which revision?
User | Revision | Line number | New 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 | 118:2b6dab10b69d | 11 | #include "Calibration.h" |
bwang | 150:08c13bfc7417 | 12 | #include "Filter.h" |
bwang | 42:030e0ec4eac5 | 13 | |
bwang | 42:030e0ec4eac5 | 14 | #include "BREMSStructs.h" |
bwang | 42:030e0ec4eac5 | 15 | #include "BREMSConfig.h" |
bwang | 30:c25c5bf0d951 | 16 | |
bwang | 15:b583cd30b063 | 17 | #include "config_motor.h" |
bwang | 15:b583cd30b063 | 18 | #include "config_loop.h" |
bwang | 19:a6cf15f89f3d | 19 | #include "config_pins.h" |
bwang | 15:b583cd30b063 | 20 | #include "config_inverter.h" |
bwang | 29:50e6e4e46580 | 21 | #include "config_driving.h" |
bwang | 72:5f1da97d62e1 | 22 | #include "config_logging.h" |
bwang | 0:bac9c3a3a6ca | 23 | |
bwang | 42:030e0ec4eac5 | 24 | #include "main.h" |
bwang | 0:bac9c3a3a6ca | 25 | |
bwang | 42:030e0ec4eac5 | 26 | IOStruct io; |
bwang | 42:030e0ec4eac5 | 27 | ReadDataStruct read; |
bwang | 42:030e0ec4eac5 | 28 | FOCStruct foc; |
bwang | 42:030e0ec4eac5 | 29 | ControlStruct control; |
bwang | 2:eabe8feaaabb | 30 | |
bwang | 42:030e0ec4eac5 | 31 | DQMapper *dq; |
bwang | 42:030e0ec4eac5 | 32 | ThrottleMapper *th; |
bwang | 2:eabe8feaaabb | 33 | |
bwang | 44:3fd6a43b91f0 | 34 | int loop_counter = 0; |
bwang | 25:3f2b585ae72d | 35 | bool control_enabled = false; |
dicarloj | 13:41d102a53caf | 36 | |
bwang | 42:030e0ec4eac5 | 37 | void update_velocity() { |
bwang | 42:030e0ec4eac5 | 38 | read.last_p_mech = read.p_mech; |
bwang | 42:030e0ec4eac5 | 39 | read.p_mech = io.pos->GetMechPosition(); |
bwang | 44:3fd6a43b91f0 | 40 | |
bwang | 42:030e0ec4eac5 | 41 | float dp_mech = read.p_mech - read.last_p_mech; |
bwang | 42:030e0ec4eac5 | 42 | if (dp_mech < -PI) dp_mech += 2 * PI; |
bwang | 42:030e0ec4eac5 | 43 | if (dp_mech > PI) dp_mech -= 2 * PI; |
bwang | 44:3fd6a43b91f0 | 44 | |
bwang | 42:030e0ec4eac5 | 45 | float w_raw = dp_mech * F_SW; //rad/s |
bwang | 44:3fd6a43b91f0 | 46 | |
bwang | 154:0a22dcf91577 | 47 | read.w = control.velocity_filter->update(w_raw); |
bwang | 42:030e0ec4eac5 | 48 | } |
bwang | 2:eabe8feaaabb | 49 | |
bwang | 118:2b6dab10b69d | 50 | void commutate() { |
bwang | 56:c681001dfa46 | 51 | /*safety checks, do we do anything this cycle?*/ |
bwang | 56:c681001dfa46 | 52 | if (!control_enabled && io.throttle_in->get_enabled() && io.pos->IsValid() && is_driving()) { |
bwang | 52:fd3d8df99287 | 53 | go_enabled(); |
bwang | 52:fd3d8df99287 | 54 | } |
bwang | 42:030e0ec4eac5 | 55 | |
bwang | 42:030e0ec4eac5 | 56 | /*update velocity, references*/ |
bwang | 42:030e0ec4eac5 | 57 | update_velocity(); |
bwang | 44:3fd6a43b91f0 | 58 | if (loop_counter % SLOW_LOOP_COUNTER == 0) { |
bwang | 44:3fd6a43b91f0 | 59 | loop_counter = 0; |
bwang | 44:3fd6a43b91f0 | 60 | slow_loop(); |
bwang | 44:3fd6a43b91f0 | 61 | } |
bwang | 44:3fd6a43b91f0 | 62 | loop_counter++; |
bwang | 42:030e0ec4eac5 | 63 | |
bwang | 42:030e0ec4eac5 | 64 | /*update position, sin, cos*/ |
bwang | 130:639cd8586f86 | 65 | foc.p = io.pos->GetElecPosition() - POS_OFFSET; |
bwang | 42:030e0ec4eac5 | 66 | float sin_p = sinf(foc.p); |
bwang | 42:030e0ec4eac5 | 67 | float cos_p = cosf(foc.p); |
bwang | 42:030e0ec4eac5 | 68 | |
bwang | 49:da8604278d76 | 69 | /*scale and offset currents (adval1, 2 are updated in ISR)*/ |
bwang | 42:030e0ec4eac5 | 70 | foc.ia = ((float) read.adval1 / 4096.0f * AVDD - I_OFFSET - read.ia_supp_offset) / I_SCALE; |
bwang | 42:030e0ec4eac5 | 71 | foc.ib = ((float) read.adval2 / 4096.0f * AVDD - I_OFFSET - read.ib_supp_offset) / I_SCALE; |
bwang | 42:030e0ec4eac5 | 72 | |
bwang | 42:030e0ec4eac5 | 73 | /*compute d, q*/ |
bwang | 42:030e0ec4eac5 | 74 | clarke(foc.ia, foc.ib, &foc.alpha, &foc.beta); |
bwang | 42:030e0ec4eac5 | 75 | park(foc.alpha, foc.beta, sin_p, cos_p, &foc.d, &foc.q); |
bwang | 42:030e0ec4eac5 | 76 | |
bwang | 42:030e0ec4eac5 | 77 | /*PI controller*/ |
bwang | 44:3fd6a43b91f0 | 78 | control.d_filtered = update_filter(control.d_filtered, foc.d, DQ_FILTER_STRENGTH); |
bwang | 44:3fd6a43b91f0 | 79 | control.q_filtered = update_filter(control.q_filtered, foc.q, DQ_FILTER_STRENGTH); |
bwang | 42:030e0ec4eac5 | 80 | |
bwang | 42:030e0ec4eac5 | 81 | float d_err = control.d_ref - control.d_filtered; |
bwang | 42:030e0ec4eac5 | 82 | float q_err = control.q_ref - control.q_filtered; |
bwang | 42:030e0ec4eac5 | 83 | |
bwang | 58:7316c5a4c417 | 84 | control.d_integral += d_err * KI_D; |
bwang | 58:7316c5a4c417 | 85 | control.q_integral += q_err * KI_Q; |
bwang | 42:030e0ec4eac5 | 86 | |
bwang | 131:031df63c7dbc | 87 | constrain_norm(&control.d_integral, &control.q_integral, 1.0f, 1.0f, INTEGRAL_MAX); |
bwang | 131:031df63c7dbc | 88 | |
bwang | 131:031df63c7dbc | 89 | foc.vd_decouple = -Lq * POLE_PAIRS * read.w * foc.q / BUS_VOLTAGE / 2.0f; |
bwang | 131:031df63c7dbc | 90 | foc.vq_decouple = Ld * POLE_PAIRS * read.w * foc.d / BUS_VOLTAGE / 2.0f; |
bwang | 42:030e0ec4eac5 | 91 | |
bwang | 158:882f9c208378 | 92 | constrain_norm(&foc.vd_decouple, &foc.vq_decouple, 1.0f, 1.0f, 1.0f); |
bwang | 131:031df63c7dbc | 93 | |
bwang | 131:031df63c7dbc | 94 | foc.vd = KP_D * d_err + control.d_integral;// + foc.vd_decouple; |
bwang | 131:031df63c7dbc | 95 | foc.vq = KP_Q * q_err + control.q_integral;// + foc.vq_decouple; |
bwang | 42:030e0ec4eac5 | 96 | |
bwang | 158:882f9c208378 | 97 | constrain_norm(&foc.vd, &foc.vq, 1.0f, 1.0f, 1.0f + OVERMODULATION_FACTOR); |
bwang | 42:030e0ec4eac5 | 98 | |
bwang | 42:030e0ec4eac5 | 99 | if (!control_enabled) { |
bwang | 42:030e0ec4eac5 | 100 | foc.vd = 0.0f; |
bwang | 42:030e0ec4eac5 | 101 | foc.vq = 0.0f; |
bwang | 42:030e0ec4eac5 | 102 | } |
bwang | 42:030e0ec4eac5 | 103 | |
bwang | 126:498f56ba051e | 104 | float pv = foc.p + read.w / V_PHASE_SWIZZLE; |
bwang | 124:e70ca81676fc | 105 | float sin_pv = sinf(pv); |
bwang | 124:e70ca81676fc | 106 | float cos_pv = cosf(pv); |
bwang | 124:e70ca81676fc | 107 | |
bwang | 42:030e0ec4eac5 | 108 | /*inverse transforms*/ |
bwang | 124:e70ca81676fc | 109 | invpark(foc.vd, foc.vq, sin_pv, cos_pv, &foc.valpha, &foc.vbeta); |
bwang | 42:030e0ec4eac5 | 110 | |
bwang | 42:030e0ec4eac5 | 111 | float va, vb, vc, voff; |
bwang | 42:030e0ec4eac5 | 112 | |
bwang | 42:030e0ec4eac5 | 113 | invclarke(foc.valpha, foc.vbeta, &va, &vb); |
bwang | 42:030e0ec4eac5 | 114 | vc = -va - vb; |
bwang | 42:030e0ec4eac5 | 115 | |
bwang | 42:030e0ec4eac5 | 116 | /*SVPWM*/ |
bwang | 44:3fd6a43b91f0 | 117 | voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;//don't think about it |
bwang | 42:030e0ec4eac5 | 118 | va = va - voff; |
bwang | 42:030e0ec4eac5 | 119 | vb = vb - voff; |
bwang | 42:030e0ec4eac5 | 120 | vc = vc - voff; |
bwang | 42:030e0ec4eac5 | 121 | |
bwang | 56:c681001dfa46 | 122 | /*safety checks, reset integral*/ |
bwang | 56:c681001dfa46 | 123 | if (!io.throttle_in->get_enabled() || !io.pos->IsValid() || !is_driving()) { |
bwang | 56:c681001dfa46 | 124 | /*do this even in disabled state, to keep integral down*/ |
bwang | 56:c681001dfa46 | 125 | go_disabled(); |
bwang | 56:c681001dfa46 | 126 | } |
bwang | 56:c681001dfa46 | 127 | |
bwang | 42:030e0ec4eac5 | 128 | /*output to timers*/ |
bwang | 157:a9b2002994d5 | 129 | set_dtc(io.a, 0.5f + 0.5f * va * LINEAR_MODULATION_MAX); |
bwang | 157:a9b2002994d5 | 130 | set_dtc(io.b, 0.5f + 0.5f * vb * LINEAR_MODULATION_MAX); |
bwang | 157:a9b2002994d5 | 131 | set_dtc(io.c, 0.5f + 0.5f * vc * LINEAR_MODULATION_MAX); |
bwang | 42:030e0ec4eac5 | 132 | } |
bwang | 42:030e0ec4eac5 | 133 | |
bwang | 44:3fd6a43b91f0 | 134 | void slow_loop() { |
bwang | 74:f10cb573d7ca | 135 | float x = io.throttle_in->get_throttle(); |
bwang | 154:0a22dcf91577 | 136 | x = control.throttle_filter->update(x); |
bwang | 152:6877dceec871 | 137 | control.torque_percent = th->map(x, read.w); |
bwang | 70:5e39beeb4a21 | 138 | dq->map(control.torque_percent, read.w, &control.d_ref, &control.q_ref); |
bwang | 44:3fd6a43b91f0 | 139 | } |
bwang | 44:3fd6a43b91f0 | 140 | |
bwang | 42:030e0ec4eac5 | 141 | void go_enabled() { |
bwang | 42:030e0ec4eac5 | 142 | control_enabled = true; |
bwang | 42:030e0ec4eac5 | 143 | io.en->write(1); |
bwang | 42:030e0ec4eac5 | 144 | } |
bwang | 42:030e0ec4eac5 | 145 | |
bwang | 42:030e0ec4eac5 | 146 | void go_disabled() { |
bwang | 52:fd3d8df99287 | 147 | control.d_integral = 0.0f; |
bwang | 52:fd3d8df99287 | 148 | control.q_integral = 0.0f; |
bwang | 42:030e0ec4eac5 | 149 | control_enabled = false; |
bwang | 42:030e0ec4eac5 | 150 | io.en->write(0); |
bwang | 42:030e0ec4eac5 | 151 | } |
bwang | 42:030e0ec4eac5 | 152 | |
bwang | 52:fd3d8df99287 | 153 | bool is_driving() { |
bwang | 154:0a22dcf91577 | 154 | return control.torque_percent > 0.01f || fabsf(read.w) > W_SAFE; |
bwang | 52:fd3d8df99287 | 155 | } |
bwang | 52:fd3d8df99287 | 156 | |
bwang | 44:3fd6a43b91f0 | 157 | float update_filter(float old, float x, float str) { |
bwang | 42:030e0ec4eac5 | 158 | return str * old + (1.0f - str) * x; |
bwang | 42:030e0ec4eac5 | 159 | } |
dicarloj | 13:41d102a53caf | 160 | |
bwang | 72:5f1da97d62e1 | 161 | void log() { |
bwang | 161:19eac809c727 | 162 | 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), |
bwang | 161:19eac809c727 | 163 | (int) (255 * foc.vd), (int) (255 * foc.vq)); |
bwang | 161:19eac809c727 | 164 | //io.pc->printf("%c,%c\n", (int) (control.d_filtered / 2.0f), (int) (control.q_filtered / 2.0f)); |
bwang | 143:a7a0c9d70e8a | 165 | //io.pc->printf("%d,%d,%d,%d,%d\n", (int) read.w, (int) control.d_filtered, (int) control.q_filtered, (int) (255 * foc.vd), (int) (255 * foc.vq)); |
bwang | 72:5f1da97d62e1 | 166 | wait(1.0f / LOG_FREQUENCY); |
bwang | 70:5e39beeb4a21 | 167 | } |
bwang | 70:5e39beeb4a21 | 168 | |
bwang | 1:7b61790f6be9 | 169 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
bwang | 75:591556ce033d | 170 | int start_state = io.throttle_in->state(); |
bwang | 91:f58472ac3fae | 171 | |
bwang | 1:7b61790f6be9 | 172 | if (TIM1->SR & TIM_SR_UIF ) { |
bwang | 4:a6669248ce4d | 173 | ADC1->CR2 |= 0x40000000; |
bwang | 91:f58472ac3fae | 174 | volatile int delay; |
bwang | 91:f58472ac3fae | 175 | for (delay = 0; delay < 35; delay++); |
bwang | 91:f58472ac3fae | 176 | |
bwang | 42:030e0ec4eac5 | 177 | read.adval1 = ADC1->DR; |
bwang | 42:030e0ec4eac5 | 178 | read.adval2 = ADC2->DR; |
bwang | 91:f58472ac3fae | 179 | |
bwang | 4:a6669248ce4d | 180 | commutate(); |
bwang | 1:7b61790f6be9 | 181 | } |
bwang | 1:7b61790f6be9 | 182 | TIM1->SR = 0x00; |
bwang | 75:591556ce033d | 183 | int end_state = io.throttle_in->state(); |
bwang | 75:591556ce033d | 184 | if (start_state != end_state) io.throttle_in->block(); |
bwang | 1:7b61790f6be9 | 185 | } |
bwang | 1:7b61790f6be9 | 186 | |
bwang | 166:4637785ba01e | 187 | int main() { |
bwang | 166:4637785ba01e | 188 | dq = new LutMapper(); |
bwang | 167:7f1f7dd969e2 | 189 | th = new AutoThrottleMapper();//NullThrottleMapper(); |
bwang | 150:08c13bfc7417 | 190 | |
bwang | 47:1c9868e226d0 | 191 | BREMSInit(&io, &read, &foc, &control, false); |
bwang | 48:a1a09c83d42c | 192 | |
bwang | 0:bac9c3a3a6ca | 193 | for (;;) { |
bwang | 72:5f1da97d62e1 | 194 | if (ENABLE_LOGGING) { |
bwang | 72:5f1da97d62e1 | 195 | log(); |
bwang | 72:5f1da97d62e1 | 196 | } |
bwang | 0:bac9c3a3a6ca | 197 | } |
bwang | 42:030e0ec4eac5 | 198 | } |