ToneAlarm.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * AP_ToneAlarm driver
  3. */
  4. /*
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <AP_HAL/AP_HAL.h>
  19. #include <AP_Math/AP_Math.h>
  20. #include "ToneAlarm.h"
  21. #include "AP_Notify.h"
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <AP_Filesystem/AP_Filesystem.h>
  25. extern const AP_HAL::HAL& hal;
  26. // Tunes follow the syntax of the Microsoft GWBasic/QBasic PLAY
  27. // statement, with some exceptions and extensions.
  28. // See http://firmware.ardupilot.org/Tools/ToneTester/
  29. const AP_ToneAlarm::Tone AP_ToneAlarm::_tones[] {
  30. #define AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK 0
  31. { "MFT200L4<<<B#A#2", false },
  32. #define AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK 1
  33. { "MFT100L4>B#A#2P8B#A#2", false },
  34. #define AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK 2
  35. { "MFT200L4<B#", false },
  36. #define AP_NOTIFY_TONE_LOUD_NEU_FEEDBACK 3
  37. { "MFT100L4>B#", false },
  38. #define AP_NOTIFY_TONE_QUIET_POS_FEEDBACK 4
  39. { "MFT200L4<A#B#", false },
  40. #define AP_NOTIFY_TONE_LOUD_POS_FEEDBACK 5
  41. { "MFT100L4>A#B#", false },
  42. #define AP_NOTIFY_TONE_LOUD_READY_OR_FINISHED 6
  43. { "MFT100L4>G#6A#6B#4", false },
  44. #define AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED 7
  45. { "MFT200L4<G#6A#6B#4", false },
  46. #define AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED 8
  47. { "MFT100L4>A#A#A#A#", false },
  48. #define AP_NOTIFY_TONE_QUIET_ARMING_WARNING 9
  49. { "MNT75L1O2G", false },
  50. #define AP_NOTIFY_TONE_LOUD_WP_COMPLETE 10
  51. { "MFT200L8G>C3", false },
  52. #define AP_NOTIFY_TONE_LOUD_LAND_WARNING_CTS 11
  53. { "MBT200L2A-G-A-G-A-G-", true },
  54. #define AP_NOTIFY_TONE_LOUD_VEHICLE_LOST_CTS 12
  55. { "MBT200>A#1", true },
  56. #define AP_NOTIFY_TONE_LOUD_BATTERY_ALERT_CTS 13
  57. { "MBNT255>A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8", true },
  58. #define AP_NOTIFY_TONE_QUIET_COMPASS_CALIBRATING_CTS 14
  59. { "MBNT255<C16P2", true },
  60. #define AP_NOTIFY_TONE_WAITING_FOR_THROW 15
  61. { "MBNT90L4O2A#O3DFN0N0N0", true},
  62. #define AP_NOTIFY_TONE_LOUD_1 16
  63. { "MFT100L8>B", false},
  64. #define AP_NOTIFY_TONE_LOUD_2 17
  65. { "MFT100L8>BB", false},
  66. #define AP_NOTIFY_TONE_LOUD_3 18
  67. { "MFT100L8>BBB", false},
  68. #define AP_NOTIFY_TONE_LOUD_4 19
  69. { "MFT100L8>BBBB", false},
  70. #define AP_NOTIFY_TONE_LOUD_5 20
  71. { "MFT100L8>BBBBB", false},
  72. #define AP_NOTIFY_TONE_LOUD_6 21
  73. { "MFT100L8>BBBBBB", false},
  74. #define AP_NOTIFY_TONE_LOUD_7 22
  75. { "MFT100L8>BBBBBBB", false},
  76. #define AP_NOTIFY_TONE_TUNING_START 23
  77. { "MFT100L20>C#D#", false},
  78. #define AP_NOTIFY_TONE_TUNING_SAVE 24
  79. { "MFT100L10DBDB>", false},
  80. #define AP_NOTIFY_TONE_TUNING_ERROR 25
  81. { "MFT100L10>BBBBBBBB", false},
  82. #define AP_NOTIFY_TONE_LEAK_DETECTED 26
  83. { "MBT255L8>A+AA-", true},
  84. #define AP_NOTIFY_TONE_QUIET_SHUTDOWN 27
  85. { "MFMST200L32O3ceP32cdP32ceP32c<c>c<cccP8L32>c>c<P32<c<c", false },
  86. #define AP_NOTIFY_TONE_QUIET_NOT_READY_OR_NOT_FINISHED 28
  87. { "MFT200L4<B#4A#6G#6", false },
  88. #define AP_NOTIFY_TONE_STARTUP 29
  89. { "MFT240L8O4aO5dcO4aO5dcO4aO5dcL16dcdcdcdc", false },
  90. #define AP_NOTIFY_TONE_NO_SDCARD 30
  91. { "MNBGG", false },
  92. };
  93. bool AP_ToneAlarm::init()
  94. {
  95. if (pNotify->buzzer_enabled() == false) {
  96. return false;
  97. }
  98. if (!hal.util->toneAlarm_init()) {
  99. return false;
  100. }
  101. // set initial boot states. This prevents us issuing a arming
  102. // warning in plane and rover on every boot
  103. flags.armed = AP_Notify::flags.armed;
  104. flags.failsafe_battery = AP_Notify::flags.failsafe_battery;
  105. flags.pre_arm_check = 1;
  106. _cont_tone_playing = -1;
  107. hal.scheduler->register_timer_process(FUNCTOR_BIND(this, &AP_ToneAlarm::_timer_task, void));
  108. #if HAVE_FILESYSTEM_SUPPORT && CONFIG_HAL_BOARD != HAL_BOARD_LINUX
  109. // if we don't have a SDcard then play a failure tone instead of
  110. // normal startup tone. This gives the user a chance to fix it
  111. // before they try to arm. We don't do this on Linux as Linux
  112. // flight controllers don't usually have removable storage
  113. struct stat st;
  114. if (AP::FS().stat(HAL_BOARD_STORAGE_DIRECTORY, &st) != 0) {
  115. play_tone(AP_NOTIFY_TONE_NO_SDCARD);
  116. return true;
  117. }
  118. #endif
  119. play_tone(AP_NOTIFY_TONE_STARTUP);
  120. return true;
  121. }
  122. // play_tune - play one of the pre-defined tunes
  123. void AP_ToneAlarm::play_tone(const uint8_t tone_index)
  124. {
  125. uint32_t tnow_ms = AP_HAL::millis();
  126. const Tone &tone_requested = _tones[tone_index];
  127. if (tone_requested.continuous) {
  128. _cont_tone_playing = tone_index;
  129. }
  130. _tone_playing = tone_index;
  131. _tone_beginning_ms = tnow_ms;
  132. play_tune(tone_requested.str);
  133. }
  134. void AP_ToneAlarm::_timer_task()
  135. {
  136. WITH_SEMAPHORE(_sem);
  137. _mml_player.update();
  138. }
  139. void AP_ToneAlarm::play_tune(const char *str)
  140. {
  141. WITH_SEMAPHORE(_sem);
  142. _mml_player.stop();
  143. strncpy(_tone_buf, str, AP_NOTIFY_TONEALARM_TONE_BUF_SIZE);
  144. _tone_buf[AP_NOTIFY_TONEALARM_TONE_BUF_SIZE-1] = 0;
  145. _mml_player.play(_tone_buf);
  146. }
  147. void AP_ToneAlarm::stop_cont_tone()
  148. {
  149. if (_cont_tone_playing == _tone_playing) {
  150. play_tune("");
  151. _tone_playing = -1;
  152. }
  153. _cont_tone_playing = -1;
  154. }
  155. void AP_ToneAlarm::check_cont_tone()
  156. {
  157. uint32_t tnow_ms = AP_HAL::millis();
  158. // if we are supposed to be playing a continuous tone,
  159. // and it was interrupted, and the interrupting tone has timed out,
  160. // resume the continuous tone
  161. if (_cont_tone_playing != -1 && _tone_playing != _cont_tone_playing && tnow_ms-_tone_beginning_ms > AP_NOTIFY_TONEALARM_MAX_TONE_LENGTH_MS) {
  162. play_tone(_cont_tone_playing);
  163. }
  164. }
  165. // update - updates led according to timed_updated. Should be called at 50Hz
  166. void AP_ToneAlarm::update()
  167. {
  168. // exit if buzzer is not enabled
  169. if (pNotify->buzzer_enabled() == false) {
  170. return;
  171. }
  172. check_cont_tone();
  173. if (AP_Notify::flags.powering_off) {
  174. if (!flags.powering_off) {
  175. play_tone(AP_NOTIFY_TONE_QUIET_SHUTDOWN);
  176. }
  177. flags.powering_off = AP_Notify::flags.powering_off;
  178. return;
  179. }
  180. if (AP_Notify::flags.compass_cal_running != flags.compass_cal_running) {
  181. if (AP_Notify::flags.compass_cal_running) {
  182. play_tone(AP_NOTIFY_TONE_QUIET_COMPASS_CALIBRATING_CTS);
  183. play_tone(AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
  184. } else {
  185. if (_cont_tone_playing == AP_NOTIFY_TONE_QUIET_COMPASS_CALIBRATING_CTS) {
  186. stop_cont_tone();
  187. }
  188. }
  189. }
  190. flags.compass_cal_running = AP_Notify::flags.compass_cal_running;
  191. if (AP_Notify::events.compass_cal_canceled) {
  192. play_tone(AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
  193. return;
  194. }
  195. if (AP_Notify::events.initiated_compass_cal) {
  196. play_tone(AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
  197. return;
  198. }
  199. if (AP_Notify::events.compass_cal_saved) {
  200. play_tone(AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED);
  201. return;
  202. }
  203. if (AP_Notify::events.compass_cal_failed) {
  204. play_tone(AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
  205. return;
  206. }
  207. // don't play other tones if compass cal is running
  208. if (AP_Notify::flags.compass_cal_running) {
  209. return;
  210. }
  211. // notify the user when autotune or mission completes
  212. if (AP_Notify::flags.armed && (AP_Notify::events.autotune_complete || AP_Notify::events.mission_complete)) {
  213. play_tone(AP_NOTIFY_TONE_LOUD_READY_OR_FINISHED);
  214. }
  215. //notify the user when autotune fails
  216. if (AP_Notify::flags.armed && (AP_Notify::events.autotune_failed)) {
  217. play_tone(AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK);
  218. }
  219. // notify the user when a waypoint completes
  220. if (AP_Notify::events.waypoint_complete) {
  221. play_tone(AP_NOTIFY_TONE_LOUD_WP_COMPLETE);
  222. }
  223. // notify the user when their mode change was successful
  224. if (AP_Notify::events.user_mode_change) {
  225. if (AP_Notify::flags.armed) {
  226. play_tone(AP_NOTIFY_TONE_LOUD_NEU_FEEDBACK);
  227. } else {
  228. play_tone(AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
  229. }
  230. }
  231. // notify the user when their mode change failed
  232. if (AP_Notify::events.user_mode_change_failed) {
  233. if (AP_Notify::flags.armed) {
  234. play_tone(AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK);
  235. } else {
  236. play_tone(AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
  237. }
  238. }
  239. // failsafe initiated mode change
  240. if (AP_Notify::events.failsafe_mode_change) {
  241. play_tone(AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED);
  242. }
  243. // notify the user when arming fails
  244. if (AP_Notify::events.arming_failed) {
  245. play_tone(AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
  246. }
  247. // notify the user when RC contact is lost
  248. if (flags.failsafe_radio != AP_Notify::flags.failsafe_radio) {
  249. flags.failsafe_radio = AP_Notify::flags.failsafe_radio;
  250. if (flags.failsafe_radio) {
  251. // armed case handled by events.failsafe_mode_change
  252. if (!AP_Notify::flags.armed) {
  253. play_tone(AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
  254. }
  255. } else {
  256. if (AP_Notify::flags.armed) {
  257. play_tone(AP_NOTIFY_TONE_LOUD_POS_FEEDBACK);
  258. } else {
  259. play_tone(AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
  260. }
  261. }
  262. }
  263. // notify the user when pre_arm checks are passing
  264. if (flags.pre_arm_check != AP_Notify::flags.pre_arm_check) {
  265. flags.pre_arm_check = AP_Notify::flags.pre_arm_check;
  266. if (flags.pre_arm_check) {
  267. play_tone(AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED);
  268. _have_played_ready_tone = true;
  269. } else {
  270. // only play sad tone if we've ever played happy tone:
  271. if (_have_played_ready_tone) {
  272. play_tone(AP_NOTIFY_TONE_QUIET_NOT_READY_OR_NOT_FINISHED);
  273. }
  274. }
  275. }
  276. // check if arming status has changed
  277. if (flags.armed != AP_Notify::flags.armed) {
  278. flags.armed = AP_Notify::flags.armed;
  279. if (flags.armed) {
  280. // arming tune
  281. play_tone(AP_NOTIFY_TONE_QUIET_ARMING_WARNING);
  282. } else {
  283. // disarming tune
  284. play_tone(AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
  285. if (!flags.leak_detected) {
  286. stop_cont_tone();
  287. }
  288. }
  289. }
  290. // check if battery status has changed
  291. if (flags.failsafe_battery != AP_Notify::flags.failsafe_battery) {
  292. flags.failsafe_battery = AP_Notify::flags.failsafe_battery;
  293. if (flags.failsafe_battery) {
  294. // battery warning tune
  295. play_tone(AP_NOTIFY_TONE_LOUD_BATTERY_ALERT_CTS);
  296. }
  297. }
  298. // check parachute release
  299. if (flags.parachute_release != AP_Notify::flags.parachute_release) {
  300. flags.parachute_release = AP_Notify::flags.parachute_release;
  301. if (flags.parachute_release) {
  302. // parachute release warning tune
  303. play_tone(AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED);
  304. }
  305. }
  306. // lost vehicle tone
  307. if (flags.vehicle_lost != AP_Notify::flags.vehicle_lost) {
  308. flags.vehicle_lost = AP_Notify::flags.vehicle_lost;
  309. if (flags.vehicle_lost) {
  310. play_tone(AP_NOTIFY_TONE_LOUD_VEHICLE_LOST_CTS);
  311. } else {
  312. stop_cont_tone();
  313. }
  314. }
  315. // waiting to be thrown vehicle tone
  316. if (flags.waiting_for_throw != AP_Notify::flags.waiting_for_throw) {
  317. flags.waiting_for_throw = AP_Notify::flags.waiting_for_throw;
  318. if (flags.waiting_for_throw) {
  319. play_tone(AP_NOTIFY_TONE_WAITING_FOR_THROW);
  320. } else {
  321. stop_cont_tone();
  322. }
  323. }
  324. if (flags.leak_detected != AP_Notify::flags.leak_detected) {
  325. flags.leak_detected = AP_Notify::flags.leak_detected;
  326. if (flags.leak_detected) {
  327. play_tone(AP_NOTIFY_TONE_LEAK_DETECTED);
  328. } else {
  329. stop_cont_tone();
  330. }
  331. }
  332. if (AP_Notify::events.tune_started) {
  333. play_tone(AP_NOTIFY_TONE_TUNING_START);
  334. AP_Notify::events.tune_started = 0;
  335. }
  336. if (AP_Notify::events.tune_next) {
  337. // signify which parameter in the set is starting
  338. play_tone(AP_NOTIFY_TONE_LOUD_1 + (AP_Notify::events.tune_next-1));
  339. AP_Notify::events.tune_next = 0;
  340. }
  341. if (AP_Notify::events.tune_save) {
  342. play_tone(AP_NOTIFY_TONE_TUNING_SAVE);
  343. AP_Notify::events.tune_save = 0;
  344. }
  345. if (AP_Notify::events.tune_error) {
  346. play_tone(AP_NOTIFY_TONE_TUNING_ERROR);
  347. AP_Notify::events.tune_error = 0;
  348. }
  349. }
  350. /*
  351. * handle a PLAY_TUNE message
  352. */
  353. void AP_ToneAlarm::handle_play_tune(const mavlink_message_t &msg)
  354. {
  355. // decode mavlink message
  356. mavlink_play_tune_t packet;
  357. mavlink_msg_play_tune_decode(&msg, &packet);
  358. WITH_SEMAPHORE(_sem);
  359. _mml_player.stop();
  360. strncpy(_tone_buf, packet.tune, MIN(sizeof(packet.tune), sizeof(_tone_buf)-1));
  361. _tone_buf[sizeof(_tone_buf)-1] = 0;
  362. uint8_t len = strlen(_tone_buf);
  363. uint8_t len2 = strnlen(packet.tune2, sizeof(packet.tune2));
  364. len2 = MIN((sizeof(_tone_buf)-1)-len, len2);
  365. strncpy(_tone_buf+len, packet.tune2, len2);
  366. _tone_buf[sizeof(_tone_buf)-1] = 0;
  367. _mml_player.play(_tone_buf);
  368. }