Prius IPM controller

Dependencies:   mbed

Fork of analoghalls5_5 by N K

context.cpp

Committer:
bwang
Date:
2015-03-09
Revision:
16:fd6771a6685a
Parent:
15:c10d0aa3c0e1
Child:
19:280c62c71cf8

File content as of revision 16:fd6771a6685a:

#include "includes.h"
#include "core.h"
#include "meta.h"
#include "sensors.h"
#include "filters.h"
#include "context.h"

float debug_registers[16];

Context::Context() {
    _index = 0;
    _time_upd_ticker = new Ticker();
    _time_upd_ticker->attach_us(this, &Context::upd_function, 50);
}

void Context::ConfigureOutputs(PinName oa, PinName ob, PinName oc, PinName en) {
    _oa = oa;
    _ob = ob;
    _oc = oc;
    _en = en;
}

void Context::ConfigureCurrentSensors(PinName ib_pin, PinName ic_pin, float scale, float filter_strength) {
    _ib_pin = ib_pin;
    _ic_pin = ic_pin;
    _scale = scale;
    _filter_strength = filter_strength;
}

void Context::ConfigurePidControllers(float ki, float kp, float kd, float pidmax, float pidmin) {
    _ki = ki;
    _kp = kp;
    _kd = kd;
    _pidmax = pidmax;
    _pidmin = pidmin;
}

void Context::ConfigureThrottle(PinName throttle_pin, float min, float max) {
    _throttle_pin = throttle_pin;
    _min = min;
    _max = max;
}

void Context::ConfigurePositionSensor(PinName pos_a_pin, PinName pos_b_pin, float cal1_a, float cal2_a, float cal1_b, float cal2_b, float offset) {
    _pos_a_pin = pos_a_pin;
    _pos_b_pin = pos_b_pin;
    _cal1_a = cal1_a;
    _cal2_a = cal2_a;
    _cal1_b = cal1_b;
    _cal2_b = cal2_b;
    _offset = offset;
}

void Context::ConfigureReference(float max_current) {
    _max_current = max_current;
}

void Context::AttachCallBack(void (*f)(Context *), int freq) {
    _call_times[_index] = 0;
    _call_periods[_index] = 1000000 / freq;
    _callbacks[_index++] = f;
}

void Context::Start() {
    InitData();
    
    for (;;) {
        for (int i = 0; i < _index; i++) {
            if (_time - _call_times[i] >= _call_periods[i]) {
                (*_callbacks[i])(this);
                _call_times[i] = _time;
            }
        }
    }
}

void Context::InitData() {
    sense_p = new AnalogHallPositionSensor(_pos_a_pin, _pos_b_pin, _cal1_a, _cal2_a, _cal1_b, _cal2_b, _offset);
    sense_ib = new AnalogCurrentSensor(_ib_pin, _scale);
    sense_ic = new AnalogCurrentSensor(_ic_pin, _scale);
    throttle = new Throttle(_throttle_pin, _min, _max);
    sense_bus = new VoltageSensor();
    sense_t_motor = new TempSensor();
    sense_t_inverter = new TempSensor();
    
    pid_d = new PidController(_ki, _kd, _kp, _pidmax, _pidmin);
    pid_q = new PidController(_ki, _kd, _kp, _pidmax, _pidmin);
    
    motor = new Motor(sense_ic, sense_ib, sense_p, sense_t_motor);
    inverter = new Inverter(_oa, _ob, _oc, _en, sense_bus, sense_t_inverter);
    user = new User(throttle);
    modulator = new SinusoidalModulator(inverter);
    reference = new SynchronousReferenceSynthesizer(_max_current);
    filter_d = new MeanFilter(_filter_strength);
    filter_q = new MeanFilter(_filter_strength);
    
    serial = new Serial(USBTX, USBRX);
    serial->baud(115200);
}

void Context::upd_function() {
    _time += 50;
}