|
1 | 1 | /****************************************************************************
|
2 | 2 | *
|
3 |
| - * Copyright (c) 2017 PX4 Development Team. All rights reserved. |
| 3 | + * Copyright (c) 2017-2021 PX4 Development Team. All rights reserved. |
4 | 4 | *
|
5 | 5 | * Redistribution and use in source and binary forms, with or without
|
6 | 6 | * modification, are permitted provided that the following conditions
|
|
40 | 40 |
|
41 | 41 | #include "status_display.h"
|
42 | 42 |
|
| 43 | +#include <board_config.h> |
| 44 | +#include <px4_log.h> |
| 45 | +#include <matrix/math.hpp> |
| 46 | +#include <drivers/drv_led.h> |
| 47 | + |
| 48 | +using namespace time_literals; |
| 49 | + |
43 | 50 | namespace events
|
44 | 51 | {
|
45 | 52 | namespace status
|
46 | 53 | {
|
47 | 54 |
|
48 | 55 | void StatusDisplay::set_leds()
|
49 | 56 | {
|
50 |
| - // Put your LED handling here |
| 57 | + bool gps_lock_valid = _vehicle_status_flags_sub.get().condition_global_position_valid; |
| 58 | + bool home_position_valid = _vehicle_status_flags_sub.get().condition_home_position_valid; |
| 59 | + int nav_state = _vehicle_status_sub.get().nav_state; |
| 60 | + |
| 61 | +#if defined(BOARD_FRONT_LED_MASK) |
| 62 | + |
| 63 | + // try to publish the static LED for the first 10s |
| 64 | + // this avoid the problem if a LED driver did not subscribe to the topic yet |
| 65 | + if (hrt_absolute_time() < 10_s) { |
| 66 | + |
| 67 | + // set the base color for front LED |
| 68 | + _led_control.led_mask = BOARD_FRONT_LED_MASK; |
| 69 | + _led_control.color = led_control_s::COLOR_WHITE; |
| 70 | + _led_control.mode = led_control_s::MODE_ON; |
| 71 | + |
| 72 | + publish(); |
| 73 | + } |
| 74 | + |
| 75 | +#endif // BOARD_FRONT_LED_MASK |
| 76 | + |
| 77 | +#if defined(BOARD_REAR_LED_MASK) |
| 78 | + // set the led mask for the status led which are the back LED |
| 79 | + _led_control.led_mask = BOARD_REAR_LED_MASK; |
| 80 | + |
| 81 | + if (nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_RTL |
| 82 | + || nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_LAND) { |
| 83 | + _led_control.color = led_control_s::COLOR_PURPLE; |
| 84 | + |
| 85 | + } else if (nav_state == vehicle_status_s::NAVIGATION_STATE_ALTCTL) { |
| 86 | + _led_control.color = led_control_s::COLOR_BLUE; |
| 87 | + |
| 88 | + } else if (nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION) { |
| 89 | + _led_control.color = led_control_s::COLOR_GREEN; |
| 90 | + |
| 91 | + } else { |
| 92 | + _led_control.color = led_control_s::COLOR_YELLOW; // TODO fix yellow and purple error |
| 93 | + } |
| 94 | + |
| 95 | + // blink if no GPS and home are set |
| 96 | + if (gps_lock_valid && home_position_valid) { |
| 97 | + _led_control.mode = led_control_s::MODE_ON; |
| 98 | + |
| 99 | + } else { |
| 100 | + _led_control.mode = led_control_s::MODE_BLINK_NORMAL; |
| 101 | + } |
| 102 | + |
| 103 | + // handle battery warnings, once a state is reached it can not be reset |
| 104 | + if (_battery_status_sub.get().warning == battery_status_s::BATTERY_WARNING_CRITICAL || _critical_battery) { |
| 105 | + _led_control.color = led_control_s::COLOR_RED; |
| 106 | + _led_control.mode = led_control_s::MODE_BLINK_FAST; |
| 107 | + _critical_battery = true; |
| 108 | + |
| 109 | + } else if (_battery_status_sub.get().warning == battery_status_s::BATTERY_WARNING_LOW || _low_battery) { |
| 110 | + _led_control.color = led_control_s::COLOR_RED; |
| 111 | + _led_control.mode = led_control_s::MODE_FLASH; |
| 112 | + _low_battery = true; |
| 113 | + } |
| 114 | + |
| 115 | + if (nav_state != _old_nav_state |
| 116 | + || gps_lock_valid != _old_gps_lock_valid |
| 117 | + || home_position_valid != _old_home_position_valid |
| 118 | + || _battery_status_sub.get().warning != _old_battery_status_warning) { |
| 119 | + |
| 120 | + publish(); |
| 121 | + } |
| 122 | + |
| 123 | +#endif // BOARD_REAR_LED_MASK |
| 124 | + |
| 125 | + // copy actual state |
| 126 | + _old_nav_state = nav_state; |
| 127 | + _old_gps_lock_valid = gps_lock_valid; |
| 128 | + _old_home_position_valid = home_position_valid; |
| 129 | + _old_battery_status_warning = _battery_status_sub.get().warning; |
51 | 130 | }
|
52 | 131 |
|
53 | 132 | } /* namespace status */
|
|
0 commit comments