robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Sat Mar 11 08:51:04 2017 +0000
Revision:
83:eb3704d4943f
Parent:
82:5e741c5ffd9f
Child:
84:dd32640942a4
more structural changes

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 82:5e741c5ffd9f 11 #include "PreferenceWriter.h"
bwang 82:5e741c5ffd9f 12 #include "CommandProcessor.h"
bwang 42:030e0ec4eac5 13
bwang 42:030e0ec4eac5 14 #include "BREMSStructs.h"
bwang 42:030e0ec4eac5 15 #include "BREMSConfig.h"
bwang 30:c25c5bf0d951 16
bwang 82:5e741c5ffd9f 17 #include "config.h"
bwang 15:b583cd30b063 18 #include "config_motor.h"
bwang 15:b583cd30b063 19 #include "config_loop.h"
bwang 19:a6cf15f89f3d 20 #include "config_pins.h"
bwang 15:b583cd30b063 21 #include "config_inverter.h"
bwang 29:50e6e4e46580 22 #include "config_driving.h"
bwang 72:5f1da97d62e1 23 #include "config_logging.h"
bwang 0:bac9c3a3a6ca 24
bwang 42:030e0ec4eac5 25 #include "main.h"
bwang 0:bac9c3a3a6ca 26
bwang 42:030e0ec4eac5 27 IOStruct io;
bwang 42:030e0ec4eac5 28 ReadDataStruct read;
bwang 42:030e0ec4eac5 29 FOCStruct foc;
bwang 42:030e0ec4eac5 30 ControlStruct control;
bwang 2:eabe8feaaabb 31
bwang 42:030e0ec4eac5 32 DQMapper *dq;
bwang 42:030e0ec4eac5 33 ThrottleMapper *th;
bwang 2:eabe8feaaabb 34
bwang 44:3fd6a43b91f0 35 int loop_counter = 0;
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 42:030e0ec4eac5 46 if (w_raw > W_CRAZY) w_raw = read.w; //with this limiting scheme noise < 0
bwang 42:030e0ec4eac5 47 if (w_raw < -W_CRAZY) w_raw = read.w; //so we need to throw out the large deltas first
bwang 44:3fd6a43b91f0 48
bwang 44:3fd6a43b91f0 49 read.w = update_filter(read.w, w_raw, W_FILTER_STRENGTH);
bwang 42:030e0ec4eac5 50 }
bwang 2:eabe8feaaabb 51
bwang 75:591556ce033d 52 void commutate() {
bwang 56:c681001dfa46 53 /*safety checks, do we do anything this cycle?*/
bwang 82:5e741c5ffd9f 54 if (!control.enabled && io.throttle_in->get_enabled() && io.pos->IsValid() && is_driving()) {
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 82:5e741c5ffd9f 79 /*get references*/
bwang 82:5e741c5ffd9f 80 /*happens here, as we may want other controllers running each cycle*/
bwang 82:5e741c5ffd9f 81 dq->map(control.torque_percent, read.w, &control.d_ref, &control.q_ref);
bwang 82:5e741c5ffd9f 82
bwang 42:030e0ec4eac5 83 /*PI controller*/
bwang 44:3fd6a43b91f0 84 control.d_filtered = update_filter(control.d_filtered, foc.d, DQ_FILTER_STRENGTH);
bwang 44:3fd6a43b91f0 85 control.q_filtered = update_filter(control.q_filtered, foc.q, DQ_FILTER_STRENGTH);
bwang 42:030e0ec4eac5 86
bwang 42:030e0ec4eac5 87 float d_err = control.d_ref - control.d_filtered;
bwang 42:030e0ec4eac5 88 float q_err = control.q_ref - control.q_filtered;
bwang 42:030e0ec4eac5 89
bwang 58:7316c5a4c417 90 control.d_integral += d_err * KI_D;
bwang 58:7316c5a4c417 91 control.q_integral += q_err * KI_Q;
bwang 42:030e0ec4eac5 92
bwang 64:b4175385d718 93 constrain_norm(&control.d_integral, &control.q_integral, 0.5f, 1.0f, 1.0f);
bwang 42:030e0ec4eac5 94
bwang 58:7316c5a4c417 95 foc.vd = KP_D * d_err + control.d_integral;// - Lq * POLE_PAIRS * read.w * foc.q / BUS_VOLTAGE / 2.0f;
bwang 58:7316c5a4c417 96 foc.vq = KP_Q * q_err + control.q_integral;// + Ld * POLE_PAIRS * read.w * foc.d / BUS_VOLTAGE / 2.0f;
bwang 42:030e0ec4eac5 97
bwang 64:b4175385d718 98 constrain_norm(&foc.vd, &foc.vq, 1.0f, 1.0f, 1.0f);
bwang 42:030e0ec4eac5 99
bwang 82:5e741c5ffd9f 100 if (!control.enabled) {
bwang 42:030e0ec4eac5 101 foc.vd = 0.0f;
bwang 42:030e0ec4eac5 102 foc.vq = 0.0f;
bwang 42:030e0ec4eac5 103 }
bwang 42:030e0ec4eac5 104
bwang 42:030e0ec4eac5 105 /*inverse transforms*/
bwang 42:030e0ec4eac5 106 invpark(foc.vd, foc.vq, sin_p, cos_p, &foc.valpha, &foc.vbeta);
bwang 42:030e0ec4eac5 107
bwang 42:030e0ec4eac5 108 float va, vb, vc, voff;
bwang 42:030e0ec4eac5 109
bwang 42:030e0ec4eac5 110 invclarke(foc.valpha, foc.vbeta, &va, &vb);
bwang 42:030e0ec4eac5 111 vc = -va - vb;
bwang 42:030e0ec4eac5 112
bwang 42:030e0ec4eac5 113 /*SVPWM*/
bwang 44:3fd6a43b91f0 114 voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;//don't think about it
bwang 42:030e0ec4eac5 115 va = va - voff;
bwang 42:030e0ec4eac5 116 vb = vb - voff;
bwang 42:030e0ec4eac5 117 vc = vc - voff;
bwang 42:030e0ec4eac5 118
bwang 56:c681001dfa46 119 /*safety checks, reset integral*/
bwang 56:c681001dfa46 120 if (!io.throttle_in->get_enabled() || !io.pos->IsValid() || !is_driving()) {
bwang 56:c681001dfa46 121 /*do this even in disabled state, to keep integral down*/
bwang 56:c681001dfa46 122 go_disabled();
bwang 56:c681001dfa46 123 }
bwang 56:c681001dfa46 124
bwang 42:030e0ec4eac5 125 /*output to timers*/
bwang 47:1c9868e226d0 126 set_dtc(io.a, 0.5f + 0.5f * va);
bwang 47:1c9868e226d0 127 set_dtc(io.b, 0.5f + 0.5f * vb);
bwang 47:1c9868e226d0 128 set_dtc(io.c, 0.5f + 0.5f * vc);
bwang 42:030e0ec4eac5 129 }
bwang 42:030e0ec4eac5 130
bwang 44:3fd6a43b91f0 131 void slow_loop() {
bwang 82:5e741c5ffd9f 132 switch (BREMS_src) {
bwang 82:5e741c5ffd9f 133 case CMD_SRC_RC:
bwang 83:eb3704d4943f 134 control.user_cmd = update_filter(control.user_cmd, io.throttle_in->get_throttle(), THROTTLE_FILTER_STRENGTH);
bwang 82:5e741c5ffd9f 135 break;
bwang 82:5e741c5ffd9f 136 case CMD_SRC_ANALOG:
bwang 82:5e741c5ffd9f 137 //analog code here;
bwang 82:5e741c5ffd9f 138 break;
bwang 82:5e741c5ffd9f 139 case CMD_SRC_TERMINAL:
bwang 82:5e741c5ffd9f 140 case CMD_SRC_SERIAL:
bwang 82:5e741c5ffd9f 141 case CMD_SRC_CAN:
bwang 82:5e741c5ffd9f 142 case CMD_SRC_INTERNAL:
bwang 82:5e741c5ffd9f 143 default:
bwang 83:eb3704d4943f 144 //these sources are updated asynchronously via interrupts
bwang 82:5e741c5ffd9f 145 break;
bwang 82:5e741c5ffd9f 146 }
bwang 82:5e741c5ffd9f 147 switch (BREMS_op) {
bwang 82:5e741c5ffd9f 148 case OP_TORQUE:
bwang 83:eb3704d4943f 149 control.torque_percent = control.user_cmd;
bwang 82:5e741c5ffd9f 150 break;
bwang 82:5e741c5ffd9f 151 case OP_DRIVING:
bwang 83:eb3704d4943f 152 control.torque_percent = th->map(control.user_cmd, read.w);
bwang 82:5e741c5ffd9f 153 break;
bwang 82:5e741c5ffd9f 154 case OP_SPEED:
bwang 82:5e741c5ffd9f 155 //set velocity setpoint here;
bwang 82:5e741c5ffd9f 156 break;
bwang 82:5e741c5ffd9f 157 case OP_POSITION:
bwang 82:5e741c5ffd9f 158 //set pos setpoint here;
bwang 82:5e741c5ffd9f 159 break;
bwang 82:5e741c5ffd9f 160 }
bwang 70:5e39beeb4a21 161 }
bwang 1:7b61790f6be9 162
bwang 0:bac9c3a3a6ca 163 int main() {
bwang 59:0416da6c3060 164 dq = new LutMapper();
bwang 45:cf8ad81fb0f0 165 th = new NullThrottleMapper();
bwang 82:5e741c5ffd9f 166
bwang 82:5e741c5ffd9f 167 BREMSInit(&io, &read, &foc, &control);
bwang 82:5e741c5ffd9f 168 io.pc->attach(rxCallback);
bwang 48:a1a09c83d42c 169
bwang 0:bac9c3a3a6ca 170 for (;;) {
bwang 0:bac9c3a3a6ca 171 }
bwang 42:030e0ec4eac5 172 }