-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
wing_rotation_adc_sensor.c
80 lines (67 loc) · 2.53 KB
/
wing_rotation_adc_sensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (C) 2022 Dennis van Wijngaarden <D.C.vanWijngaarden@tudelft.nl>
*
* This file is part of paparazzi
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/** @file "modules/rot_wing_drone/wing_rotation_adc_sensor.c"
* @author Dennis van Wijngaarden <D.C.vanWijngaarden@tudelft.nl>
* Module to read skew angle from adc sensor
*/
#include "modules/rot_wing_drone/wing_rotation_adc_sensor.h"
#include "generated/airframe.h"
#include "modules/core/abi.h"
#include <stdlib.h>
#include "mcu_periph/adc.h"
/*** ADC channel connected to the wing rotation potentiometer */
#ifndef ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION
#define ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION ADC_5
#endif
#ifndef ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION_NB_SAMPLES
#define ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION_NB_SAMPLES 16
#endif
#ifndef ADC_WING_ROT_OFFSET
#error "ADC_WING_ROT_OFFSET not defined"
#endif
#ifndef ADC_WING_ROT_SCALE
#error "ADC_WING_ROT_SCALE not defined"
#endif
#ifdef ADC_WING_ROT_OFFSET
static float adc_offset = ADC_WING_ROT_OFFSET;
#endif
#ifdef ADC_WING_ROT_SCALE
static float adc_scale = ADC_WING_ROT_SCALE;
#endif
static struct adc_buf buf_wing_rot_pos;
// Initialization
void wing_rotation_adc_init(void)
{
// ADC init
adc_buf_channel(ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION, &buf_wing_rot_pos,
ADC_CHANNEL_WING_ROTATION_CONTROLLER_POSITION_NB_SAMPLES);
}
void wing_rotation_adc_to_deg(void)
{
float adc_wing_rotation = buf_wing_rot_pos.sum / buf_wing_rot_pos.av_nb_sample;
float wing_angle_deg = adc_scale * adc_wing_rotation + adc_offset;
// SEND ABI Message to ctr_eff_sched and other modules that want Actuator position feedback
struct act_feedback_t feedback = {0};
feedback.idx = SERVO_ROTATION_MECH_IDX;
feedback.position = 0.5 * M_PI - RadOfDeg(wing_angle_deg);
feedback.set.position = true;
// Send ABI message
AbiSendMsgACT_FEEDBACK(ACT_FEEDBACK_UAVCAN_ID, &feedback, 1);
}