2017-04-02 11:56:26 -03:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// UAVCAN GPS driver
|
|
|
|
//
|
|
|
|
#include "AP_GPS_UAVCAN.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2017-05-06 06:13:31 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig_CAN.h>
|
2017-04-02 11:56:26 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2017-05-06 06:13:31 -03:00
|
|
|
#define debug_gps_uavcan(level, fmt, args...) do { if ((level) <= AP_BoardConfig_CAN::get_can_debug()) { printf(fmt, ##args); }} while (0)
|
2017-04-02 11:56:26 -03:00
|
|
|
|
|
|
|
AP_GPS_UAVCAN::AP_GPS_UAVCAN(AP_GPS &_gps, AP_GPS::GPS_State &_state, AP_HAL::UARTDriver *_port) :
|
|
|
|
AP_GPS_Backend(_gps, _state, _port)
|
|
|
|
{
|
|
|
|
_new_data = false;
|
|
|
|
_sem_gnss = hal.util->new_semaphore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each instance we need to deregister from AP_UAVCAN class
|
|
|
|
AP_GPS_UAVCAN::~AP_GPS_UAVCAN()
|
|
|
|
{
|
2017-05-06 06:13:31 -03:00
|
|
|
if (hal.can_mgr[_manager] != nullptr) {
|
|
|
|
AP_UAVCAN *ap_uavcan = hal.can_mgr[_manager]->get_UAVCAN();
|
2017-04-02 11:56:26 -03:00
|
|
|
if (ap_uavcan != nullptr) {
|
|
|
|
ap_uavcan->remove_gps_listener(this);
|
|
|
|
|
|
|
|
debug_gps_uavcan(2, "AP_GPS_UAVCAN destructed\n\r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 06:13:31 -03:00
|
|
|
void AP_GPS_UAVCAN::set_uavcan_manager(uint8_t mgr)
|
|
|
|
{
|
|
|
|
_manager = mgr;
|
|
|
|
}
|
|
|
|
|
2017-04-02 11:56:26 -03:00
|
|
|
// Consume new data and mark it received
|
|
|
|
bool AP_GPS_UAVCAN::read(void)
|
|
|
|
{
|
2017-05-02 02:58:35 -03:00
|
|
|
if (_sem_gnss->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2017-04-02 11:56:26 -03:00
|
|
|
if (_new_data) {
|
|
|
|
_new_data = false;
|
|
|
|
|
|
|
|
state = _interm_state;
|
|
|
|
_sem_gnss->give();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_sem_gnss->give();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_GPS_UAVCAN::handle_gnss_msg(const AP_GPS::GPS_State &msg)
|
|
|
|
{
|
2017-05-02 02:58:35 -03:00
|
|
|
if (_sem_gnss->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2017-04-02 11:56:26 -03:00
|
|
|
_interm_state = msg;
|
|
|
|
_new_data = true;
|
|
|
|
_sem_gnss->give();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAL_WITH_UAVCAN
|