CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Rover/radio.cpp
Views: 1798
1
#include "Rover.h"
2
3
/*
4
allow for runtime change of control channel ordering
5
*/
6
void Rover::set_control_channels(void)
7
{
8
// check change on RCMAP
9
// the library gaurantees that these are non-nullptr:
10
channel_steer = &rc().get_roll_channel();
11
channel_throttle = &rc().get_throttle_channel();
12
channel_lateral = &rc().get_yaw_channel();
13
14
// set rc channel ranges
15
channel_steer->set_angle(SERVO_MAX);
16
channel_throttle->set_angle(100);
17
if (channel_lateral != nullptr) {
18
channel_lateral->set_angle(100);
19
}
20
21
// walking robots rc input init
22
channel_roll = rc().find_channel_for_option(RC_Channel::AUX_FUNC::ROLL);
23
channel_pitch = rc().find_channel_for_option(RC_Channel::AUX_FUNC::PITCH);
24
channel_walking_height = rc().find_channel_for_option(RC_Channel::AUX_FUNC::WALKING_HEIGHT);
25
if (channel_roll != nullptr) {
26
channel_roll->set_angle(SERVO_MAX);
27
channel_roll->set_default_dead_zone(30);
28
}
29
if (channel_pitch != nullptr) {
30
channel_pitch->set_angle(SERVO_MAX);
31
channel_pitch->set_default_dead_zone(30);
32
}
33
if (channel_walking_height != nullptr) {
34
channel_walking_height->set_angle(SERVO_MAX);
35
channel_walking_height->set_default_dead_zone(30);
36
}
37
38
// sailboat rc input init
39
g2.sailboat.init_rc_in();
40
41
// Allow to reconfigure output when not armed
42
if (!arming.is_armed()) {
43
g2.motors.setup_servo_output();
44
// For a rover safety is TRIM throttle
45
g2.motors.setup_safety_output();
46
}
47
// setup correct scaling for ESCs like the UAVCAN ESCs which
48
// take a proportion of speed. Default to 1000 to 2000 for systems without
49
// a k_throttle output
50
hal.rcout->set_esc_scaling(1000, 2000);
51
g2.servo_channels.set_esc_scaling_for(SRV_Channel::k_throttle);
52
}
53
54
void Rover::init_rc_in()
55
{
56
// set rc dead zones
57
channel_steer->set_default_dead_zone(30);
58
channel_throttle->set_default_dead_zone(30);
59
if (channel_lateral != nullptr) {
60
channel_lateral->set_default_dead_zone(30);
61
}
62
}
63
64
/*
65
check for driver input on rudder/steering stick for arming/disarming
66
*/
67
void Rover::rudder_arm_disarm_check()
68
{
69
// check if arming/disarm using rudder is allowed
70
const AP_Arming::RudderArming arming_rudder = arming.get_rudder_arming_type();
71
if (arming_rudder == AP_Arming::RudderArming::IS_DISABLED) {
72
return;
73
}
74
75
// In Rover we need to check that its set to the throttle trim and within the DZ
76
// if throttle is not within trim dz, then pilot cannot rudder arm/disarm
77
if (!channel_throttle->in_trim_dz()) {
78
rudder_arm_timer = 0;
79
return;
80
}
81
82
// check if arming/disarming allowed from this mode
83
if (!control_mode->allows_arming_from_transmitter()) {
84
rudder_arm_timer = 0;
85
return;
86
}
87
88
if (!arming.is_armed()) {
89
// when not armed, full right rudder starts arming counter
90
if (channel_steer->get_control_in() > 4000) {
91
const uint32_t now = millis();
92
93
if (rudder_arm_timer == 0 ||
94
now - rudder_arm_timer < ARM_DELAY_MS) {
95
if (rudder_arm_timer == 0) {
96
rudder_arm_timer = now;
97
}
98
} else {
99
// time to arm!
100
arming.arm(AP_Arming::Method::RUDDER);
101
rudder_arm_timer = 0;
102
}
103
} else {
104
// not at full right rudder
105
rudder_arm_timer = 0;
106
}
107
} else if ((arming_rudder == AP_Arming::RudderArming::ARMDISARM) && !g2.motors.active()) {
108
// when armed and motor not active (not moving), full left rudder starts disarming counter
109
if (channel_steer->get_control_in() < -4000) {
110
const uint32_t now = millis();
111
112
if (rudder_arm_timer == 0 ||
113
now - rudder_arm_timer < ARM_DELAY_MS) {
114
if (rudder_arm_timer == 0) {
115
rudder_arm_timer = now;
116
}
117
} else {
118
// time to disarm!
119
arming.disarm(AP_Arming::Method::RUDDER);
120
rudder_arm_timer = 0;
121
}
122
} else {
123
// not at full left rudder
124
rudder_arm_timer = 0;
125
}
126
}
127
}
128
129
void Rover::read_radio()
130
{
131
if (!rc().read_input()) {
132
// check if we lost RC link
133
radio_failsafe_check(channel_throttle->get_radio_in());
134
return;
135
}
136
137
failsafe.last_valid_rc_ms = AP_HAL::millis();
138
// check that RC value are valid
139
radio_failsafe_check(channel_throttle->get_radio_in());
140
141
// check if we try to do RC arm/disarm
142
rudder_arm_disarm_check();
143
}
144
145
void Rover::radio_failsafe_check(uint16_t pwm)
146
{
147
if (!g.fs_throttle_enabled) {
148
// radio failsafe disabled
149
return;
150
}
151
152
bool failed = pwm < static_cast<uint16_t>(g.fs_throttle_value);
153
if (AP_HAL::millis() - failsafe.last_valid_rc_ms > 500) {
154
failed = true;
155
}
156
AP_Notify::flags.failsafe_radio = failed;
157
failsafe_trigger(FAILSAFE_EVENT_THROTTLE, "Radio", failed);
158
}
159
160