mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-02 14:13:42 -04:00
91dabbe418
Just setting up the periodic callback sampling time on initialization may not work well for sensors that need to request for a sample with a bus transaction, sleep and then read the new data. That's because the function will be kept calling at a periodic rate, while the time in which we can read the value is not really that sampling time, but rather the time in which sensor was last read + the time spent in the function before sending a new sample request. Instead of creating a new type of thread to handle this case, just implement the minimal and easy case of updating the period for this callback, that can only be called from inside the callback function.
50 lines
1.5 KiB
C++
50 lines
1.5 KiB
C++
/*
|
|
* This file 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 file 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/>.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <inttypes.h>
|
|
#include <AP_HAL/HAL.h>
|
|
#include "Semaphores.h"
|
|
#include "Scheduler.h"
|
|
|
|
namespace PX4 {
|
|
|
|
class DeviceBus {
|
|
public:
|
|
DeviceBus(uint8_t _thread_priority = APM_I2C_PRIORITY) :
|
|
thread_priority(_thread_priority) {}
|
|
|
|
struct DeviceBus *next;
|
|
Semaphore semaphore;
|
|
|
|
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb, AP_HAL::Device *hal_device);
|
|
bool adjust_timer(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec);
|
|
static void *bus_thread(void *arg);
|
|
|
|
private:
|
|
struct callback_info {
|
|
struct callback_info *next;
|
|
AP_HAL::Device::PeriodicCb cb;
|
|
uint32_t period_usec;
|
|
uint64_t next_usec;
|
|
} *callbacks;
|
|
uint8_t thread_priority;
|
|
pthread_t thread_ctx;
|
|
bool thread_started;
|
|
AP_HAL::Device *hal_device;
|
|
};
|
|
|
|
}
|