forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio.cpp
157 lines (140 loc) · 4.95 KB
/
radio.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Rover.h"
/*
allow for runtime change of control channel ordering
*/
void Rover::set_control_channels(void)
{
// check change on RCMAP
channel_steer = rc().channel(rcmap.roll()-1);
channel_throttle = rc().channel(rcmap.throttle()-1);
channel_lateral = rc().channel(rcmap.yaw()-1);
// set rc channel ranges
channel_steer->set_angle(SERVO_MAX);
channel_throttle->set_angle(100);
if (channel_lateral != nullptr) {
channel_lateral->set_angle(100);
}
// walking robots rc input init
channel_roll = rc().find_channel_for_option(RC_Channel::AUX_FUNC::ROLL);
channel_pitch = rc().find_channel_for_option(RC_Channel::AUX_FUNC::PITCH);
channel_walking_height = rc().find_channel_for_option(RC_Channel::AUX_FUNC::WALKING_HEIGHT);
if (channel_roll != nullptr) {
channel_roll->set_angle(SERVO_MAX);
channel_roll->set_default_dead_zone(30);
}
if (channel_pitch != nullptr) {
channel_pitch->set_angle(SERVO_MAX);
channel_pitch->set_default_dead_zone(30);
}
if (channel_walking_height != nullptr) {
channel_walking_height->set_angle(SERVO_MAX);
channel_walking_height->set_default_dead_zone(30);
}
// sailboat rc input init
g2.sailboat.init_rc_in();
// Allow to reconfigure output when not armed
if (!arming.is_armed()) {
g2.motors.setup_servo_output();
// For a rover safety is TRIM throttle
g2.motors.setup_safety_output();
}
// setup correct scaling for ESCs like the UAVCAN ESCs which
// take a proportion of speed. Default to 1000 to 2000 for systems without
// a k_throttle output
hal.rcout->set_esc_scaling(1000, 2000);
g2.servo_channels.set_esc_scaling_for(SRV_Channel::k_throttle);
}
void Rover::init_rc_in()
{
// set rc dead zones
channel_steer->set_default_dead_zone(30);
channel_throttle->set_default_dead_zone(30);
if (channel_lateral != nullptr) {
channel_lateral->set_default_dead_zone(30);
}
}
/*
check for driver input on rudder/steering stick for arming/disarming
*/
void Rover::rudder_arm_disarm_check()
{
// check if arming/disarm using rudder is allowed
const AP_Arming::RudderArming arming_rudder = arming.get_rudder_arming_type();
if (arming_rudder == AP_Arming::RudderArming::IS_DISABLED) {
return;
}
// In Rover we need to check that its set to the throttle trim and within the DZ
// if throttle is not within trim dz, then pilot cannot rudder arm/disarm
if (!channel_throttle->in_trim_dz()) {
rudder_arm_timer = 0;
return;
}
// check if arming/disarming allowed from this mode
if (!control_mode->allows_arming_from_transmitter()) {
rudder_arm_timer = 0;
return;
}
if (!arming.is_armed()) {
// when not armed, full right rudder starts arming counter
if (channel_steer->get_control_in() > 4000) {
const uint32_t now = millis();
if (rudder_arm_timer == 0 ||
now - rudder_arm_timer < ARM_DELAY_MS) {
if (rudder_arm_timer == 0) {
rudder_arm_timer = now;
}
} else {
// time to arm!
arming.arm(AP_Arming::Method::RUDDER);
rudder_arm_timer = 0;
}
} else {
// not at full right rudder
rudder_arm_timer = 0;
}
} else if ((arming_rudder == AP_Arming::RudderArming::ARMDISARM) && !g2.motors.active()) {
// when armed and motor not active (not moving), full left rudder starts disarming counter
if (channel_steer->get_control_in() < -4000) {
const uint32_t now = millis();
if (rudder_arm_timer == 0 ||
now - rudder_arm_timer < ARM_DELAY_MS) {
if (rudder_arm_timer == 0) {
rudder_arm_timer = now;
}
} else {
// time to disarm!
arming.disarm(AP_Arming::Method::RUDDER);
rudder_arm_timer = 0;
}
} else {
// not at full left rudder
rudder_arm_timer = 0;
}
}
}
void Rover::read_radio()
{
if (!rc().read_input()) {
// check if we lost RC link
radio_failsafe_check(channel_throttle->get_radio_in());
return;
}
failsafe.last_valid_rc_ms = AP_HAL::millis();
// check that RC value are valid
radio_failsafe_check(channel_throttle->get_radio_in());
// check if we try to do RC arm/disarm
rudder_arm_disarm_check();
}
void Rover::radio_failsafe_check(uint16_t pwm)
{
if (!g.fs_throttle_enabled) {
// radio failsafe disabled
return;
}
bool failed = pwm < static_cast<uint16_t>(g.fs_throttle_value);
if (AP_HAL::millis() - failsafe.last_valid_rc_ms > 500) {
failed = true;
}
AP_Notify::flags.failsafe_radio = failed;
failsafe_trigger(FAILSAFE_EVENT_THROTTLE, "Radio", failed);
}