mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-03 14:38:30 -04:00
AP_Notify: add Buzzer
This commit is contained in:
parent
d1cf9f949f
commit
1f0fb3f06b
@ -30,6 +30,7 @@ void AP_Notify::init(void)
|
||||
#endif
|
||||
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
||||
externalled.init();
|
||||
buzzer.init();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -44,5 +45,6 @@ void AP_Notify::update(void)
|
||||
#endif
|
||||
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
||||
externalled.update();
|
||||
buzzer.update();
|
||||
#endif
|
||||
}
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <ToshibaLED_PX4.h>
|
||||
#include <ToneAlarm_PX4.h>
|
||||
#include <ExternalLED.h>
|
||||
#include <Buzzer.h>
|
||||
|
||||
class AP_Notify
|
||||
{
|
||||
@ -62,6 +63,7 @@ private:
|
||||
#elif CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
||||
ToshibaLED_I2C toshibaled;
|
||||
ExternalLED externalled;
|
||||
Buzzer buzzer;
|
||||
#else
|
||||
ToshibaLED_I2C toshibaled;
|
||||
#endif
|
||||
|
169
libraries/AP_Notify/Buzzer.cpp
Normal file
169
libraries/AP_Notify/Buzzer.cpp
Normal file
@ -0,0 +1,169 @@
|
||||
/*
|
||||
Buzzer driver
|
||||
*/
|
||||
/*
|
||||
This program 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <AP_HAL.h>
|
||||
|
||||
#include "Buzzer.h"
|
||||
#include "AP_Notify.h"
|
||||
|
||||
extern const AP_HAL::HAL& hal;
|
||||
|
||||
bool Buzzer::init()
|
||||
{
|
||||
// setup the pin and ensure it's off
|
||||
hal.gpio->pinMode(BUZZER_PIN, GPIO_OUTPUT);
|
||||
on(false);
|
||||
|
||||
// set initial boot states. This prevents us issueing a arming
|
||||
// warning in plane and rover on every boot
|
||||
_flags.armed = AP_Notify::flags.armed;
|
||||
_flags.failsafe_battery = AP_Notify::flags.failsafe_battery;
|
||||
return true;
|
||||
}
|
||||
|
||||
// update - updates led according to timed_updated. Should be called at 50Hz
|
||||
void Buzzer::update()
|
||||
{
|
||||
// reduce 50hz call down to 10hz
|
||||
_counter++;
|
||||
if (_counter < 5) {
|
||||
return;
|
||||
}
|
||||
_counter = 0;
|
||||
|
||||
// complete currently played pattern
|
||||
if (_pattern != NONE) {
|
||||
_pattern_counter++;
|
||||
switch (_pattern) {
|
||||
case SINGLE_BUZZ:
|
||||
// buzz for 10th of a second
|
||||
if (_pattern_counter == 1) {
|
||||
on(true);
|
||||
}else{
|
||||
on(false);
|
||||
_pattern = NONE;
|
||||
}
|
||||
return;
|
||||
case DOUBLE_BUZZ:
|
||||
// buzz for 10th of a second
|
||||
switch (_pattern_counter) {
|
||||
case 1:
|
||||
on(true);
|
||||
break;
|
||||
case 2:
|
||||
on(false);
|
||||
break;
|
||||
case 3:
|
||||
on(true);
|
||||
break;
|
||||
case 4:
|
||||
default:
|
||||
on(false);
|
||||
_pattern = NONE;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
case GPS_GLITCH:
|
||||
// play bethoven's 5th type buzz (three fast, one long)
|
||||
switch (_pattern_counter) {
|
||||
case 1:
|
||||
case 3:
|
||||
case 5:
|
||||
case 7:
|
||||
on(true);
|
||||
break;
|
||||
case 2:
|
||||
case 4:
|
||||
case 6:
|
||||
on(false);
|
||||
break;
|
||||
case 17:
|
||||
on(false);
|
||||
_pattern = NONE;
|
||||
break;
|
||||
default:
|
||||
// do nothing
|
||||
break;
|
||||
}
|
||||
return;
|
||||
default:
|
||||
// do nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check if armed status has changed
|
||||
if (_flags.armed != AP_Notify::flags.armed) {
|
||||
_flags.armed = AP_Notify::flags.armed;
|
||||
if (_flags.armed) {
|
||||
// double buzz when armed
|
||||
play_pattern(DOUBLE_BUZZ);
|
||||
}else{
|
||||
// single buzz when disarmed
|
||||
play_pattern(SINGLE_BUZZ);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// check gps glitch
|
||||
if (_flags.gps_glitching != AP_Notify::flags.gps_glitching) {
|
||||
_flags.gps_glitching = AP_Notify::flags.gps_glitching;
|
||||
if (_flags.gps_glitching) {
|
||||
// gps glitch warning buzz
|
||||
play_pattern(GPS_GLITCH);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// check gps failsafe
|
||||
if (_flags.failsafe_gps != AP_Notify::flags.failsafe_gps) {
|
||||
_flags.failsafe_gps = AP_Notify::flags.failsafe_gps;
|
||||
if (_flags.failsafe_gps) {
|
||||
// gps glitch warning buzz
|
||||
play_pattern(GPS_GLITCH);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// if battery failsafe constantly single buzz
|
||||
if (AP_Notify::flags.failsafe_battery) {
|
||||
play_pattern(SINGLE_BUZZ);
|
||||
}
|
||||
}
|
||||
|
||||
// on - turns the buzzer on or off
|
||||
void Buzzer::on(bool turn_on)
|
||||
{
|
||||
// return immediately if nothing to do
|
||||
if (_flags.on == turn_on) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update state
|
||||
_flags.on = turn_on;
|
||||
|
||||
// pull pin high or low
|
||||
hal.gpio->write(BUZZER_PIN, _flags.on);
|
||||
}
|
||||
|
||||
/// play_pattern - plays the defined buzzer pattern
|
||||
void Buzzer::play_pattern(BuzzerPattern pattern_id)
|
||||
{
|
||||
_pattern = pattern_id;
|
||||
_pattern_counter = 0;
|
||||
}
|
69
libraries/AP_Notify/Buzzer.h
Normal file
69
libraries/AP_Notify/Buzzer.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
Buzzer driver
|
||||
*/
|
||||
/*
|
||||
This program 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef __BUZZER_H__
|
||||
#define __BUZZER_H__
|
||||
|
||||
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1
|
||||
# define BUZZER_PIN 63 // pin 63 on APM1
|
||||
#elif CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
||||
# define BUZZER_PIN 59 // pin 59 on APM2
|
||||
#endif
|
||||
|
||||
class Buzzer
|
||||
{
|
||||
public:
|
||||
/// Constructor
|
||||
Buzzer() : _counter(0), _pattern(NONE), _pattern_counter(0) {}
|
||||
|
||||
/// init - initialise the buzzer
|
||||
bool init(void);
|
||||
|
||||
/// update - updates buzzer according to timed_updated. Should be called at 50Hz
|
||||
void update();
|
||||
|
||||
/// on - turns the buzzer on or off
|
||||
void on(bool on_off);
|
||||
|
||||
enum BuzzerPattern {
|
||||
NONE = 0,
|
||||
SINGLE_BUZZ = 1,
|
||||
DOUBLE_BUZZ = 2,
|
||||
GPS_GLITCH = 3
|
||||
};
|
||||
|
||||
/// play_pattern - plays the defined buzzer pattern
|
||||
void play_pattern(BuzzerPattern pattern_id);
|
||||
|
||||
private:
|
||||
|
||||
/// buzzer_flag_type - bitmask of current state and ap_notify states we track
|
||||
struct buzzer_flag_type {
|
||||
uint8_t on : 1; // 1 if the buzzer is currently on
|
||||
uint8_t gps_glitching : 1; // 1 if gps position is not good
|
||||
uint8_t arming : 1; // 1 if we are beginning the arming process
|
||||
uint8_t armed : 1; // 0 = disarmed, 1 = armed
|
||||
uint8_t failsafe_battery : 1; // 1 if battery failsafe has triggered
|
||||
uint8_t failsafe_gps : 1; // 1 if gps failsafe
|
||||
} _flags;
|
||||
|
||||
uint8_t _counter; // reduces 50hz update down to 10hz for internal processing
|
||||
BuzzerPattern _pattern; // current pattern
|
||||
uint8_t _pattern_counter; // used to time on/off of current patter
|
||||
};
|
||||
|
||||
#endif // __BUZZER_H__
|
Loading…
Reference in New Issue
Block a user