Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
pfc_actuators.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Freek van Tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of paparazzi
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #include "preflight_checks.h"
28 #include "core/abi.h"
30 #include <stdio.h>
31 
35 #ifndef PFC_ACTUATORS_MAX_ANGLE_ERROR
36 #define PFC_ACTUATORS_MAX_ANGLE_ERROR 0.1f
37 #endif
38 
42 #ifndef PFC_ACTUATORS_MAX_RPM_ERROR
43 #define PFC_ACTUATORS_MAX_RPM_ERROR 250.0f
44 #endif
45 
49 #ifndef PFC_ACTUATORS_DEBUG
50 #define PFC_ACTUATORS_DEBUG false
51 #endif
52 
61 };
62 
70 };
71 
78 
81  float low_feedback;
82  float high_feedback;
83 
84  float timeout;
85 };
86 
89 
94 
95  float last_feedback;
99 };
100 
101 // Local variables and functions
102 static struct pfc_actuator_t pfc_acts[] = PFC_ACTUATORS;
103 static struct pfc_actuators_t pfc_actuators;
104 static struct preflight_check_t actuators_pfc;
106 static void pfc_actuators_cb(struct preflight_result_t *result);
107 static void pfc_act_feedback_cb(uint8_t sender_id, struct act_feedback_t *feedback, uint8_t num_act);
108 
114 static void pfc_actuators_error(const char *fmt, ...) {
115  char msg[200];
116 
117  // Add the error
118  va_list args;
119  va_start(args, fmt);
120  int rc = vsnprintf(msg, 200, fmt, args);
121  va_end(args);
122 
123  if(rc > 0) {
124  DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, rc, msg);
125  }
126 }
127 
131 #if PFC_ACTUATORS_DEBUG
132 #define pfc_actuators_debug pfc_actuators_error
133 #else
134 #define pfc_actuators_debug(...)
135 #endif
136 
140 void pfc_actuators_init(void) {
144  pfc_actuators.act_nb = sizeof(pfc_acts) / sizeof(struct pfc_actuator_t);
147 
149  AbiBindMsgACT_FEEDBACK(ABI_BROADCAST, &act_feedback_ev, pfc_act_feedback_cb);
150 }
151 
155 void pfc_actuators_run(void) {
156  // Only actuate when running
158  return;
159  }
160 
161  // Get the actuators
163 
164  // Verify the result and continue
166  switch(pfc_actuators.act_state) {
168  pfc_actuators_debug("Actuator %d starting LOW", pfc_actuators.act_idx);
173  break;
175  // Check if the feedback is correct
176  if(act->feedback_id != 255 &&
180  } else if(act->feedback_id2 != 255 &&
184  }
186  pfc_actuators_debug("Actuator %d LOW ok %.2f, %.2f starting HIGH", pfc_actuators.act_idx, pfc_actuators.last_feedback, pfc_actuators.last_feedback2);
192  }
193  break;
195  // Check if the feedback is correct
196  if(act->feedback_id != 255 &&
200  } else if(act->feedback_id2 != 255 &&
204  }
211  }
212  break;
213  }
214  }
215 
216  // Finished testing
219  pfc_actuators_error("Actuators checks done %d", pfc_actuators.act_idx);
220  }
221 }
222 
226 void pfc_actuators_start(bool start) {
234  }
235  else if(!start && pfc_actuators.state == PFC_ACTUATORS_STATE_RUNNING) {
240  }
241 }
242 
250  return value;
251  }
252 
255  }
258  }
259 
260  return value;
261 }
262 
268 static void pfc_actuators_cb(struct preflight_result_t *result)
269 {
270  switch(pfc_actuators.state) {
272  preflight_error(result, "Actuators not checked perform checks first[%d]", pfc_actuators.act_nb);
273  break;
275  preflight_error(result, "Actuators are currently being checked[%d/%d]",pfc_actuators.act_idx, pfc_actuators.act_nb);
276  break;
278  preflight_success(result, "Actuators checked and moved succesfully[%d]", pfc_actuators.act_nb);
279  break;
281  preflight_error(result, "Actuators not responding correctly[%d/%d]",pfc_actuators.act_idx, pfc_actuators.act_nb);
282  break;
283  }
284 }
285 
289 static void pfc_act_feedback_cb(uint8_t sender_id __attribute__((unused)), struct act_feedback_t *feedback, uint8_t num_act) {
291  return;
292  }
293 
294  // Save the last feedback values
295  for(uint8_t i = 0; i < num_act; i++) {
296  if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id && feedback[i].set.rpm) {
297  pfc_actuators.last_feedback = feedback[i].rpm;
299  } else if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id && feedback[i].set.position) {
300  pfc_actuators.last_feedback = feedback[i].position;
302  }
303 
304  if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id2 && feedback[i].set.rpm) {
305  pfc_actuators.last_feedback2 = feedback[i].rpm;
307  } else if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id2 && feedback[i].set.position) {
308  pfc_actuators.last_feedback2 = feedback[i].position;
310  }
311  }
312 }
Main include for ABI (AirBorneInterface).
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:58
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
int32_t rpm
RPM.
Definition: actuators.h:51
bool position
Position is set.
Definition: actuators.h:48
struct act_feedback_t::act_feedback_set_t set
Bitset registering what is set as feedback.
float position
In radians.
Definition: actuators.h:52
static uint32_t idx
int16_t high
The high value to set the actuator to.
Definition: pfc_actuators.c:80
uint8_t feedback_id2
The secondary feedback id of the actuator (255 for none)
Definition: pfc_actuators.c:77
enum pfc_actuators_state_t state
The state of the preflight checks.
Definition: pfc_actuators.c:88
enum pfc_actuator_state_t act_state
The state of the actuator (during the test)
Definition: pfc_actuators.c:92
int16_t low
The low value to set the actuator to.
Definition: pfc_actuators.c:79
float high_feedback
The expected feedback value when the actuator is high.
Definition: pfc_actuators.c:82
pfc_actuators_state_t
The status of the preflight checks.
Definition: pfc_actuators.c:56
@ PFC_ACTUATORS_STATE_ERROR
Definition: pfc_actuators.c:60
@ PFC_ACTUATORS_STATE_INIT
Definition: pfc_actuators.c:57
@ PFC_ACTUATORS_STATE_SUCCESS
Definition: pfc_actuators.c:59
@ PFC_ACTUATORS_STATE_RUNNING
Definition: pfc_actuators.c:58
uint8_t feedback_id
The feedback id of the actuator (255 for none)
Definition: pfc_actuators.c:76
#define PFC_ACTUATORS_DEBUG
Enable debugging to set the expected feedback values.
Definition: pfc_actuators.c:50
uint8_t act_nb
The number of actuators.
Definition: pfc_actuators.c:91
void pfc_actuators_init(void)
Register the preflight checks for the actuators.
void pfc_actuators_start(bool start)
Start the actuator testing.
static void pfc_act_feedback_cb(uint8_t sender_id, struct act_feedback_t *feedback, uint8_t num_act)
Callback for the actuator feedback.
static struct preflight_check_t actuators_pfc
float act_start_time
The start time of the actuator (during the test)
Definition: pfc_actuators.c:93
float timeout
The timeout for the actuator to move.
Definition: pfc_actuators.c:84
int16_t pfc_actuators_value(uint8_t idx, int16_t value)
Get the actuator value in the command laws to move the actuator during the preflight checks.
float last_feedback
The last measured feedback value of the actuator.
Definition: pfc_actuators.c:95
static struct pfc_actuator_t pfc_acts[]
float last_feedback_err2
The last expected secondary feedback error of the actuator (based on RPM/angle)
Definition: pfc_actuators.c:98
pfc_actuator_state_t
The state of the actuator during the test.
Definition: pfc_actuators.c:66
@ PFC_ACTUATOR_STATE_WAIT
Definition: pfc_actuators.c:67
@ PFC_ACTUATOR_STATE_HIGH
Definition: pfc_actuators.c:69
@ PFC_ACTUATOR_STATE_LOW
Definition: pfc_actuators.c:68
static void pfc_actuators_error(const char *fmt,...)
Send an error message to the ground station.
float low_feedback
The expected feedback value when the actuator is low.
Definition: pfc_actuators.c:81
#define PFC_ACTUATORS_MAX_RPM_ERROR
Maximum error for the RPM of the actuators.
Definition: pfc_actuators.c:43
float last_feedback_err
The last expected feedback error of the actuator (based on RPM/angle)
Definition: pfc_actuators.c:96
static struct pfc_actuators_t pfc_actuators
uint8_t act_idx
The current actuator index.
Definition: pfc_actuators.c:90
static void pfc_actuators_cb(struct preflight_result_t *result)
Check the actuators with feedback.
float last_feedback2
The last measured secondary feedback value of the actuator.
Definition: pfc_actuators.c:97
#define PFC_ACTUATORS_MAX_ANGLE_ERROR
Maximum error for the angle of the actuators (rad)
Definition: pfc_actuators.c:36
void pfc_actuators_run(void)
Move the actuators, should be put in the command laws.
static abi_event act_feedback_ev
#define pfc_actuators_debug(...)
Send a debug message to the ground station.
The configuration struct of the actuator.
Definition: pfc_actuators.c:75
void preflight_error(struct preflight_result_t *result, const char *fmt,...)
Register a preflight error used inside the preflight checking functions.
void preflight_success(struct preflight_result_t *result, const char *fmt,...)
Register a preflight success used inside the preflight checking functions.
void preflight_check_register(struct preflight_check_t *check, preflight_check_f func)
Register a preflight check and add it to the linked list.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
Periodic telemetry system header (includes downlink utility and generated code).
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98