ACS Firmware Documentation
Attitude Control System Capstone 2018 for db@reSat
Macros | Functions | Variables
acs.c File Reference
#include "acs.h"
Include dependency graph for acs.c:

Go to the source code of this file.

Macros

#define EVENT_COUNT   (int)(sizeof(trap)/sizeof(acs_trap))
 determines the total number of events More...
 
#define TRANS_COUNT   (int)(sizeof(trans)/sizeof(acs_transition))
 TRANS_COUNT define the total number of legal transitions. More...
 

Functions

static void trans_cleanup (ACS *acs)
 cleans up on transition More...
 
static void update_recv (ACS *acs, int recv_byte)
 updates the recv buffer More...
 
static int state_off (ACS *acs)
 returns the off state More...
 
static int state_init (ACS *acs)
 returns the init state More...
 
static int state_rdy (ACS *acs)
 returns the ready state More...
 
static int state_rw (ACS *acs)
 return the reaction wheel state More...
 
static int state_mtqr (ACS *acs)
 returns the magnetorquer state More...
 
static int trap_rw_start (ACS *acs)
 function to start the the reaction wheels More...
 
static int trap_rw_stop (ACS *acs)
 function to stop the reaction wheels More...
 
static int trap_mtqr_start (ACS *acs)
 function to start the magnetorquer More...
 
static int trap_mtqr_stop (ACS *acs)
 function to stop the magnetorquer More...
 
static int trap_mtqr_dc (ACS *acs)
 function to change the PWM duty cycle for the magnetorquer More...
 
static int trap_mtqr_dir (ACS *acs)
 function for function for changing the polarity of the magnetorquer More...
 
static int trap_rw_stretch (ACS *acs)
 Changes how many steps are added in between each LUT step. More...
 
static int trap_rw_control (ACS *acs)
 Closed loop using encoder, or brute forcing with open loop. More...
 
static int trap_rw_skip (ACS *acs)
 Changes how many steps are skipped in the LUT when going to next step. More...
 
static int trap_rw_scale (ACS *acs)
 Changes the scale factor, modifying LUT value by 0-100%. More...
 
static int fsm_trap (ACS *acs)
 control falls here when a state change request is unrecognized More...
 
static acs_event getNextEvent (ACS *acs)
 waits for events on the CAN bus and processes them More...
 
static int acs_statemachine (ACS *acs)
 the ACS state machine More...
 
int acsInit (ACS *acs)
 general ACS initialization More...
 
 THD_WORKING_AREA (wa_acsThread, WA_ACS_THD_SIZE)
 ACS thread and thread working area. More...
 
 THD_FUNCTION (acsThread, acs)
 

Variables

event_listener_t el
 
const acs_trap trap []
 trap function table More...
 
const acs_transition trans []
 state transition table More...
 

Macro Definition Documentation

◆ EVENT_COUNT

#define EVENT_COUNT   (int)(sizeof(trap)/sizeof(acs_trap))

determines the total number of events

◆ TRANS_COUNT

#define TRANS_COUNT   (int)(sizeof(trans)/sizeof(acs_transition))

TRANS_COUNT define the total number of legal transitions.

Function Documentation

◆ acs_statemachine()

static int acs_statemachine ( ACS acs)
static

the ACS state machine

Note
this is a critical section
372  {
373  int i;
374  acs->cur_state = state_init(acs);
375  update_recv(acs,ACS_STATE);
376 
377  while (!chThdShouldTerminateX() && acs->cur_state != ST_OFF) {
378  acs->event = getNextEvent(acs);
379 
380  // *******critical section**********
381  chSysLock();
382  acs->can_buf.send[LAST_EVENT]=acs->event;
383  chSysUnlock();
384  // *******end critical section**********
385 
386  for (i = 0;i < TRANS_COUNT;++i) {
387  if((acs->cur_state == trans[i].state)||(ST_ANY == trans[i].state)){
388  if((acs->event == trans[i].event)||(EV_ANY == trans[i].event)){
389  acs->cur_state = (trans[i].fn)(acs);
390  update_recv(acs,ACS_STATE);
391  }
392  }
393  }
394  chThdSleepMilliseconds(500);
395 /*
396  // this stuff is for debugging
397  chprintf(DEBUG_CHP, "motor stretch: %d\r\n", acs->motor.stretch);
398  chprintf(DEBUG_CHP, "motor openLoop: %d\r\n", acs->motor.openLoop);
399  chprintf(DEBUG_CHP, "motor skip: %d\r\n\n", acs->motor.skip);
400  chprintf(DEBUG_CHP, "motor scale: %d\r\n\n", acs->motor.scale);
401  chprintf(DEBUG_CHP, "motor samples: %d\r\n\n", acs->motor.samples[0]);
402 //*/
403  }
404 
405  return EXIT_SUCCESS;
406 }
int(* fn)(ACS *acs)
Definition: acs.h:168
Definition: acs.h:71
#define EXIT_SUCCESS
Definition: acs.h:24
Definition: acs.h:96
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
Definition: acs.h:70
static int state_init(ACS *acs)
returns the init state
Definition: acs.c:71
acs_state cur_state
Definition: acs.h:149
acs_event event
Definition: acs.h:150
acs_event event
Definition: acs.h:167
#define TRANS_COUNT
TRANS_COUNT define the total number of legal transitions.
Definition: acs.c:322
static void update_recv(ACS *acs, int recv_byte)
updates the recv buffer
Definition: acs.c:42
Definition: acs.h:97
can_buffer can_buf
the most recent event
Definition: acs.h:151
-1
Definition: acs.h:114
static acs_event getNextEvent(ACS *acs)
waits for events on the CAN bus and processes them
Definition: acs.c:330
const acs_transition trans[]
state transition table
Definition: acs.c:310
acs_state state
Definition: acs.h:166

◆ acsInit()

int acsInit ( ACS acs)

general ACS initialization

412  {
413  (void)acs;
414  mtqrInit(&acs->mtqr);
415  return EXIT_SUCCESS;
416 }
void mtqrInit(MTQR *mtqr)
Magnetorquer initialization function.
Definition: magnetorquer.c:9
#define EXIT_SUCCESS
Definition: acs.h:24
MTQR mtqr
Definition: acs.h:153

◆ fsm_trap()

static int fsm_trap ( ACS acs)
static

control falls here when a state change request is unrecognized

  • Note
    this is a critical section
280  {
281  int i,trap_status;
282 
283  for(i = 0;i < EVENT_COUNT;++i){
284  if(acs->event == trap[i].event){
285  if(trap[i].state == ST_ANY || acs->cur_state == trap[i].state){
286  trap_status = (trap[i].fn)(acs);
287  if(trap_status){
288  // *******critical section**********
289  chSysLock();
290  acs->can_buf.send[ERROR_CODE]=77;
291  chSysUnlock();
292  chThdSleepMilliseconds(500);
293  // *******end critical section**********
294  }
295  }
296  }
297  }
298 
299  return acs->cur_state;
300 }
const acs_trap trap[]
trap function table
Definition: acs.c:257
int(* fn)(ACS *acs)
Definition: acs.h:168
Definition: acs.h:69
#define EVENT_COUNT
determines the total number of events
Definition: acs.c:271
Definition: acs.h:96
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
acs_state cur_state
Definition: acs.h:149
acs_event event
Definition: acs.h:150
acs_event event
Definition: acs.h:167
can_buffer can_buf
the most recent event
Definition: acs.h:151
acs_state state
Definition: acs.h:166

◆ getNextEvent()

static acs_event getNextEvent ( ACS acs)
static

waits for events on the CAN bus and processes them

Note
this is a critical section
Todo:
: we should seperate the trap from the state changes in the next version
330  {
331  acs_event event = EV_ANY;
332  chEvtWaitAny(ALL_EVENTS);
333 // ******critical section*******
334  chSysLock();
335  for(int i=0;i<CAN_BUF_SIZE;++i){
336  acs->recv[i]=acs->can_buf.recv[i];
337  acs->can_buf.recv[i]=0;
338  }
339  chSysUnlock();
340 // ******end critical section*******
341 
342  switch(acs->recv[MSG_TYPE]){
343  case NOP:
344  break;
345  case CHG_STATE:
346  event = acs->recv[ARG_BYTE];
347  acs->data = acs->recv[ARG_BYTE+1];
348  break;
349  case CALL_TRAP:
350  // event = EV_STATUS;
353  // state command
354  break;
355  default:
356  break;
357  }
358 
359  if(event < EV_ANY || event >= EV_END){
360  return (acs_event)acs->cur_state;
361  }
362 
363  return event;
364 }
Definition: acs.h:84
Definition: acs.h:85
uint8_t recv[CAN_BUF_SIZE]
Definition: acs.h:140
Definition: acs.h:46
acs_event
enumeration for all the possible events
Definition: acs.h:113
Definition: acs.h:45
Definition: acs.h:83
uint8_t recv[CAN_BUF_SIZE]
Definition: acs.h:157
acs_state cur_state
Definition: acs.h:149
this must be the last event
Definition: acs.h:130
uint8_t data
data was a bandaid
Definition: acs.h:156
#define CAN_BUF_SIZE
Definition: acs.h:19
can_buffer can_buf
the most recent event
Definition: acs.h:151
-1
Definition: acs.h:114

◆ state_init()

static int state_init ( ACS acs)
static

returns the init state

71  {
72  (void)acs;
73  return ST_INIT;
74 }
Definition: acs.h:98

◆ state_mtqr()

static int state_mtqr ( ACS acs)
static

returns the magnetorquer state

99  {
100  trans_cleanup(acs);
101  return ST_MTQR;
102 }
Definition: acs.h:101
static void trans_cleanup(ACS *acs)
cleans up on transition
Definition: acs.c:11

◆ state_off()

static int state_off ( ACS acs)
static

returns the off state

62  {
63  (void)acs;
64  return ST_OFF;
65 }
Definition: acs.h:97

◆ state_rdy()

static int state_rdy ( ACS acs)
static

returns the ready state

80  {
81  trans_cleanup(acs);
82  return ST_RDY;
83 }
Definition: acs.h:99
static void trans_cleanup(ACS *acs)
cleans up on transition
Definition: acs.c:11

◆ state_rw()

static int state_rw ( ACS acs)
static

return the reaction wheel state

89  {
90  trans_cleanup(acs);
91  bldcInit(&acs->motor);
92  return ST_RW;
93 }
Definition: acs.h:100
void bldcInit(bldc *pbldc)
Sets up initial values for the BLDC object.
Definition: bldc.c:177
static void trans_cleanup(ACS *acs)
cleans up on transition
Definition: acs.c:11
bldc motor
Definition: acs.h:152

◆ THD_FUNCTION()

THD_FUNCTION ( acsThread  ,
acs   
)
423  {
424  chRegSetThreadName("acsThread");
425  chEvtRegister(&rpdo_event,&el,0);
426 
428 }
static int acs_statemachine(ACS *acs)
the ACS state machine
Definition: acs.c:372
ACS acs
Definition: main.c:25
event_listener_t el
Definition: acs.c:3

◆ THD_WORKING_AREA()

THD_WORKING_AREA ( wa_acsThread  ,
WA_ACS_THD_SIZE   
)

ACS thread and thread working area.

◆ trans_cleanup()

static void trans_cleanup ( ACS acs)
static

cleans up on transition

Parameters
[in]acspointer to the ACS object ACS caonnot be NULL
11  {
12  switch(acs->cur_state){
13  case ST_OFF:
14 
15  break;
16  case ST_INIT:
17 
18  break;
19  case ST_RDY:
20 
21  break;
22 
23  case ST_RW:
24  bldcExit(&acs->motor);
25  break;
26 
27  case ST_MTQR:
28  mtqrExit(&acs->mtqr);
29  break;
30  default:
31 
32  break;
33  }
34 }
Definition: acs.h:101
Definition: acs.h:100
void mtqrExit(MTQR *mtqr)
Magnetorquer exit function.
Definition: magnetorquer.c:91
Definition: acs.h:98
Definition: acs.h:99
MTQR mtqr
Definition: acs.h:153
bldc motor
Definition: acs.h:152
acs_state cur_state
Definition: acs.h:149
Definition: acs.h:97
void bldcExit(bldc *pbldc)
Function prototype with no return type. Takes bldc type as paramater.
Definition: bldc.c:255

◆ trap_mtqr_dc()

static int trap_mtqr_dc ( ACS acs)
static

function to change the PWM duty cycle for the magnetorquer

Note
this is a critical section
176  {
177  (void)acs;
178  // *******critical section**********
179  chSysLock();
181  chSysUnlock();
182  // *******end critical section**********
183  mtqrSetDC((acs->recv[ARG_BYTE+1]<<8) | acs->recv[ARG_BYTE+2]);
184  return EXIT_SUCCESS;
185 }
Definition: acs.h:72
void mtqrSetDC(uint16_t dc)
Magnetorquer set duty cycle function.
Definition: magnetorquer.c:61
Definition: acs.h:46
#define EXIT_SUCCESS
Definition: acs.h:24
d
Definition: acs.h:128
uint8_t recv[CAN_BUF_SIZE]
Definition: acs.h:157
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
can_buffer can_buf
the most recent event
Definition: acs.h:151

◆ trap_mtqr_dir()

static int trap_mtqr_dir ( ACS acs)
static

function for function for changing the polarity of the magnetorquer

Note
this is a critical section
194  {
195  (void)acs;
196  // *******critical section**********
197  chSysLock();
199  chSysUnlock();
200  // *******end critical section**********
201  mtqrSetDir(acs->recv[ARG_BYTE+1]);
202  return EXIT_SUCCESS;
203 }
Definition: acs.h:72
Definition: acs.h:46
#define EXIT_SUCCESS
Definition: acs.h:24
void mtqrSetDir(uint8_t dc)
Magnetorquer set direction function.
Definition: magnetorquer.c:77
uint8_t recv[CAN_BUF_SIZE]
Definition: acs.h:157
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
c
Definition: acs.h:127
can_buffer can_buf
the most recent event
Definition: acs.h:151

◆ trap_mtqr_start()

static int trap_mtqr_start ( ACS acs)
static

function to start the magnetorquer

Note
this is a critical section
142  {
143  // *******critical section**********
144  chSysLock();
146  chSysUnlock();
147  // *******end critical section**********
148  mtqrStart(&acs->mtqr);
149  return EXIT_SUCCESS;
150 }
Definition: acs.h:72
b
Definition: acs.h:126
#define EXIT_SUCCESS
Definition: acs.h:24
MTQR mtqr
Definition: acs.h:153
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
can_buffer can_buf
the most recent event
Definition: acs.h:151
void mtqrStart(MTQR *mtqr)
Magnetorquer start function.
Definition: magnetorquer.c:25

◆ trap_mtqr_stop()

static int trap_mtqr_stop ( ACS acs)
static

function to stop the magnetorquer

Note
this is a critical section
158  {
159  (void)acs;
160  // *******critical section**********
161  chSysLock();
163  chSysUnlock();
164  // *******end critical section**********
165  mtqrStop(&acs->mtqr);
166  return EXIT_SUCCESS;
167 }
Definition: acs.h:72
#define EXIT_SUCCESS
Definition: acs.h:24
MTQR mtqr
Definition: acs.h:153
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
void mtqrStop(MTQR *mtqr)
Magnetorquer stop function.
Definition: magnetorquer.c:43
c
Definition: acs.h:127
can_buffer can_buf
the most recent event
Definition: acs.h:151

◆ trap_rw_control()

static int trap_rw_control ( ACS acs)
static

Closed loop using encoder, or brute forcing with open loop.

Todo:
: have a better data system.
220  {
221  (void)acs;
223  acs->motor.openLoop = (bool) acs->data;
224  return EXIT_SUCCESS;
225 }
#define EXIT_SUCCESS
Definition: acs.h:24
bldc motor
Definition: acs.h:152
uint8_t data
data was a bandaid
Definition: acs.h:156
bool openLoop
Definition: bldc.h:108

◆ trap_rw_scale()

static int trap_rw_scale ( ACS acs)
static

Changes the scale factor, modifying LUT value by 0-100%.

Todo:
: have a better data system.
242  {
243  (void)acs;
245  acs->motor.scale = acs->data;
246  return EXIT_SUCCESS;
247 }
#define EXIT_SUCCESS
Definition: acs.h:24
uint16_t scale
period counter
Definition: bldc.h:95
bldc motor
Definition: acs.h:152
uint8_t data
data was a bandaid
Definition: acs.h:156

◆ trap_rw_skip()

static int trap_rw_skip ( ACS acs)
static

Changes how many steps are skipped in the LUT when going to next step.

Todo:
: have a better data system.
231  {
232  (void)acs;
234  acs->motor.skip = acs->data;
235  return EXIT_SUCCESS;
236 }
#define EXIT_SUCCESS
Definition: acs.h:24
bldc motor
Definition: acs.h:152
uint8_t data
data was a bandaid
Definition: acs.h:156
uint16_t skip
Definition: bldc.h:95

◆ trap_rw_start()

static int trap_rw_start ( ACS acs)
static

function to start the the reaction wheels

Note
this is a critical section
110  {
111  // *******critical section**********
112  chSysLock();
114  chSysUnlock();
115  // *******end critical section**********
116  bldcStart(&acs->motor);
117  return EXIT_SUCCESS;
118 }
5
Definition: acs.h:120
Definition: acs.h:72
#define EXIT_SUCCESS
Definition: acs.h:24
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
bldc motor
Definition: acs.h:152
void bldcStart(bldc *pbldc)
Enables the three PWM channels, starting to go through the LUT.
Definition: bldc.c:210
can_buffer can_buf
the most recent event
Definition: acs.h:151

◆ trap_rw_stop()

static int trap_rw_stop ( ACS acs)
static

function to stop the reaction wheels

Note
this is a critical section
126  {
127  // *******critical section**********
128  chSysLock();
130  chSysUnlock();
131  // *******end critical section**********
132  bldcStop(&acs->motor);
133  return EXIT_SUCCESS;
134 }
Definition: acs.h:72
void bldcStop(bldc *pbldc)
Stops BLDC control.
Definition: bldc.c:227
#define EXIT_SUCCESS
Definition: acs.h:24
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
bldc motor
Definition: acs.h:152
6
Definition: acs.h:121
can_buffer can_buf
the most recent event
Definition: acs.h:151

◆ trap_rw_stretch()

static int trap_rw_stretch ( ACS acs)
static

Changes how many steps are added in between each LUT step.

Todo:
: have a better data system.
209  {
210  (void)acs;
212  acs->motor.stretch = acs->data;
213  return EXIT_SUCCESS;
214 }
#define EXIT_SUCCESS
Definition: acs.h:24
bldc motor
Definition: acs.h:152
uint8_t data
data was a bandaid
Definition: acs.h:156
uint16_t stretch
number of steps in lut
Definition: bldc.h:95

◆ update_recv()

static void update_recv ( ACS acs,
int  recv_byte 
)
static

updates the recv buffer

Note
this is a critical section
42  {
43  switch(recv_byte){
44  case ERROR_CODE:
45  break;
46  case ACS_STATE:
47 // *******critical section**********
48  chSysLock();
49  acs->can_buf.send[recv_byte]=acs->cur_state;
50  chSysUnlock();
51 // *******end critical section**********
52  break;
53  default:
54  break;
55  }
56 }
Definition: acs.h:69
uint8_t send[CAN_BUF_SIZE]
Definition: acs.h:139
Definition: acs.h:70
acs_state cur_state
Definition: acs.h:149
can_buffer can_buf
the most recent event
Definition: acs.h:151

Variable Documentation

◆ el

event_listener_t el

◆ trans

const acs_transition trans[]
Initial value:
= {
{ST_RDY, EV_OFF, &state_off},
{ST_RW, EV_RDY, &state_rdy},
{ST_MTQR, EV_RDY, &state_rdy},
}
2
Definition: acs.h:117
static int state_off(ACS *acs)
returns the off state
Definition: acs.c:62
Definition: acs.h:101
static int state_rw(ACS *acs)
return the reaction wheel state
Definition: acs.c:89
Definition: acs.h:100
Definition: acs.h:98
Definition: acs.h:99
Definition: acs.h:96
4
Definition: acs.h:119
static int state_mtqr(ACS *acs)
returns the magnetorquer state
Definition: acs.c:99
3
Definition: acs.h:118
static int fsm_trap(ACS *acs)
control falls here when a state change request is unrecognized
Definition: acs.c:280
0
Definition: acs.h:115
static int state_rdy(ACS *acs)
returns the ready state
Definition: acs.c:80
-1
Definition: acs.h:114

state transition table

this state transistion table defines legal transitions and and what function is called upon a successful match in the table.

◆ trap

const acs_trap trap[]
Initial value:
= {
}
5
Definition: acs.h:120
static int trap_mtqr_dir(ACS *acs)
function for function for changing the polarity of the magnetorquer
Definition: acs.c:194
e
Definition: acs.h:129
static int trap_rw_stop(ACS *acs)
function to stop the reaction wheels
Definition: acs.c:126
Definition: acs.h:101
b
Definition: acs.h:126
Definition: acs.h:100
static int trap_mtqr_dc(ACS *acs)
function to change the PWM duty cycle for the magnetorquer
Definition: acs.c:176
static int trap_rw_skip(ACS *acs)
Changes how many steps are skipped in the LUT when going to next step.
Definition: acs.c:231
static int trap_rw_stretch(ACS *acs)
Changes how many steps are added in between each LUT step.
Definition: acs.c:209
d
Definition: acs.h:128
9
Definition: acs.h:124
7
Definition: acs.h:122
static int trap_mtqr_start(ACS *acs)
function to start the magnetorquer
Definition: acs.c:142
static int trap_rw_start(ACS *acs)
function to start the the reaction wheels
Definition: acs.c:110
c
Definition: acs.h:127
8
Definition: acs.h:123
6
Definition: acs.h:121
a
Definition: acs.h:125
static int trap_mtqr_stop(ACS *acs)
function to stop the magnetorquer
Definition: acs.c:158
static int trap_rw_scale(ACS *acs)
Changes the scale factor, modifying LUT value by 0-100%.
Definition: acs.c:242
static int trap_rw_control(ACS *acs)
Closed loop using encoder, or brute forcing with open loop.
Definition: acs.c:220

trap function table

this table defines the events allowed to be called in states, and the function that needs to be called on a successful match