Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
actuators.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2014-2015 Freek van Tienen <freek.v.tienen@gmail.com>
4  * Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
35 #include "actuators.h"
36 #include "autopilot.h"
37 #include "modules/core/abi.h"
38 #include <endian.h>
39 #include <string.h>
40 
41 #include <stdio.h>
42 
43 
44 #ifndef SERVO_MOTOR_IDX
45 #warning "Disco actuators require a <servo name=MOTOR>"
46 #endif
47 
51 struct __attribute__((__packed__)) disco_bldc_obs {
58  /* bit 0 indicates an overcurrent on the RC receiver port when high
59  * bits #1-#6 indicate an overcurrent on the #1-#6 PPM servos
60  */
64 
74 static uint8_t disco_channels[] = { 4, 5, 6, 1, 2, 3 };
75 
76 #define DISCO_BLDC_STATUS_STOPPED 1
77 #define DISCO_BLDC_STATUS_RAMPUP 2
78 #define DISCO_BLDC_STATUS_RUNNING 4
79 #define DISCO_BLDC_STATUS_RAMPDOWN 5
80 
81 // motor start threshold in RPM
82 // RPM range on disco is [1000, 12500]
83 // start and 1100
84 #define DISCO_BLDC_START_MOTOR_THRESHOLD 1100
85 
87 static uint8_t actuators_disco_checksum(uint8_t *bytes, uint8_t size);
88 
90 {
91  /* Initialize the I2C connection */
95  int i = 0;
96  for (i = 0; i < ACTUATORS_DISCO_PWM_NB; i++) {
97  pwm_sysfs_init(&actuators_disco.pwm[i], "/sys/class/pwm", "export", "run", "duty_ns", "period_ns", disco_channels[i]);
98  }
99 }
100 
102 {
105  } else if (idx > ACTUATORS_DISCO_PWM_NB) {
106  // wrong index, do nothing
107  } else {
108  // val is a PWM value in ms, convert to ns
110  }
111 }
112 
114 {
115  // Handle PWM outputs
116  // already done in set function (FIXME ?)
117 
118  // Handle motor speed controller
119 
120  // Receive the status
123  // copy data from buffer
124 #pragma GCC diagnostic push
125 #pragma GCC diagnostic ignored "-Wcast-qual"
126  memcpy(&obs_data, (uint8_t*)actuators_disco.i2c_trans.buf, sizeof(obs_data));
127 #pragma GCC diagnostic pop
128 
129  // Update status
130  electrical.vsupply = (float)(be16toh(obs_data.batt_mv)) / 1000.f;
131  // extract 'rpm saturation bit'
132  actuators_disco.rpm_saturated = (obs_data.rpm & (1 << 7)) ? true : false;
133  // clear 'rpm saturation bit' and fix endianess
134  obs_data.rpm &= (uint16_t)(~(1 << 7));
135  obs_data.rpm = be16toh(obs_data.rpm);
136  if (obs_data.rpm == 0) {
138  }
139 
140  // When detected a suicide
141  uint8_t bldc_status = obs_data.status & 0x07;
142  if (obs_data.error == 2 && bldc_status != DISCO_BLDC_STATUS_STOPPED) {
143  autopilot_set_kill_throttle(true); //FIXME: make behaviour definable low flying should not stop it
144  }
145 
146  // Start the motors
147  if (bldc_status != DISCO_BLDC_STATUS_RUNNING &&
148  bldc_status != DISCO_BLDC_STATUS_RAMPUP && //FIXME also on rampdown?
150  // Reset the error
153 
154  // Start the motors
157  }
158  // Stop the motors
159  else if ((bldc_status == DISCO_BLDC_STATUS_RUNNING || bldc_status == DISCO_BLDC_STATUS_RAMPUP) &&
163  } else if (bldc_status == DISCO_BLDC_STATUS_RUNNING) {
164  // Send the commands
168  actuators_disco.i2c_trans.buf[3] = 0x00; // enable security
169 #pragma GCC diagnostic push
170 #pragma GCC diagnostic ignored "-Wcast-qual"
172 #pragma GCC diagnostic pop
174  }
175 
176  // Send ABI message
177  struct act_feedback_t feedback = {0};
178  feedback.idx = SERVO_MOTOR_IDX;
179  feedback.rpm = actuators_disco.rpm_obs;
180  feedback.set.rpm = true;
181  AbiSendMsgACT_FEEDBACK(ACT_FEEDBACK_BOARD_ID, &feedback, 1);
182 }
183 
185 {
186  uint8_t checksum = 0;
187  for (int i = 0; i < size; i++) {
188  checksum = checksum ^ bytes[i];
189  }
190 
191  return checksum;
192 }
Main include for ABI (AirBorneInterface).
#define ACT_FEEDBACK_BOARD_ID
static uint8_t checksum
Definition: airspeed_uADC.c:61
void autopilot_set_kill_throttle(bool kill)
set kill throttle
Definition: autopilot.c:302
Core autopilot interface common to all firmwares.
void actuators_disco_commit(void)
Definition: actuators.c:113
uint8_t status
Definition: actuators.c:54
uint8_t checksum
Definition: actuators.c:62
uint8_t motors_err
Definition: actuators.c:56
#define DISCO_BLDC_STATUS_STOPPED
Definition: actuators.c:76
#define DISCO_BLDC_STATUS_RAMPUP
Definition: actuators.c:77
#define DISCO_BLDC_START_MOTOR_THRESHOLD
Definition: actuators.c:84
uint16_t batt_mv
Definition: actuators.c:53
#define DISCO_BLDC_STATUS_RUNNING
Definition: actuators.c:78
struct ActuatorsDisco actuators_disco
Definition: actuators.c:86
struct disco_bldc_obs obs_data
uint8_t overrcurrent
Definition: actuators.c:61
uint8_t temp
Definition: actuators.c:57
uint8_t error
Definition: actuators.c:55
static uint8_t disco_channels[]
Internal mapping of the PWM with output index servo rail 1 <-> linux pwm_4 servo rail 2 <-> linux pwm...
Definition: actuators.c:74
static uint8_t actuators_disco_checksum(uint8_t *bytes, uint8_t size)
Definition: actuators.c:184
void actuators_disco_set(uint8_t idx, uint16_t val)
Definition: actuators.c:101
uint16_t rpm
Definition: actuators.c:52
void actuators_disco_init(void)
Definition: actuators.c:89
private observation structure
Definition: actuators.c:51
#define ACTUATORS_DISCO_PWM_NB
Max number of PWM channels.
Definition: actuators.h:51
#define ACTUATORS_DISCO_CLEAR_ERROR
Clear all current erros.
Definition: actuators.h:46
bool rpm_saturated
RPM saturation flag (bit 15 in obs data)
Definition: actuators.h:60
#define ACTUATORS_DISCO_SET_REF_SPEED
Set reference speed.
Definition: actuators.h:41
#define ACTUATORS_DISCO_MOTOR_IDX
Index for motor BLDC.
Definition: actuators.h:52
#define ACTUATORS_DISCO_START_PROP
Start the propellers.
Definition: actuators.h:43
uint16_t rpm_obs
Measured RPM.
Definition: actuators.h:57
uint16_t motor_rpm
Motor RPM setpoint.
Definition: actuators.h:56
#define ACTUATORS_DISCO_GET_OBS_DATA
Get observation data.
Definition: actuators.h:42
#define ACTUATORS_DISCO_ADDR
Definition: actuators.h:38
struct i2c_transaction i2c_trans
I2C transaction for communicating with the Disco BLDC driver.
Definition: actuators.h:55
struct PWM_Sysfs pwm[ACTUATORS_DISCO_PWM_NB]
Array of PWM outputs.
Definition: actuators.h:58
#define ACTUATORS_DISCO_STOP_PROP
Stop the propellers.
Definition: actuators.h:45
struct Electrical electrical
Definition: electrical.c:92
Interface for electrical status: supply voltage, current, battery status, etc.
float vsupply
supply voltage in V
Definition: electrical.h:45
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction and wait for it to complete.
Definition: i2c.c:403
bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction and wait for it to complete.
Definition: i2c.c:359
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
#define true
Definition: microrl.h:6
Hardware independent API for actuators (servos, motor controllers).
int32_t rpm
RPM.
Definition: actuators.h:51
struct act_feedback_t::act_feedback_set_t set
Bitset registering what is set as feedback.
uint8_t idx
General index of the actuators (generated in airframe.h)
Definition: actuators.h:45
static uint32_t idx
void pwm_sysfs_set_duty(struct PWM_Sysfs *pwm, uint32_t duty)
Definition: pwm_sysfs.c:101
int pwm_sysfs_init(struct PWM_Sysfs *pwm, char *base_path, char *_export, char *_enable, char *_duty, char *_period, uint8_t channel)
Definition: pwm_sysfs.c:52
uint16_t val[TCOUPLE_NB]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98