Bayley Wang
/
priustroller_3
temp repo
Fork of priustroller_2 by
Diff: context.cpp
- Revision:
- 11:dccbaa9274c5
- Child:
- 13:79e247e54d78
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/context.cpp Sun Mar 08 08:37:38 2015 +0000 @@ -0,0 +1,101 @@ +#include "includes.h" +#include "core.h" +#include "meta.h" +#include "sensors.h" +#include "filters.h" +#include "context.h" + +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_b; + _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, _cal1_b, _cal2_a, _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; +} \ No newline at end of file