2012-04-30 04:17:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-02-28 21:00:48 -04:00
|
|
|
|
2015-05-13 00:16:45 -03:00
|
|
|
#include "Rover.h"
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::init_barometer(void)
|
2014-02-23 18:25:50 -04:00
|
|
|
{
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO, "Calibrating barometer");
|
2014-02-23 18:25:50 -04:00
|
|
|
barometer.calibrate();
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO, "barometer calibration complete");
|
2014-02-23 18:25:50 -04:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::init_sonar(void)
|
2012-05-14 12:47:08 -03:00
|
|
|
{
|
2014-06-27 00:18:20 -03:00
|
|
|
sonar.init();
|
2012-05-14 12:47:08 -03:00
|
|
|
}
|
|
|
|
|
2013-10-02 03:07:28 -03:00
|
|
|
// read_battery - reads battery voltage and current and invokes failsafe
|
|
|
|
// should be called at 10hz
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::read_battery(void)
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
2013-10-02 03:07:28 -03:00
|
|
|
battery.read();
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
2013-03-14 18:08:35 -03:00
|
|
|
// read the receiver RSSI as an 8 bit number for MAVLink
|
|
|
|
// RC_CHANNELS_SCALED message
|
2015-05-12 04:00:25 -03:00
|
|
|
void Rover::read_receiver_rssi(void)
|
2013-03-14 18:08:35 -03:00
|
|
|
{
|
2015-08-28 03:00:12 -03:00
|
|
|
receiver_rssi = rssi.read_receiver_rssi_uint8();
|
2013-03-14 18:08:35 -03:00
|
|
|
}
|
2013-03-21 18:49:51 -03:00
|
|
|
|
2015-07-30 22:24:32 -03:00
|
|
|
//Calibrate compass
|
|
|
|
void Rover::compass_cal_update() {
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
compass.compass_cal_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 18:49:51 -03:00
|
|
|
// read the sonars
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::read_sonars(void)
|
2013-03-21 18:49:51 -03:00
|
|
|
{
|
2014-06-27 00:18:20 -03:00
|
|
|
sonar.update();
|
|
|
|
|
2015-04-17 03:41:00 -03:00
|
|
|
if (sonar.status() == RangeFinder::RangeFinder_NotConnected) {
|
2013-03-21 18:49:51 -03:00
|
|
|
// this makes it possible to disable sonar at runtime
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-17 03:41:00 -03:00
|
|
|
if (sonar.has_data(1)) {
|
2013-03-21 18:49:51 -03:00
|
|
|
// we have two sonars
|
2014-06-27 00:18:20 -03:00
|
|
|
obstacle.sonar1_distance_cm = sonar.distance_cm(0);
|
|
|
|
obstacle.sonar2_distance_cm = sonar.distance_cm(1);
|
2013-04-19 18:29:57 -03:00
|
|
|
if (obstacle.sonar1_distance_cm <= (uint16_t)g.sonar_trigger_cm &&
|
2015-03-02 01:53:43 -04:00
|
|
|
obstacle.sonar1_distance_cm <= (uint16_t)obstacle.sonar2_distance_cm) {
|
2013-03-21 18:49:51 -03:00
|
|
|
// we have an object on the left
|
2013-03-28 20:49:08 -03:00
|
|
|
if (obstacle.detected_count < 127) {
|
|
|
|
obstacle.detected_count++;
|
|
|
|
}
|
|
|
|
if (obstacle.detected_count == g.sonar_debounce) {
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text_fmt(MAV_SEVERITY_INFO, "Sonar1 obstacle %u cm",
|
2013-05-28 20:59:21 -03:00
|
|
|
(unsigned)obstacle.sonar1_distance_cm);
|
2013-03-28 18:53:02 -03:00
|
|
|
}
|
2013-03-28 19:36:54 -03:00
|
|
|
obstacle.detected_time_ms = hal.scheduler->millis();
|
2013-03-21 18:49:51 -03:00
|
|
|
obstacle.turn_angle = g.sonar_turn_angle;
|
2013-04-19 18:29:57 -03:00
|
|
|
} else if (obstacle.sonar2_distance_cm <= (uint16_t)g.sonar_trigger_cm) {
|
2013-03-21 18:49:51 -03:00
|
|
|
// we have an object on the right
|
2013-03-28 20:49:08 -03:00
|
|
|
if (obstacle.detected_count < 127) {
|
|
|
|
obstacle.detected_count++;
|
|
|
|
}
|
|
|
|
if (obstacle.detected_count == g.sonar_debounce) {
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text_fmt(MAV_SEVERITY_INFO, "Sonar2 obstacle %u cm",
|
2013-05-28 20:59:21 -03:00
|
|
|
(unsigned)obstacle.sonar2_distance_cm);
|
2013-03-28 18:53:02 -03:00
|
|
|
}
|
2013-03-28 19:36:54 -03:00
|
|
|
obstacle.detected_time_ms = hal.scheduler->millis();
|
2013-03-21 18:49:51 -03:00
|
|
|
obstacle.turn_angle = -g.sonar_turn_angle;
|
|
|
|
}
|
2013-03-28 18:08:14 -03:00
|
|
|
} else {
|
|
|
|
// we have a single sonar
|
2014-06-27 00:18:20 -03:00
|
|
|
obstacle.sonar1_distance_cm = sonar.distance_cm(0);
|
2013-04-18 21:23:57 -03:00
|
|
|
obstacle.sonar2_distance_cm = 0;
|
2013-04-19 18:29:57 -03:00
|
|
|
if (obstacle.sonar1_distance_cm <= (uint16_t)g.sonar_trigger_cm) {
|
2013-03-28 18:08:14 -03:00
|
|
|
// obstacle detected in front
|
2013-03-28 20:49:08 -03:00
|
|
|
if (obstacle.detected_count < 127) {
|
|
|
|
obstacle.detected_count++;
|
|
|
|
}
|
|
|
|
if (obstacle.detected_count == g.sonar_debounce) {
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text_fmt(MAV_SEVERITY_NOTICE, "Sonar obstacle %u cm",
|
2013-05-28 20:59:21 -03:00
|
|
|
(unsigned)obstacle.sonar1_distance_cm);
|
2013-03-28 18:53:02 -03:00
|
|
|
}
|
2013-03-28 18:08:14 -03:00
|
|
|
obstacle.detected_time_ms = hal.scheduler->millis();
|
|
|
|
obstacle.turn_angle = g.sonar_turn_angle;
|
|
|
|
}
|
2013-03-21 18:49:51 -03:00
|
|
|
}
|
|
|
|
|
2013-04-18 21:23:57 -03:00
|
|
|
Log_Write_Sonar();
|
|
|
|
|
2013-03-21 18:49:51 -03:00
|
|
|
// no object detected - reset after the turn time
|
2013-03-28 20:49:08 -03:00
|
|
|
if (obstacle.detected_count >= g.sonar_debounce &&
|
2013-03-28 18:53:02 -03:00
|
|
|
hal.scheduler->millis() > obstacle.detected_time_ms + g.sonar_turn_time*1000) {
|
2015-11-03 01:19:03 -04:00
|
|
|
gcs_send_text_fmt(MAV_SEVERITY_INFO, "Obstacle passed");
|
2013-03-28 20:49:08 -03:00
|
|
|
obstacle.detected_count = 0;
|
|
|
|
obstacle.turn_angle = 0;
|
2013-03-21 18:49:51 -03:00
|
|
|
}
|
|
|
|
}
|