working-est copy with class-based code. still open loop

Dependencies:   mbed

Fork of analoghalls6 by N K

Committer:
nki
Date:
Sun Mar 08 00:45:28 2015 +0000
Revision:
10:b4abecccec7a
Parent:
9:d3b70c15baa9
uguu;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang 1:1f58bdcf2956 1 #ifndef __META_H
bwang 1:1f58bdcf2956 2 #define __META_H
bwang 1:1f58bdcf2956 3
bwang 1:1f58bdcf2956 4 #include "includes.h"
bwang 1:1f58bdcf2956 5 #include "core.h"
bwang 1:1f58bdcf2956 6 #include "sensors.h"
bwang 1:1f58bdcf2956 7
bwang 1:1f58bdcf2956 8 class Modulator {
bwang 1:1f58bdcf2956 9 public:
bwang 1:1f58bdcf2956 10 Modulator(Inverter *inverter) {_inverter = inverter;}
bwang 1:1f58bdcf2956 11 virtual void Update(float va, float vb) = 0;
bwang 1:1f58bdcf2956 12 protected:
bwang 1:1f58bdcf2956 13 Inverter* _inverter;
bwang 1:1f58bdcf2956 14 };
bwang 1:1f58bdcf2956 15
bwang 1:1f58bdcf2956 16 class SinusoidalModulator: public Modulator {
bwang 1:1f58bdcf2956 17 public:
bwang 1:1f58bdcf2956 18 SinusoidalModulator(Inverter *inverter):Modulator(inverter) {}
bwang 1:1f58bdcf2956 19 virtual void Update(float va, float vb);
nki 9:d3b70c15baa9 20 private:
nki 9:d3b70c15baa9 21 static float LutSin(float theta);
nki 9:d3b70c15baa9 22 static float LutCos(float theta);
bwang 1:1f58bdcf2956 23 };
bwang 1:1f58bdcf2956 24
bwang 1:1f58bdcf2956 25 class PidController {
bwang 1:1f58bdcf2956 26 public:
bwang 1:1f58bdcf2956 27 PidController(float ki, float kp, float kd, float out_max, float out_min);
bwang 1:1f58bdcf2956 28 float Update(float ref, float in);
bwang 1:1f58bdcf2956 29 private:
bwang 1:1f58bdcf2956 30 float _ki, _kp, _kd;
bwang 1:1f58bdcf2956 31 float _last_in, _integral;
bwang 1:1f58bdcf2956 32 float _out_max, _out_min;
bwang 1:1f58bdcf2956 33 };
bwang 1:1f58bdcf2956 34
bwang 3:0a2396597e0d 35 class ReferenceSynthesizer {
bwang 3:0a2396597e0d 36 public:
bwang 3:0a2396597e0d 37 ReferenceSynthesizer(float max_phase_current) {_max_phase_current = max_phase_current;}
nki 10:b4abecccec7a 38 virtual void GetReference(float angle, float throttle, float *ref_d, float *ref_q) {*ref_d = 0; *ref_q = 0;}
bwang 3:0a2396597e0d 39 protected:
bwang 3:0a2396597e0d 40 static float LutSin(float theta);
bwang 3:0a2396597e0d 41 static float LutCos(float theta);
bwang 3:0a2396597e0d 42 protected:
bwang 3:0a2396597e0d 43 float _max_phase_current;
bwang 3:0a2396597e0d 44 };
bwang 3:0a2396597e0d 45
bwang 3:0a2396597e0d 46 class SynchronousReferenceSynthesizer : public ReferenceSynthesizer {
bwang 3:0a2396597e0d 47 public:
bwang 3:0a2396597e0d 48 SynchronousReferenceSynthesizer(float max_phase_current):ReferenceSynthesizer(max_phase_current) {}
nki 10:b4abecccec7a 49 virtual void GetReference(float angle, float throttle, float *ref_d, float *ref_q);
bwang 3:0a2396597e0d 50 };
bwang 3:0a2396597e0d 51
bwang 1:1f58bdcf2956 52 class StatusUpdater {
bwang 1:1f58bdcf2956 53 public:
bwang 1:1f58bdcf2956 54 StatusUpdater(Inverter *inverter, Motor *motor, User *user);
nki 9:d3b70c15baa9 55 void Config(int fast_sample_rate, int med_sample_rate, int slow_sample_rate);
bwang 1:1f58bdcf2956 56 void Start();
bwang 1:1f58bdcf2956 57 private:
bwang 1:1f58bdcf2956 58 static void time_upd_isr();
nki 4:fdadf4a3577a 59 static float LutSin(float theta);
nki 4:fdadf4a3577a 60 static float LutCos(float theta);
bwang 1:1f58bdcf2956 61 private:
bwang 1:1f58bdcf2956 62 Inverter *_inverter;
bwang 1:1f58bdcf2956 63 Motor *_motor;
bwang 1:1f58bdcf2956 64 User *_user;
bwang 1:1f58bdcf2956 65 int _fast_sample_rate;
nki 9:d3b70c15baa9 66 int _med_sample_rate;
bwang 1:1f58bdcf2956 67 int _slow_sample_rate;
bwang 1:1f58bdcf2956 68
bwang 1:1f58bdcf2956 69 static unsigned long _time;
bwang 1:1f58bdcf2956 70 Ticker _time_ticker;
bwang 1:1f58bdcf2956 71 };
bwang 1:1f58bdcf2956 72
bwang 1:1f58bdcf2956 73 class LoopDriver {
bwang 1:1f58bdcf2956 74 public:
bwang 3:0a2396597e0d 75 LoopDriver(Inverter *inverter, Motor *motor, User *user, PidController *pid_d, PidController *pid_q,
bwang 3:0a2396597e0d 76 Modulator *modulator, float max_phase_current, int update_frequency);
bwang 1:1f58bdcf2956 77 void Start();
bwang 1:1f58bdcf2956 78 private:
bwang 3:0a2396597e0d 79 static void Clarke(float a, float b, float *alpha, float *beta);
bwang 3:0a2396597e0d 80 static void Parke(float alpha, float beta, float theta, float *d, float *q);
bwang 3:0a2396597e0d 81 static void InverseParke(float d, float q, float theta, float *alpha, float *beta);
bwang 1:1f58bdcf2956 82 private:
bwang 1:1f58bdcf2956 83 static void update();
bwang 1:1f58bdcf2956 84 private:
bwang 3:0a2396597e0d 85 static float LutSin(float theta);
bwang 3:0a2396597e0d 86 static float LutCos(float theta);
bwang 3:0a2396597e0d 87 private:
bwang 3:0a2396597e0d 88 static Inverter *_inverter;
bwang 3:0a2396597e0d 89 static Motor *_motor;
bwang 3:0a2396597e0d 90 static User *_user;
bwang 3:0a2396597e0d 91 static PidController *_pid_d, *_pid_q;
bwang 3:0a2396597e0d 92 static ReferenceSynthesizer *_reference;
bwang 3:0a2396597e0d 93 static Modulator *_modulator;
bwang 1:1f58bdcf2956 94
bwang 3:0a2396597e0d 95 Ticker _upd_ticker;
bwang 3:0a2396597e0d 96 float _max_phase_current;
bwang 1:1f58bdcf2956 97 int _update_frequency;
nki 4:fdadf4a3577a 98 static int _blinky_toggle;
bwang 2:8696a62a4077 99 };
bwang 1:1f58bdcf2956 100
bwang 1:1f58bdcf2956 101 #endif
bwang 1:1f58bdcf2956 102