forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.cpp
146 lines (126 loc) · 4.65 KB
/
sensors.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
#include "Rover.h"
#include <AP_RangeFinder/RangeFinder_Backend.h>
#include <AP_VisualOdom/AP_VisualOdom.h>
// check for new compass data - 10Hz
void Rover::update_compass(void)
{
if (AP::compass().enabled() && compass.read()) {
ahrs.set_compass(&compass);
}
}
// Save compass offsets
void Rover::compass_save() {
if (AP::compass().enabled() &&
compass.get_learn_type() >= Compass::LEARN_INTERNAL &&
!arming.is_armed()) {
compass.save_offsets();
}
}
// init beacons used for non-gps position estimates
void Rover::init_beacon()
{
g2.beacon.init();
}
// init visual odometry sensor
void Rover::init_visual_odom()
{
g2.visual_odom.init();
}
// update wheel encoders
void Rover::update_wheel_encoder()
{
// exit immediately if not enabled
if (g2.wheel_encoder.num_sensors() == 0) {
return;
}
// update encoders
g2.wheel_encoder.update();
// initialise on first iteration
const uint32_t now = AP_HAL::millis();
if (wheel_encoder_last_ekf_update_ms == 0) {
wheel_encoder_last_ekf_update_ms = now;
for (uint8_t i = 0; i < g2.wheel_encoder.num_sensors(); i++) {
wheel_encoder_last_angle_rad[i] = g2.wheel_encoder.get_delta_angle(i);
wheel_encoder_last_update_ms[i] = g2.wheel_encoder.get_last_reading_ms(i);
}
return;
}
// calculate delta angle and delta time and send to EKF
// use time of last ping from wheel encoder
// send delta time (time between this wheel encoder time and previous wheel encoder time)
// in case where wheel hasn't moved, count = 0 (cap the delta time at 50ms - or system time)
// use system clock of last update instead of time of last ping
const float system_dt = (now - wheel_encoder_last_ekf_update_ms) / 1000.0f;
for (uint8_t i = 0; i < g2.wheel_encoder.num_sensors(); i++) {
// calculate angular change (in radians)
const float curr_angle_rad = g2.wheel_encoder.get_delta_angle(i);
const float delta_angle = curr_angle_rad - wheel_encoder_last_angle_rad[i];
wheel_encoder_last_angle_rad[i] = curr_angle_rad;
// save cumulative distances at current time (in meters)
wheel_encoder_last_distance_m[i] = g2.wheel_encoder.get_distance(i);
// calculate delta time
float delta_time;
const uint32_t latest_sensor_update_ms = g2.wheel_encoder.get_last_reading_ms(i);
const uint32_t sensor_diff_ms = latest_sensor_update_ms - wheel_encoder_last_update_ms[i];
// if we have not received any sensor updates, or time difference is too high then use time since last update to the ekf
// check for old or insane sensor update times
if (sensor_diff_ms == 0 || sensor_diff_ms > 100) {
delta_time = system_dt;
wheel_encoder_last_update_ms[i] = wheel_encoder_last_ekf_update_ms;
} else {
delta_time = sensor_diff_ms / 1000.0f;
wheel_encoder_last_update_ms[i] = latest_sensor_update_ms;
}
/* delAng is the measured change in angular position from the previous measurement where a positive rotation is produced by forward motion of the vehicle (rad)
* delTime is the time interval for the measurement of delAng (sec)
* timeStamp_ms is the time when the rotation was last measured (msec)
* posOffset is the XYZ body frame position of the wheel hub (m)
*/
EKF3.writeWheelOdom(delta_angle, delta_time, wheel_encoder_last_update_ms[i], g2.wheel_encoder.get_pos_offset(i), g2.wheel_encoder.get_wheel_radius(i));
}
// record system time update for next iteration
wheel_encoder_last_ekf_update_ms = now;
}
// Accel calibration
void Rover::accel_cal_update() {
if (hal.util->get_soft_armed()) {
return;
}
ins.acal_update();
// check if new trim values, and set them float trim_roll, trim_pitch;
float trim_roll, trim_pitch;
if (ins.get_new_trim(trim_roll, trim_pitch)) {
ahrs.set_trim(Vector3f(trim_roll, trim_pitch, 0));
}
}
// read the rangefinders
void Rover::read_rangefinders(void)
{
rangefinder.update();
Log_Write_Depth();
}
// initialise proximity sensor
void Rover::init_proximity(void)
{
g2.proximity.init();
g2.proximity.set_rangefinder(&rangefinder);
}
/*
ask airspeed sensor for a new value, duplicated from plane
*/
void Rover::read_airspeed(void)
{
g2.airspeed.update(should_log(MASK_LOG_IMU));
}
/*
update RPM sensors
*/
void Rover::rpm_update(void)
{
rpm_sensor.update();
if (rpm_sensor.enabled(0) || rpm_sensor.enabled(1)) {
if (should_log(MASK_LOG_RC)) {
logger.Write_RPM(rpm_sensor);
}
}
}