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 #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 
62 };
63 
71 };
72 
79 
82  float low_feedback;
83  float high_feedback;
84 
85  float timeout;
86 };
87 
90 
95 
96  float last_feedback;
100 };
101 
102 // Local variables and functions
103 static struct pfc_actuator_t pfc_acts[] = PFC_ACTUATORS;
104 static struct pfc_actuators_t pfc_actuators;
105 static struct preflight_check_t actuators_pfc;
107 static void pfc_actuators_cb(struct preflight_result_t *result);
108 static void pfc_act_feedback_cb(uint8_t sender_id, struct act_feedback_t *feedback, uint8_t num_act);
109 
115 static void pfc_actuators_error(const char *fmt, ...) {
116  char msg[200];
117 
118  // Add the error
119  va_list args;
120  va_start(args, fmt);
121  int rc = vsnprintf(msg, 200, fmt, args);
122  va_end(args);
123 
124  if(rc > 0) {
125  DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, rc, msg);
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 
141 void pfc_actuators_init(void) {
145  pfc_actuators.act_nb = sizeof(pfc_acts) / sizeof(struct pfc_actuator_t);
148 
150  AbiBindMsgACT_FEEDBACK(ABI_BROADCAST, &act_feedback_ev, pfc_act_feedback_cb);
151 }
152 
156 void pfc_actuators_run(void) {
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 &&
181  } else if(act->feedback_id2 != 255 &&
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 &&
201  } else if(act->feedback_id2 != 255 &&
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 
227 void pfc_actuators_start(bool start) {
235  }
236  else if(!start && pfc_actuators.state == PFC_ACTUATORS_STATE_RUNNING) {
241  }
242 }
243 
251  return value;
252  }
253 
256  }
259  }
260 
261  return value;
262 }
263 
269 static 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 
290 static void pfc_act_feedback_cb(uint8_t sender_id __attribute__((unused)), struct act_feedback_t *feedback, uint8_t num_act) {
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) {
301  pfc_actuators.last_feedback = feedback[i].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) {
309  pfc_actuators.last_feedback2 = feedback[i].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)
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:81
uint8_t feedback_id2
The secondary feedback id of the actuator (255 for none)
Definition: pfc_actuators.c:78
enum pfc_actuators_state_t state
The state of the preflight checks.
Definition: pfc_actuators.c:89
enum pfc_actuator_state_t act_state
The state of the actuator (during the test)
Definition: pfc_actuators.c:93
int16_t low
The low value to set the actuator to.
Definition: pfc_actuators.c:80
float high_feedback
The expected feedback value when the actuator is high.
Definition: pfc_actuators.c:83
pfc_actuators_state_t
The status of the preflight checks.
Definition: pfc_actuators.c:57
@ PFC_ACTUATORS_STATE_ERROR
Definition: pfc_actuators.c:61
@ PFC_ACTUATORS_STATE_INIT
Definition: pfc_actuators.c:58
@ PFC_ACTUATORS_STATE_SUCCESS
Definition: pfc_actuators.c:60
@ PFC_ACTUATORS_STATE_RUNNING
Definition: pfc_actuators.c:59
uint8_t feedback_id
The feedback id of the actuator (255 for none)
Definition: pfc_actuators.c:77
#define PFC_ACTUATORS_DEBUG
Enable debugging to set the expected feedback values.
Definition: pfc_actuators.c:51
uint8_t act_nb
The number of actuators.
Definition: pfc_actuators.c:92
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:94
float timeout
The timeout for the actuator to move.
Definition: pfc_actuators.c:85
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:96
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:99
pfc_actuator_state_t
The state of the actuator during the test.
Definition: pfc_actuators.c:67
@ PFC_ACTUATOR_STATE_WAIT
Definition: pfc_actuators.c:68
@ PFC_ACTUATOR_STATE_HIGH
Definition: pfc_actuators.c:70
@ PFC_ACTUATOR_STATE_LOW
Definition: pfc_actuators.c:69
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:82
#define PFC_ACTUATORS_MAX_RPM_ERROR
Maximum error for the RPM of the actuators.
Definition: pfc_actuators.c:44
float last_feedback_err
The last expected feedback error of the actuator (based on RPM/angle)
Definition: pfc_actuators.c:97
static struct pfc_actuators_t pfc_actuators
uint8_t act_idx
The current actuator index.
Definition: pfc_actuators.c:91
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:98
#define PFC_ACTUATORS_MAX_ANGLE_ERROR
Maximum error for the angle of the actuators (rad)
Definition: pfc_actuators.c:37
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:76
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