mirror of https://github.com/ArduPilot/ardupilot
AP_Scripting: add CANSensor and manaul bindings to load.
This commit is contained in:
parent
e29dd0e7e7
commit
44276be3a1
|
@ -21,6 +21,7 @@
|
|||
#include <GCS_MAVLink/GCS.h>
|
||||
#include <AP_Filesystem/AP_Filesystem.h>
|
||||
#include <AP_HAL/I2CDevice.h>
|
||||
#include "AP_Scripting_CANSensor.h"
|
||||
|
||||
#ifndef SCRIPTING_MAX_NUM_I2C_DEVICE
|
||||
#define SCRIPTING_MAX_NUM_I2C_DEVICE 4
|
||||
|
@ -67,6 +68,11 @@ public:
|
|||
uint8_t num_i2c_devices;
|
||||
AP_HAL::OwnPtr<AP_HAL::I2CDevice> *_i2c_dev[SCRIPTING_MAX_NUM_I2C_DEVICE];
|
||||
|
||||
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
// Scripting CAN sensor
|
||||
ScriptingCANSensor *_CAN_dev;
|
||||
#endif
|
||||
|
||||
// mission item buffer
|
||||
static const int mission_cmd_queue_size = 5;
|
||||
struct scripting_mission_cmd {
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
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/>.
|
||||
*/
|
||||
/*
|
||||
Scripting CANSensor class, for easy scripting CAN support
|
||||
*/
|
||||
#include "AP_Scripting_CANSensor.h"
|
||||
|
||||
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
|
||||
// handler for outgoing frames, using uint32
|
||||
bool ScriptingCANSensor::write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us)
|
||||
{
|
||||
return CANSensor::write_frame(out_frame, timeout_us);
|
||||
};
|
||||
|
||||
// handler for incoming frames, add to buffers
|
||||
void ScriptingCANSensor::handle_frame(AP_HAL::CANFrame &frame)
|
||||
{
|
||||
WITH_SEMAPHORE(sem);
|
||||
if (buffer_list != nullptr) {
|
||||
buffer_list->handle_frame(frame);
|
||||
}
|
||||
}
|
||||
|
||||
// add a new buffer to this sensor
|
||||
ScriptingCANBuffer* ScriptingCANSensor::add_buffer(uint32_t buffer_len)
|
||||
{
|
||||
WITH_SEMAPHORE(sem);
|
||||
ScriptingCANBuffer *new_buff = new ScriptingCANBuffer(*this, buffer_len);
|
||||
if (buffer_list == nullptr) {
|
||||
buffer_list = new_buff;
|
||||
} else {
|
||||
buffer_list->add_buffer(new_buff);
|
||||
}
|
||||
return new_buff;
|
||||
}
|
||||
|
||||
// Call main sensor write method
|
||||
bool ScriptingCANBuffer::write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us)
|
||||
{
|
||||
return sensor.write_frame(out_frame, timeout_us);
|
||||
};
|
||||
|
||||
// read a frame from the buffer
|
||||
bool ScriptingCANBuffer::read_frame(AP_HAL::CANFrame &frame)
|
||||
{
|
||||
return buffer.pop(frame);
|
||||
}
|
||||
|
||||
// recursively add frame to buffer
|
||||
void ScriptingCANBuffer::handle_frame(AP_HAL::CANFrame &frame)
|
||||
{
|
||||
WITH_SEMAPHORE(sem);
|
||||
buffer.push(frame);
|
||||
if (next != nullptr) {
|
||||
next->handle_frame(frame);
|
||||
}
|
||||
}
|
||||
|
||||
// recursively add new buffer
|
||||
void ScriptingCANBuffer::add_buffer(ScriptingCANBuffer* new_buff) {
|
||||
WITH_SEMAPHORE(sem);
|
||||
if (next == nullptr) {
|
||||
next = new_buff;
|
||||
return;
|
||||
}
|
||||
next->add_buffer(new_buff);
|
||||
}
|
||||
|
||||
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
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/>.
|
||||
*/
|
||||
/*
|
||||
Scripting CANSensor class, for easy scripting CAN support
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AP_CANManager/AP_CANSensor.h>
|
||||
|
||||
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
class ScriptingCANBuffer;
|
||||
class ScriptingCANSensor : public CANSensor {
|
||||
public:
|
||||
|
||||
ScriptingCANSensor():CANSensor("Script") {
|
||||
register_driver(AP_CANManager::Driver_Type::Driver_Type_Scripting);
|
||||
}
|
||||
|
||||
// handler for outgoing frames, using uint32
|
||||
bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us);
|
||||
|
||||
// handler for incoming frames, add to buffers
|
||||
void handle_frame(AP_HAL::CANFrame &frame) override;
|
||||
|
||||
// add a new buffer to this sensor
|
||||
ScriptingCANBuffer* add_buffer(uint32_t buffer_len);
|
||||
|
||||
private:
|
||||
|
||||
HAL_Semaphore sem;
|
||||
|
||||
ScriptingCANBuffer *buffer_list;
|
||||
|
||||
};
|
||||
|
||||
class ScriptingCANBuffer {
|
||||
public:
|
||||
|
||||
ScriptingCANBuffer(ScriptingCANSensor &_sensor, uint32_t buffer_size):sensor(_sensor), buffer(buffer_size) {};
|
||||
|
||||
// Call main sensor write method
|
||||
bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us);
|
||||
|
||||
// read a frame from the buffer
|
||||
bool read_frame(AP_HAL::CANFrame &frame);
|
||||
|
||||
// recursively add frame to buffer
|
||||
void handle_frame(AP_HAL::CANFrame &frame);
|
||||
|
||||
// recursively add new buffer
|
||||
void add_buffer(ScriptingCANBuffer* new_buff);
|
||||
|
||||
private:
|
||||
|
||||
ObjectBuffer<AP_HAL::CANFrame> buffer;
|
||||
|
||||
ScriptingCANSensor &sensor;
|
||||
|
||||
ScriptingCANBuffer *next;
|
||||
|
||||
HAL_Semaphore sem;
|
||||
|
||||
};
|
||||
|
||||
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
@ -318,13 +318,13 @@ static int lua_get_i2c_device(lua_State *L) {
|
|||
|
||||
AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices] = new AP_HAL::OwnPtr<AP_HAL::I2CDevice>;
|
||||
if (AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices] == nullptr) {
|
||||
return luaL_argerror(L, 1, "i2c device nullptr");;
|
||||
return luaL_argerror(L, 1, "i2c device nullptr");
|
||||
}
|
||||
|
||||
*AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices] = std::move(hal.i2c_mgr->get_device(bus, address, bus_clock, use_smbus));
|
||||
|
||||
if (AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices] == nullptr || AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices]->get() == nullptr) {
|
||||
return luaL_argerror(L, 1, "i2c device nullptr");;
|
||||
return luaL_argerror(L, 1, "i2c device nullptr");
|
||||
}
|
||||
|
||||
new_AP_HAL__I2CDevice(L);
|
||||
|
@ -340,6 +340,34 @@ const luaL_Reg i2c_functions[] = {
|
|||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
static int lua_get_CAN_device(lua_State *L) {
|
||||
|
||||
check_arguments(L, 1, "CAN:get_device");
|
||||
|
||||
const uint32_t raw_buffer_len = coerce_to_uint32_t(L, 1);
|
||||
luaL_argcheck(L, ((raw_buffer_len >= 1U) && (raw_buffer_len <= 25U)), 1, "argument out of range");
|
||||
const uint32_t buffer_len = static_cast<uint32_t>(raw_buffer_len);
|
||||
|
||||
if (AP::scripting()->_CAN_dev == nullptr) {
|
||||
AP::scripting()->_CAN_dev = new ScriptingCANSensor();
|
||||
if (AP::scripting()->_CAN_dev == nullptr) {
|
||||
return luaL_argerror(L, 1, "CAN device nullptr");
|
||||
}
|
||||
}
|
||||
|
||||
new_ScriptingCANBuffer(L);
|
||||
*check_ScriptingCANBuffer(L, -1) = AP::scripting()->_CAN_dev->add_buffer(buffer_len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg CAN_functions[] = {
|
||||
{"get_device", lua_get_CAN_device},
|
||||
{NULL, NULL}
|
||||
};
|
||||
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
|
||||
void load_lua_bindings(lua_State *L) {
|
||||
lua_pushstring(L, "logger");
|
||||
luaL_newlib(L, AP_Logger_functions);
|
||||
|
@ -349,6 +377,12 @@ void load_lua_bindings(lua_State *L) {
|
|||
luaL_newlib(L, i2c_functions);
|
||||
lua_settable(L, -3);
|
||||
|
||||
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
||||
lua_pushstring(L, "CAN");
|
||||
luaL_newlib(L, CAN_functions);
|
||||
lua_settable(L, -3);
|
||||
#endif
|
||||
|
||||
luaL_setfuncs(L, global_functions, 0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue