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

Dependencies:   mbed

Fork of analoghalls6 by N K

Committer:
nki
Date:
Mon Mar 02 11:17:15 2015 +0000
Revision:
4:fdadf4a3577a
Parent:
3:0a2396597e0d
Child:
9:d3b70c15baa9
motor spins;

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);
bwang 1:1f58bdcf2956 20 };
bwang 1:1f58bdcf2956 21
bwang 1:1f58bdcf2956 22 class PidController {
bwang 1:1f58bdcf2956 23 public:
bwang 1:1f58bdcf2956 24 PidController(float ki, float kp, float kd, float out_max, float out_min);
bwang 1:1f58bdcf2956 25 float Update(float ref, float in);
bwang 1:1f58bdcf2956 26 private:
bwang 1:1f58bdcf2956 27 float _ki, _kp, _kd;
bwang 1:1f58bdcf2956 28 float _last_in, _integral;
bwang 1:1f58bdcf2956 29 float _out_max, _out_min;
bwang 1:1f58bdcf2956 30 };
bwang 1:1f58bdcf2956 31
bwang 3:0a2396597e0d 32 class ReferenceSynthesizer {
bwang 3:0a2396597e0d 33 public:
bwang 3:0a2396597e0d 34 ReferenceSynthesizer(float max_phase_current) {_max_phase_current = max_phase_current;}
bwang 3:0a2396597e0d 35 virtual void GetReference(float angle, float *ref_d, float *ref_q) {*ref_d = 0; *ref_q = 0;}
bwang 3:0a2396597e0d 36 protected:
bwang 3:0a2396597e0d 37 static float LutSin(float theta);
bwang 3:0a2396597e0d 38 static float LutCos(float theta);
bwang 3:0a2396597e0d 39 protected:
bwang 3:0a2396597e0d 40 float _max_phase_current;
bwang 3:0a2396597e0d 41 };
bwang 3:0a2396597e0d 42
bwang 3:0a2396597e0d 43 class SynchronousReferenceSynthesizer : public ReferenceSynthesizer {
bwang 3:0a2396597e0d 44 public:
bwang 3:0a2396597e0d 45 SynchronousReferenceSynthesizer(float max_phase_current):ReferenceSynthesizer(max_phase_current) {}
bwang 3:0a2396597e0d 46 virtual void GetReference(float angle, float *ref_d, float *ref_q);
bwang 3:0a2396597e0d 47 };
bwang 3:0a2396597e0d 48
bwang 1:1f58bdcf2956 49 class StatusUpdater {
bwang 1:1f58bdcf2956 50 public:
bwang 1:1f58bdcf2956 51 StatusUpdater(Inverter *inverter, Motor *motor, User *user);
bwang 1:1f58bdcf2956 52 void Config(int fast_sample_rate, int slow_sample_rate);
bwang 1:1f58bdcf2956 53 void Start();
bwang 1:1f58bdcf2956 54 private:
bwang 1:1f58bdcf2956 55 static void time_upd_isr();
nki 4:fdadf4a3577a 56 static float LutSin(float theta);
nki 4:fdadf4a3577a 57 static float LutCos(float theta);
bwang 1:1f58bdcf2956 58 private:
bwang 1:1f58bdcf2956 59 Inverter *_inverter;
bwang 1:1f58bdcf2956 60 Motor *_motor;
bwang 1:1f58bdcf2956 61 User *_user;
bwang 1:1f58bdcf2956 62 int _fast_sample_rate;
bwang 1:1f58bdcf2956 63 int _slow_sample_rate;
bwang 1:1f58bdcf2956 64
bwang 1:1f58bdcf2956 65 static unsigned long _time;
bwang 1:1f58bdcf2956 66 Ticker _time_ticker;
bwang 1:1f58bdcf2956 67 };
bwang 1:1f58bdcf2956 68
bwang 1:1f58bdcf2956 69 class LoopDriver {
bwang 1:1f58bdcf2956 70 public:
bwang 3:0a2396597e0d 71 LoopDriver(Inverter *inverter, Motor *motor, User *user, PidController *pid_d, PidController *pid_q,
bwang 3:0a2396597e0d 72 Modulator *modulator, float max_phase_current, int update_frequency);
bwang 1:1f58bdcf2956 73 void Start();
bwang 1:1f58bdcf2956 74 private:
bwang 3:0a2396597e0d 75 static void Clarke(float a, float b, float *alpha, float *beta);
bwang 3:0a2396597e0d 76 static void Parke(float alpha, float beta, float theta, float *d, float *q);
bwang 3:0a2396597e0d 77 static void InverseParke(float d, float q, float theta, float *alpha, float *beta);
bwang 1:1f58bdcf2956 78 private:
bwang 1:1f58bdcf2956 79 static void update();
bwang 1:1f58bdcf2956 80 private:
bwang 3:0a2396597e0d 81 static float LutSin(float theta);
bwang 3:0a2396597e0d 82 static float LutCos(float theta);
bwang 3:0a2396597e0d 83 private:
bwang 3:0a2396597e0d 84 static Inverter *_inverter;
bwang 3:0a2396597e0d 85 static Motor *_motor;
bwang 3:0a2396597e0d 86 static User *_user;
bwang 3:0a2396597e0d 87 static PidController *_pid_d, *_pid_q;
bwang 3:0a2396597e0d 88 static ReferenceSynthesizer *_reference;
bwang 3:0a2396597e0d 89 static Modulator *_modulator;
bwang 1:1f58bdcf2956 90
bwang 3:0a2396597e0d 91 Ticker _upd_ticker;
bwang 3:0a2396597e0d 92 float _max_phase_current;
bwang 1:1f58bdcf2956 93 int _update_frequency;
nki 4:fdadf4a3577a 94 static int _blinky_toggle;
bwang 2:8696a62a4077 95 };
bwang 1:1f58bdcf2956 96
bwang 1:1f58bdcf2956 97 #endif
bwang 1:1f58bdcf2956 98