robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Thu May 04 17:35:10 2017 +0000
Revision:
155:7c6005933d4c
Parent:
152:6877dceec871
Child:
157:a9b2002994d5
05/04/2017 13:34 - changed logic in PwmIn::handle_fall, which no longer resets timer on invalid edges

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"
bwang118:2b6dab10b69d 11#include "Calibration.h"
bwang150:08c13bfc7417 12#include "Filter.h"
bwang42:030e0ec4eac5 13
bwang42:030e0ec4eac5 14#include "BREMSStructs.h"
bwang42:030e0ec4eac5 15#include "BREMSConfig.h"
bwang30:c25c5bf0d951 16
bwang15:b583cd30b063 17#include "config_motor.h"
bwang15:b583cd30b063 18#include "config_loop.h"
bwang19:a6cf15f89f3d 19#include "config_pins.h"
bwang15:b583cd30b063 20#include "config_inverter.h"
bwang29:50e6e4e46580 21#include "config_driving.h"
bwang72:5f1da97d62e1 22#include "config_logging.h"
bwang0:bac9c3a3a6ca 23
bwang42:030e0ec4eac5 24#include "main.h"
bwang0:bac9c3a3a6ca 25
bwang42:030e0ec4eac5 26IOStruct io;
bwang42:030e0ec4eac5 27ReadDataStruct read;
bwang42:030e0ec4eac5 28FOCStruct foc;
bwang42:030e0ec4eac5 29ControlStruct control;
bwang0:bac9c3a3a6ca 30
bwang42:030e0ec4eac5 31DQMapper *dq;
bwang42:030e0ec4eac5 32ThrottleMapper *th;
bwang2:eabe8feaaabb 33
bwang44:3fd6a43b91f0 34int loop_counter = 0;
bwang25:3f2b585ae72d 35bool control_enabled = false;
dicarloj13:41d102a53caf 36
bwang42:030e0ec4eac5 37void update_velocity() {
bwang42:030e0ec4eac5 38 read.last_p_mech = read.p_mech;
bwang42:030e0ec4eac5 39 read.p_mech = io.pos->GetMechPosition();
bwang44:3fd6a43b91f0 40
bwang42:030e0ec4eac5 41 float dp_mech = read.p_mech - read.last_p_mech;
bwang42:030e0ec4eac5 42 if (dp_mech < -PI) dp_mech += 2 * PI;
bwang42:030e0ec4eac5 43 if (dp_mech > PI) dp_mech -= 2 * PI;
bwang44:3fd6a43b91f0 44
bwang42:030e0ec4eac5 45 float w_raw = dp_mech * F_SW; //rad/s
bwang44:3fd6a43b91f0 46
bwang154:0a22dcf91577 47 read.w = control.velocity_filter->update(w_raw);
bwang42:030e0ec4eac5 48}
bwang2:eabe8feaaabb 49
bwang118:2b6dab10b69d 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*/
bwang130:639cd8586f86 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
bwang131:031df63c7dbc 87 constrain_norm(&control.d_integral, &control.q_integral, 1.0f, 1.0f, INTEGRAL_MAX);
bwang42:030e0ec4eac5 88
bwang131:031df63c7dbc 89 foc.vd_decouple = -Lq * POLE_PAIRS * read.w * foc.q / BUS_VOLTAGE / 2.0f;
bwang131:031df63c7dbc 90 foc.vq_decouple = Ld * POLE_PAIRS * read.w * foc.d / BUS_VOLTAGE / 2.0f;
bwang131:031df63c7dbc 91
bwang131:031df63c7dbc 92 constrain_norm(&foc.vd_decouple, &foc.vq_decouple, 1.0f, 1.0f, 1.0f);
bwang131:031df63c7dbc 93
bwang131:031df63c7dbc 94 foc.vd = KP_D * d_err + control.d_integral;// + foc.vd_decouple;
bwang131:031df63c7dbc 95 foc.vq = KP_Q * q_err + control.q_integral;// + foc.vq_decouple;
bwang42:030e0ec4eac5 96
bwang64:b4175385d718 97 constrain_norm(&foc.vd, &foc.vq, 1.0f, 1.0f, 1.0f);
bwang42:030e0ec4eac5 98
bwang42:030e0ec4eac5 99 if (!control_enabled) {
bwang42:030e0ec4eac5 100 foc.vd = 0.0f;
bwang42:030e0ec4eac5 101 foc.vq = 0.0f;
bwang42:030e0ec4eac5 102 }
bwang42:030e0ec4eac5 103
bwang126:498f56ba051e 104 float pv = foc.p + read.w / V_PHASE_SWIZZLE;
bwang124:e70ca81676fc 105 float sin_pv = sinf(pv);
bwang124:e70ca81676fc 106 float cos_pv = cosf(pv);
bwang124:e70ca81676fc 107
bwang42:030e0ec4eac5 108 /*inverse transforms*/
bwang124:e70ca81676fc 109 invpark(foc.vd, foc.vq, sin_pv, cos_pv, &foc.valpha, &foc.vbeta);
bwang42:030e0ec4eac5 110
bwang42:030e0ec4eac5 111 float va, vb, vc, voff;
bwang42:030e0ec4eac5 112
bwang42:030e0ec4eac5 113 invclarke(foc.valpha, foc.vbeta, &va, &vb);
bwang42:030e0ec4eac5 114 vc = -va - vb;
bwang42:030e0ec4eac5 115
bwang42:030e0ec4eac5 116 /*SVPWM*/
bwang44:3fd6a43b91f0 117 voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;//don't think about it
bwang42:030e0ec4eac5 118 va = va - voff;
bwang42:030e0ec4eac5 119 vb = vb - voff;
bwang42:030e0ec4eac5 120 vc = vc - voff;
bwang42:030e0ec4eac5 121
bwang56:c681001dfa46 122 /*safety checks, reset integral*/
bwang56:c681001dfa46 123 if (!io.throttle_in->get_enabled() || !io.pos->IsValid() || !is_driving()) {
bwang56:c681001dfa46 124 /*do this even in disabled state, to keep integral down*/
bwang56:c681001dfa46 125 go_disabled();
bwang56:c681001dfa46 126 }
bwang56:c681001dfa46 127
bwang42:030e0ec4eac5 128 /*output to timers*/
bwang93:e7e45957f496 129 set_dtc(io.a, 0.5f + 0.5f * va * MODULATION_MAX);
bwang93:e7e45957f496 130 set_dtc(io.b, 0.5f + 0.5f * vb * MODULATION_MAX);
bwang93:e7e45957f496 131 set_dtc(io.c, 0.5f + 0.5f * vc * MODULATION_MAX);
bwang42:030e0ec4eac5 132}
bwang42:030e0ec4eac5 133
bwang44:3fd6a43b91f0 134void slow_loop() {
bwang74:f10cb573d7ca 135 float x = io.throttle_in->get_throttle();
bwang154:0a22dcf91577 136 x = control.throttle_filter->update(x);
bwang152:6877dceec871 137 control.torque_percent = th->map(x, read.w);
bwang70:5e39beeb4a21 138 dq->map(control.torque_percent, read.w, &control.d_ref, &control.q_ref);
bwang44:3fd6a43b91f0 139}
bwang44:3fd6a43b91f0 140
bwang42:030e0ec4eac5 141void go_enabled() {
bwang42:030e0ec4eac5 142 control_enabled = true;
bwang42:030e0ec4eac5 143 io.en->write(1);
bwang42:030e0ec4eac5 144}
bwang42:030e0ec4eac5 145
bwang42:030e0ec4eac5 146void go_disabled() {
bwang52:fd3d8df99287 147 control.d_integral = 0.0f;
bwang52:fd3d8df99287 148 control.q_integral = 0.0f;
bwang42:030e0ec4eac5 149 control_enabled = false;
bwang42:030e0ec4eac5 150 io.en->write(0);
bwang42:030e0ec4eac5 151}
bwang42:030e0ec4eac5 152
bwang52:fd3d8df99287 153bool is_driving() {
bwang154:0a22dcf91577 154 return control.torque_percent > 0.01f || fabsf(read.w) > W_SAFE;
bwang52:fd3d8df99287 155}
bwang52:fd3d8df99287 156
bwang44:3fd6a43b91f0 157float update_filter(float old, float x, float str) {
bwang42:030e0ec4eac5 158 return str * old + (1.0f - str) * x;
bwang42:030e0ec4eac5 159}
dicarloj13:41d102a53caf 160
bwang72:5f1da97d62e1 161void log() {
bwang154:0a22dcf91577 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),
bwang154:0a22dcf91577 163 (int) (255 * foc.vd), (int) (255 * foc.vq));
bwang143:a7a0c9d70e8a 164 //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));
bwang72:5f1da97d62e1 165 wait(1.0f / LOG_FREQUENCY);
bwang70:5e39beeb4a21 166}
bwang70:5e39beeb4a21 167
bwang1:7b61790f6be9 168extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
bwang75:591556ce033d 169 int start_state = io.throttle_in->state();
bwang91:f58472ac3fae 170
bwang1:7b61790f6be9 171 if (TIM1->SR & TIM_SR_UIF ) {
bwang4:a6669248ce4d 172 ADC1->CR2 |= 0x40000000;
bwang91:f58472ac3fae 173 volatile int delay;
bwang91:f58472ac3fae 174 for (delay = 0; delay < 35; delay++);
bwang91:f58472ac3fae 175
bwang42:030e0ec4eac5 176 read.adval1 = ADC1->DR;
bwang42:030e0ec4eac5 177 read.adval2 = ADC2->DR;
bwang91:f58472ac3fae 178
bwang4:a6669248ce4d 179 commutate();
bwang1:7b61790f6be9 180 }
bwang1:7b61790f6be9 181 TIM1->SR = 0x00;
bwang75:591556ce033d 182 int end_state = io.throttle_in->state();
bwang75:591556ce033d 183 if (start_state != end_state) io.throttle_in->block();
bwang1:7b61790f6be9 184}
bwang1:7b61790f6be9 185
bwang0:bac9c3a3a6ca 186int main() {
bwang132:101b74e4763a 187 dq = new LutMapper();
bwang45:cf8ad81fb0f0 188 th = new NullThrottleMapper();
bwang150:08c13bfc7417 189
bwang47:1c9868e226d0 190 BREMSInit(&io, &read, &foc, &control, false);
bwang48:a1a09c83d42c 191
bwang0:bac9c3a3a6ca 192 for (;;) {
bwang72:5f1da97d62e1 193 if (ENABLE_LOGGING) {
bwang72:5f1da97d62e1 194 log();
bwang72:5f1da97d62e1 195 }
bwang0:bac9c3a3a6ca 196 }
bwang42:030e0ec4eac5 197}