robot

Dependencies:   FastPWM3 mbed

main.cpp

Committer:
bwang
Date:
2017-03-12
Revision:
84:dd32640942a4
Parent:
83:eb3704d4943f
Child:
86:b059f637e9ac

File content as of revision 84:dd32640942a4:

#include "mbed.h"
#include "math.h"

#include "PositionSensor.h"
#include "FastPWM.h"
#include "PwmIn.h"
#include "MathHelpers.h"
#include "Transforms.h"
#include "DQMapper.h"
#include "ThrottleMapper.h"
#include "PreferenceWriter.h"
#include "CommandProcessor.h"
#include "Preferences.h"

#include "BREMSStructs.h"
#include "BREMSConfig.h"

#include "config_motor.h"
#include "config_loop.h"
#include "config_pins.h"
#include "config_inverter.h"
#include "config_driving.h"
#include "config_logging.h"

#include "main.h"

IOStruct io;
ReadDataStruct read;
FOCStruct foc;
ControlStruct control;

DQMapper *dq;
ThrottleMapper *th;

int loop_counter = 0;

void update_velocity() {
    read.last_p_mech = read.p_mech;
    read.p_mech = io.pos->GetMechPosition();
    
    float dp_mech = read.p_mech - read.last_p_mech;
    if (dp_mech < -PI) dp_mech += 2 * PI;
    if (dp_mech >  PI) dp_mech -= 2 * PI;
    
    float w_raw = dp_mech * F_SW; //rad/s
    if (w_raw > W_CRAZY) w_raw = read.w; //with this limiting scheme noise < 0
    if (w_raw < -W_CRAZY) w_raw = read.w; //so we need to throw out the large deltas first
    
    read.w = update_filter(read.w, w_raw, W_FILTER_STRENGTH);
}

void commutate() {    
    /*safety checks, do we do anything this cycle?*/
    if (!control.enabled && checks_passed()) {
        go_enabled();
    }
    
    /*update velocity, references*/
    update_velocity();
    if (loop_counter % SLOW_LOOP_COUNTER == 0) {
        loop_counter = 0;
        slow_loop();
    }
    loop_counter++;
    
    /*update position, sin, cos*/
    foc.p = io.pos->GetElecPosition() - POS_OFFSET;
    float sin_p = sinf(foc.p);
    float cos_p = cosf(foc.p);
    
    /*scale and offset currents (adval1, 2 are updated in ISR)*/    
    foc.ia = ((float) read.adval1 / 4096.0f * AVDD - I_OFFSET - read.ia_supp_offset) / I_SCALE;
    foc.ib = ((float) read.adval2 / 4096.0f * AVDD - I_OFFSET - read.ib_supp_offset) / I_SCALE;
    
    /*compute d, q*/
    clarke(foc.ia, foc.ib, &foc.alpha, &foc.beta);
    park(foc.alpha, foc.beta, sin_p, cos_p, &foc.d, &foc.q);
    
    /*run additional controllers; the output of every controller is a torque percent*/
    switch (BREMS_op) {
    case OP_TORQUE:
        control.torque_percent = control.user_cmd;
        break;
    case OP_DRIVING:
        control.torque_percent = th->map(control.user_cmd, read.w);
        break;
    case OP_SPEED:
        //set velocity setpoint here;
        break;
    case OP_POSITION:
        //set pos setpoint here;
        break;
    }
    
    /*get references*/
    /*happens here, as we may want other controllers running each cycle*/
    dq->map(control.torque_percent, read.w, &control.d_ref, &control.q_ref);
    
    /*PI controller*/
    control.d_filtered = update_filter(control.d_filtered, foc.d, DQ_FILTER_STRENGTH);
    control.q_filtered = update_filter(control.q_filtered, foc.q, DQ_FILTER_STRENGTH);
        
    float d_err = control.d_ref - control.d_filtered;
    float q_err = control.q_ref - control.q_filtered;
    
    control.d_integral += d_err * KI_D;
    control.q_integral += q_err * KI_Q;
    
    constrain_norm(&control.d_integral, &control.q_integral, 0.5f, 1.0f, 1.0f);
    
    foc.vd = KP_D * d_err + control.d_integral;// - Lq * POLE_PAIRS * read.w * foc.q / BUS_VOLTAGE / 2.0f;
    foc.vq = KP_Q * q_err + control.q_integral;// + Ld * POLE_PAIRS * read.w * foc.d / BUS_VOLTAGE / 2.0f;
    
    constrain_norm(&foc.vd, &foc.vq, 1.0f, 1.0f, 1.0f);
    
    if (!control.enabled) {
        foc.vd = 0.0f;
        foc.vq = 0.0f;
    }
    
    /*inverse transforms*/
    invpark(foc.vd, foc.vq, sin_p, cos_p, &foc.valpha, &foc.vbeta);
    
    float va, vb, vc, voff;
    
    invclarke(foc.valpha, foc.vbeta, &va, &vb);
    vc = -va - vb;
    
    /*SVPWM*/
    voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;//don't think about it
    va = va - voff;
    vb = vb - voff;
    vc = vc - voff;
    
    /*safety checks, reset integral*/
    if (!checks_passed()) {
        go_disabled();
    }
    
    /*output to timers*/
    switch (BREMS_mode) {
    case MODE_CFG:
        set_dtc(io.a, 0.5f);
        set_dtc(io.b, 0.5f);
        set_dtc(io.c, 0.5f);
        break;
    case MODE_RUN:
        set_dtc(io.a, 0.5f + 0.5f * va);
        set_dtc(io.b, 0.5f + 0.5f * vb);
        set_dtc(io.c, 0.5f + 0.5f * vc);
        break;
    case MODE_ZERO:
        //zeroing code here;
        break;
    case MODE_CHR:
        //i have no idea lol;
        break;
    }
}

void slow_loop() {
    switch (BREMS_src) {
    case CMD_SRC_RC:
        control.user_cmd = update_filter(control.user_cmd, io.throttle_in->get_throttle(), THROTTLE_FILTER_STRENGTH);
        break;
    case CMD_SRC_ANALOG:
        //analog code here;
        break;
    case CMD_SRC_TERMINAL:
    case CMD_SRC_SERIAL:
    case CMD_SRC_CAN:
    case CMD_SRC_INTERNAL:
    default:
        //these sources are updated asynchronously via interrupts
        break;
    }
}

int main() {
    dq = new LutMapper();
    th = new NullThrottleMapper();

    BREMSInit(&io, &read, &foc, &control);
    io.pc->attach(rxCallback);
    
    for (;;) {
    }
}