Prius IPM controller

Dependencies:   mbed

Fork of analoghalls5_5 by N K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers context.cpp Source File

context.cpp

00001 #include "includes.h"
00002 #include "core.h"
00003 #include "meta.h"
00004 #include "sensors.h"
00005 #include "filters.h"
00006 #include "context.h"
00007 #include "debug.h"
00008 
00009 float debug_registers[32];
00010 
00011 Context::Context() {
00012     _index = 0;
00013     _time_upd_ticker = new Ticker();
00014     _time_upd_ticker->attach_us(this, &Context::upd_function, 50);
00015 }
00016 
00017 void Context::ConfigureOutputs(PinName oa, PinName ob, PinName oc, PinName en) {
00018     _oa = oa;
00019     _ob = ob;
00020     _oc = oc;
00021     _en = en;
00022 }
00023 
00024 void Context::ConfigureCurrentSensors(PinName ib_pin, PinName ic_pin, float scale, float filter_strength) {
00025     _ib_pin = ib_pin;
00026     _ic_pin = ic_pin;
00027     _scale = scale;
00028     _filter_strength = filter_strength;
00029 }
00030 
00031 void Context::ConfigureIdPidController(float ki, float kp, float kd, float pidmax, float pidmin) {
00032     _dki = ki;
00033     _dkp = kp;
00034     _dkd = kd;
00035     _dpidmax = pidmax;
00036     _dpidmin = pidmin;
00037 }
00038 
00039 
00040 void Context::ConfigureIqPidController(float ki, float kp, float kd, float pidmax, float pidmin) {
00041     _qki = ki;
00042     _qkp = kp;
00043     _qkd = kd;
00044     _qpidmax = pidmax;
00045     _qpidmin = pidmin;
00046 }
00047 
00048 void Context::ConfigureThrottle(PinName throttle_pin, float min, float max) {
00049     _throttle_pin = throttle_pin;
00050     _min = min;
00051     _max = max;
00052 }
00053 
00054 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) {
00055     _pos_a_pin = pos_a_pin;
00056     _pos_b_pin = pos_b_pin;
00057     _cal1_a = cal1_a;
00058     _cal2_a = cal2_a;
00059     _cal1_b = cal1_b;
00060     _cal2_b = cal2_b;
00061     _offset = offset;
00062 }
00063 
00064 void Context::ConfigureReference(float max_current) {
00065     _max_current = max_current;
00066 }
00067 
00068 void Context::AttachCallBack(void (*f)(Context *), int freq) {
00069     _call_times[_index] = 0;
00070     _call_periods[_index] = 1000000 / freq;
00071     _callbacks[_index++] = f;
00072 }
00073 
00074 void Context::Start() {
00075     InitData();
00076     
00077     for (;;) {
00078         for (int i = 0; i < _index; i++) {
00079             if (_time - _call_times[i] >= _call_periods[i]) {
00080                 (*_callbacks[i])(this);
00081                 _call_times[i] = _time;
00082             }
00083         }
00084     }
00085 }
00086 
00087 void Context::ConfigureDebugger(int debugger_channels, int debugger_size) {
00088     _debugger_channels = debugger_channels;
00089     _debugger_size = debugger_size;
00090 }
00091 
00092 void Context::InitData() {
00093     sense_p = new AnalogHallPositionSensor(_pos_a_pin, _pos_b_pin, _cal1_a, _cal2_a, _cal1_b, _cal2_b, _offset);
00094     sense_ib = new AnalogCurrentSensor(_ib_pin, _scale);
00095     sense_ic = new AnalogCurrentSensor(_ic_pin, _scale);
00096     throttle = new Throttle(_throttle_pin, _min, _max);
00097     sense_bus = new VoltageSensor();
00098     sense_t_motor = new TempSensor();
00099     sense_t_inverter = new TempSensor();
00100     
00101     pid_d = new PidController(_dki, _dkp, _dkd, _dpidmax, _dpidmin);
00102     pid_q = new PidController(_qki, _qkp, _qkd, _qpidmax, _qpidmin);
00103     
00104     motor = new Motor(sense_ic, sense_ib, sense_p, sense_t_motor);
00105     inverter = new Inverter(_oa, _ob, _oc, _en, sense_bus, sense_t_inverter);
00106     user = new User(throttle);
00107     modulator = new SvmModulator(inverter);
00108     reference = new SynchronousReferenceSynthesizer(_max_current);
00109     filter_d = new MeanFilter(_filter_strength);
00110     filter_q = new MeanFilter(_filter_strength);
00111     
00112     serial = new Serial(USBTX, USBRX);
00113     serial->baud(115200);
00114     serial->printf("%s\n\r", "Init Serial Communications");
00115     
00116     debugger = new BufferedDebugger(this, _debugger_channels, _debugger_size);
00117 }
00118 
00119 void Context::upd_function() {
00120     _time += 50;
00121 }