-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrent_control.cpp
70 lines (59 loc) · 1.84 KB
/
current_control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Performs current control for each magnet with "rx" as set points.
// Current control is high frequency inner loop to the collision control thread.
#include "pid.h"
#include "utils.h"
#include "rodos.h"
#include "magnet.h"
#include "topics.h"
#include "current_control.h"
#include "satellite_config.h"
static int last_sign[4] = {1,1,1,1};
static pid ctrl[4];
static CommBuffer<data_desired_current> cb_desired_current;
static Subscriber subs_desired_current(topic_desired_current, cb_desired_current);
static data_desired_current rx;
static data_current_ctrl tx;
static double time = 0;
void current_control_thread::init()
{
magnet::init();
for(uint8_t i = 0; i < 4; i++)
{
ctrl[i].set_kp(PID_CURRENT_KP);
ctrl[i].set_ki(PID_CURRENT_KI);
ctrl[i].set_control_limits(PID_CURRENT_UMIN, PID_CURRENT_UMAX);
}
}
void current_control_thread::run(void)
{
TIME_LOOP(THREAD_START_CURRENT_CTRL_MILLIS * MILLISECONDS, period * MILLISECONDS)
{
time = NOW();
tx.i[0] = tx.i[1] = tx.i[2] = tx.i[3] = 0.0;
if(stop_control)
{
for(uint8_t i = 0; i < 4; i++)
{
ctrl[i].reset_memory();
magnet::stop(MAGNET_IDX_ALL);
}
}
else
{
magnet::get_current(tx.i); // Feedback measurements
cb_desired_current.getOnlyIfNewData(rx); // Set points
// Perform current control for each magnet
for(uint8_t i = 0; i < 4; i++)
{
tx.i[i] = last_sign[i] * tx.i[i]; // Signed current
float error = rx.i[i] - tx.i[i];
float pwm = ctrl[i].update(error, period / 1000.0);
magnet::actuate((magnet_idx)i, pwm);
last_sign[i] = sign(pwm); // Store sign
}
}
tx.dt = (NOW() - time) / MILLISECONDS;
topic_current_ctrl.publish(tx);
}
}
current_control_thread tamariw_current_control_thread("current_control_thread", THREAD_PRIO_CURRENT_CTRL);