APM:Libraries
AP_GPS_UAVCAN.cpp
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 //
17 // UAVCAN GPS driver
18 //
19 #include "AP_GPS_UAVCAN.h"
20 #include <stdint.h>
21 
22 #if HAL_WITH_UAVCAN
23 #include <AP_UAVCAN/AP_UAVCAN.h>
26 
27 extern const AP_HAL::HAL& hal;
28 
29 #define debug_gps_uavcan(level, fmt, args...) do { if ((level) <= AP_BoardConfig_CAN::get_can_debug()) { printf(fmt, ##args); }} while (0)
30 
32  AP_GPS_Backend(_gps, _state, _port)
33 {
34  _sem_gnss = hal.util->new_semaphore();
35 }
36 
37 // For each instance we need to deregister from AP_UAVCAN class
39 {
40  if (hal.can_mgr[_manager] != nullptr) {
41  AP_UAVCAN *ap_uavcan = hal.can_mgr[_manager]->get_UAVCAN();
42  if (ap_uavcan != nullptr) {
43  ap_uavcan->remove_gps_listener(this);
44 
45  debug_gps_uavcan(2, "AP_GPS_UAVCAN destructed\n\r");
46  }
47  }
48 }
49 
50 void AP_GPS_UAVCAN::set_uavcan_manager(uint8_t mgr)
51 {
52  _manager = mgr;
53 }
54 
55 // Consume new data and mark it received
56 bool AP_GPS_UAVCAN::read(void)
57 {
58  if (_sem_gnss->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
59  if (_new_data) {
60  _new_data = false;
61 
62  state = _interm_state;
63  _sem_gnss->give();
64 
65  return true;
66  }
67 
68  _sem_gnss->give();
69  }
70  return false;
71 }
72 
74 {
75  if (_sem_gnss->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
76  _interm_state = msg;
77  _new_data = true;
78  _sem_gnss->give();
79  }
80 }
81 
82 #endif // HAL_WITH_UAVCAN
Definition: AP_GPS.h:48
~AP_GPS_UAVCAN() override
AP_HAL::Util * util
Definition: HAL.h:115
const AP_HAL::HAL & hal
Definition: UARTDriver.cpp:37
#define HAL_SEMAPHORE_BLOCK_FOREVER
Definition: Semaphores.h:5
virtual Semaphore * new_semaphore(void)
Definition: Util.h:108
bool read() override
AP_HAL::CANManager ** can_mgr
Definition: HAL.h:120
void set_uavcan_manager(uint8_t mgr)
void remove_gps_listener(AP_GPS_Backend *rem_listener)
AP_GPS::GPS_State & state
public state for this instance
Definition: GPS_Backend.h:71
void handle_gnss_msg(const AP_GPS::GPS_State &msg) override
AP_GPS_UAVCAN(AP_GPS &_gps, AP_GPS::GPS_State &_state, AP_HAL::UARTDriver *_port)