Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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#include <math.h>
32
36#ifndef PFC_ACTUATORS_MAX_ANGLE_ERROR
37#define PFC_ACTUATORS_MAX_ANGLE_ERROR 0.05f
38#endif
39
43#ifndef PFC_ACTUATORS_MAX_RPM_ERROR
44#define PFC_ACTUATORS_MAX_RPM_ERROR 250.0f
45#endif
46
50#ifndef PFC_ACTUATORS_DEBUG
51#define PFC_ACTUATORS_DEBUG false
52#endif
53
63
72
87
101
102// Local variables and functions
107static void pfc_actuators_cb(struct preflight_result_t *result);
108static void pfc_act_feedback_cb(uint8_t sender_id, struct act_feedback_t *feedback, uint8_t num_act);
109
115static void pfc_actuators_error(const char *fmt, ...) {
116 char msg[200];
117
118 // Add the error
120 va_start(args, fmt);
121 int rc = vsnprintf(msg, 200, fmt, args);
122 va_end(args);
123
124 if(rc > 0) {
126 }
127}
128
132#if PFC_ACTUATORS_DEBUG
133#define pfc_actuators_debug pfc_actuators_error
134#else
135#define pfc_actuators_debug(...)
136#endif
137
152
157 // Only actuate when running
159 return;
160 }
161
162 // Get the actuators
164
165 // Verify the result and continue
167 switch(pfc_actuators.act_state) {
169 pfc_actuators_debug("Actuator %d starting LOW", pfc_actuators.act_idx);
174 break;
176 // Check if the feedback is correct
177 if(act->feedback_id != 255 &&
179 pfc_actuators_error("Actuator %d not responding correctly LOW %.2f < %.2f < %.2f", pfc_actuators.act_idx, act->low_feedback - pfc_actuators.last_feedback_err, pfc_actuators.last_feedback, act->low_feedback + pfc_actuators.last_feedback_err);
181 } else if(act->feedback_id2 != 255 &&
183 pfc_actuators_error("Actuator %d not responding correctly LOW2 %.2f < %.2f < %.2f", pfc_actuators.act_idx, act->low_feedback - pfc_actuators.last_feedback_err2, pfc_actuators.last_feedback2, act->low_feedback + pfc_actuators.last_feedback_err2);
185 }
187 pfc_actuators_debug("Actuator %d LOW ok %.2f, %.2f starting HIGH", pfc_actuators.act_idx, pfc_actuators.last_feedback, pfc_actuators.last_feedback2);
193 }
194 break;
196 // Check if the feedback is correct
197 if(act->feedback_id != 255 &&
199 pfc_actuators_error("Actuator %d not responding correctly HIGH %.2f < %.2f < %.2f", pfc_actuators.act_idx, act->high_feedback - pfc_actuators.last_feedback_err, pfc_actuators.last_feedback, act->high_feedback + pfc_actuators.last_feedback_err);
201 } else if(act->feedback_id2 != 255 &&
203 pfc_actuators_error("Actuator %d not responding correctly HIGH2 %.2f < %.2f < %.2f", pfc_actuators.act_idx, act->high_feedback - pfc_actuators.last_feedback_err2, pfc_actuators.last_feedback2, act->high_feedback + pfc_actuators.last_feedback_err2);
205 }
212 }
213 break;
214 }
215 }
216
217 // Finished testing
220 pfc_actuators_error("Actuators checks done %d", pfc_actuators.act_idx);
221 }
222}
223
243
251 return value;
252 }
253
256 }
259 }
260
261 return value;
262}
263
269static void pfc_actuators_cb(struct preflight_result_t *result)
270{
271 switch(pfc_actuators.state) {
273 preflight_error(result, "Actuators not checked [act: %d]", pfc_actuators.act_nb);
274 break;
276 preflight_error(result, "Actuators are currently being checked [act: %d/%d]",pfc_actuators.act_idx, pfc_actuators.act_nb);
277 break;
279 preflight_success(result, "Actuators checked and succesfull [act: %d]", pfc_actuators.act_nb);
280 break;
282 preflight_error(result, "Actuators not responding correctly [act: %d/%d]",pfc_actuators.act_idx, pfc_actuators.act_nb);
283 break;
284 }
285}
286
292 return;
293 }
294
295 // Save the last feedback values
296 for(uint8_t i = 0; i < num_act; i++) {
297 if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id && feedback[i].set.rpm) {
298 pfc_actuators.last_feedback = feedback[i].rpm;
300 } else if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id && feedback[i].set.position) {
303 }
304
305 if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id2 && feedback[i].set.rpm) {
306 pfc_actuators.last_feedback2 = feedback[i].rpm;
308 } else if(feedback[i].idx == pfc_acts[pfc_actuators.act_idx].feedback_id2 && feedback[i].set.position) {
311 }
312 }
313}
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)
uint16_t foo
Definition main_demo5.c:58
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.
uint8_t feedback_id2
The secondary feedback id of the actuator (255 for none)
enum pfc_actuators_state_t state
The state of the preflight checks.
enum pfc_actuator_state_t act_state
The state of the actuator (during the test)
int16_t low
The low value to set the actuator to.
float high_feedback
The expected feedback value when the actuator is high.
pfc_actuators_state_t
The status of the preflight checks.
@ PFC_ACTUATORS_STATE_ERROR
@ PFC_ACTUATORS_STATE_INIT
@ PFC_ACTUATORS_STATE_SUCCESS
@ PFC_ACTUATORS_STATE_RUNNING
uint8_t feedback_id
The feedback id of the actuator (255 for none)
#define PFC_ACTUATORS_DEBUG
Enable debugging to set the expected feedback values.
uint8_t act_nb
The number of actuators.
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)
float timeout
The timeout for the actuator to move.
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.
static struct pfc_actuator_t pfc_acts[]
float last_feedback_err2
The last expected secondary feedback error of the actuator (based on RPM/angle)
pfc_actuator_state_t
The state of the actuator during the test.
@ PFC_ACTUATOR_STATE_WAIT
@ PFC_ACTUATOR_STATE_HIGH
@ PFC_ACTUATOR_STATE_LOW
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.
#define PFC_ACTUATORS_MAX_RPM_ERROR
Maximum error for the RPM of the actuators.
float last_feedback_err
The last expected feedback error of the actuator (based on RPM/angle)
static struct pfc_actuators_t pfc_actuators
uint8_t act_idx
The current actuator index.
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.
#define PFC_ACTUATORS_MAX_ANGLE_ERROR
Maximum error for the angle of the actuators (rad)
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.
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.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.