2018-10-03 17:37:43 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2019-04-29 04:42:26 -03:00
|
|
|
#include <AP_HAL/HAL.h>
|
2020-02-09 09:40:26 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2018-10-03 17:37:43 -03:00
|
|
|
|
|
|
|
#include "lua_bindings.h"
|
|
|
|
|
2019-04-29 04:42:26 -03:00
|
|
|
#include "lua_boxed_numerics.h"
|
2020-05-10 06:37:10 -03:00
|
|
|
#include <AP_Scripting/lua_generated_bindings.h>
|
2019-03-14 04:38:12 -03:00
|
|
|
|
2021-03-03 03:43:45 -04:00
|
|
|
#include <AP_Scripting/AP_Scripting.h>
|
2021-10-05 19:51:21 -03:00
|
|
|
#include <string.h>
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2019-04-29 04:42:26 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// millis
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_millis(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2019-04-29 04:42:26 -03:00
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = AP_HAL::millis();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:49:40 -03:00
|
|
|
// micros
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_micros(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2020-05-09 20:49:40 -03:00
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = AP_HAL::micros();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_mission_receive(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2021-02-25 21:09:17 -04:00
|
|
|
|
|
|
|
ObjectBuffer<struct AP_Scripting::scripting_mission_cmd> *input = AP::scripting()->mission_data;
|
|
|
|
|
|
|
|
if (input == nullptr) {
|
|
|
|
// no mission items ever received
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AP_Scripting::scripting_mission_cmd cmd;
|
|
|
|
|
|
|
|
if (!input->pop(cmd)) {
|
|
|
|
// no new item
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = cmd.time_ms;
|
|
|
|
|
|
|
|
lua_pushinteger(L, cmd.p1);
|
|
|
|
lua_pushnumber(L, cmd.content_p1);
|
|
|
|
lua_pushnumber(L, cmd.content_p2);
|
|
|
|
lua_pushnumber(L, cmd.content_p3);
|
|
|
|
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:17:40 -03:00
|
|
|
int AP_Logger_Write(lua_State *L) {
|
2020-02-09 09:40:26 -04:00
|
|
|
AP_Logger * AP_logger = AP_Logger::get_singleton();
|
|
|
|
if (AP_logger == nullptr) {
|
|
|
|
return luaL_argerror(L, 1, "logger not supported on this firmware");
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "logger") != NULL) ? 1 : 0;
|
|
|
|
|
2020-02-09 09:40:26 -04:00
|
|
|
// check we have at least 4 arguments passed in
|
2022-09-03 13:01:36 -03:00
|
|
|
const int args = lua_gettop(L) - arg_offset;
|
2020-02-09 09:40:26 -04:00
|
|
|
if (args < 4) {
|
|
|
|
return luaL_argerror(L, args, "too few arguments");
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
const char * name = luaL_checkstring(L, 1 + arg_offset);
|
|
|
|
const char * labels = luaL_checkstring(L, 2 + arg_offset);
|
|
|
|
const char * fmt = luaL_checkstring(L, 3 + arg_offset);
|
2020-02-09 09:40:26 -04:00
|
|
|
|
|
|
|
// cheack the name, labels and format are not too long
|
|
|
|
if (strlen(name) >= LS_NAME_SIZE) {
|
|
|
|
return luaL_error(L, "Name must be 4 or less chars long");
|
|
|
|
}
|
2020-07-25 09:13:36 -03:00
|
|
|
uint8_t length = strlen(labels);
|
2020-02-09 09:40:26 -04:00
|
|
|
if (length >= (LS_LABELS_SIZE - 7)) { // need 7 chars to add 'TimeUS,'
|
|
|
|
return luaL_error(L, "labels must be less than 58 chars long");
|
|
|
|
}
|
|
|
|
// Count the number of commas
|
|
|
|
uint8_t commas = 1;
|
|
|
|
for (uint8_t i=0; i<length; i++) {
|
|
|
|
if (labels[i] == ',') {
|
|
|
|
commas++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length = strlen(fmt);
|
|
|
|
if (length >= (LS_FORMAT_SIZE - 1)) { // need 1 char to add timestamp
|
|
|
|
return luaL_error(L, "format must be less than 15 chars long");
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
// check the number of arguments matches the number of values in the label
|
|
|
|
if (length != commas) {
|
|
|
|
return luaL_argerror(L, args, "label does not match format");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool have_units = false;
|
|
|
|
if (args - 5 == length) {
|
|
|
|
// check if there are enough arguments for units and multiplyers
|
|
|
|
have_units = true;
|
|
|
|
} else if (args - 3 != length) {
|
|
|
|
// check the number of arguments matches the length of the foramt string
|
2020-02-09 09:40:26 -04:00
|
|
|
return luaL_argerror(L, args, "format does not match No. of arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend timestamp to format and labels
|
|
|
|
char label_cat[LS_LABELS_SIZE];
|
|
|
|
strcpy(label_cat,"TimeUS,");
|
|
|
|
strcat(label_cat,labels);
|
|
|
|
char fmt_cat[LS_FORMAT_SIZE];
|
|
|
|
strcpy(fmt_cat,"Q");
|
|
|
|
strcat(fmt_cat,fmt);
|
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
// Need to declare these here so they don't go out of scope
|
|
|
|
char units_cat[LS_FORMAT_SIZE];
|
|
|
|
char multipliers_cat[LS_FORMAT_SIZE];
|
|
|
|
|
|
|
|
uint8_t field_start = 4;
|
|
|
|
struct AP_Logger::log_write_fmt *f;
|
|
|
|
if (!have_units) {
|
|
|
|
// ask for a mesage type
|
|
|
|
f = AP_logger->msg_fmt_for_name(name, label_cat, nullptr, nullptr, fmt_cat, true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// read in units and multiplers strings
|
|
|
|
field_start += 2;
|
2022-09-03 13:01:36 -03:00
|
|
|
const char * units = luaL_checkstring(L, 4 + arg_offset);
|
|
|
|
const char * multipliers = luaL_checkstring(L, 5 + arg_offset);
|
2020-07-25 09:13:36 -03:00
|
|
|
|
|
|
|
if (length != strlen(units)) {
|
|
|
|
return luaL_error(L, "units must be same length as format");
|
|
|
|
}
|
|
|
|
if (length != strlen(multipliers)) {
|
|
|
|
return luaL_error(L, "multipliers must be same length as format");
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend timestamp to units and multiplyers
|
|
|
|
strcpy(units_cat,"s");
|
|
|
|
strcat(units_cat,units);
|
|
|
|
|
|
|
|
strcpy(multipliers_cat,"F");
|
|
|
|
strcat(multipliers_cat,multipliers);
|
|
|
|
|
|
|
|
// ask for a mesage type
|
|
|
|
f = AP_logger->msg_fmt_for_name(name, label_cat, units_cat, multipliers_cat, fmt_cat, true);
|
|
|
|
}
|
|
|
|
|
2020-02-09 09:40:26 -04:00
|
|
|
if (f == nullptr) {
|
|
|
|
// unable to map name to a messagetype; could be out of
|
|
|
|
// msgtypes, could be out of slots, ...
|
|
|
|
return luaL_argerror(L, args, "could not map message type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// work out how long the block will be
|
|
|
|
int16_t msg_len = AP_logger->Write_calc_msg_len(fmt_cat);
|
|
|
|
if (msg_len == -1) {
|
|
|
|
return luaL_argerror(L, args, "unknown format");
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_Buffer buffer;
|
|
|
|
luaL_buffinit(L, &buffer);
|
|
|
|
|
|
|
|
// add logging headers
|
|
|
|
const char header[2] = {(char)HEAD_BYTE1, (char)HEAD_BYTE2};
|
|
|
|
luaL_addlstring(&buffer, header, sizeof(header));
|
|
|
|
luaL_addlstring(&buffer, (char *)&f->msg_type, sizeof(f->msg_type));
|
|
|
|
|
|
|
|
// timestamp is always first value
|
|
|
|
const uint64_t now = AP_HAL::micros64();
|
|
|
|
luaL_addlstring(&buffer, (char *)&now, sizeof(uint64_t));
|
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
for (uint8_t i=field_start; i<=args; i++) {
|
2020-02-09 09:40:26 -04:00
|
|
|
uint8_t charlen = 0;
|
2020-07-25 09:13:36 -03:00
|
|
|
uint8_t index = have_units ? i-5 : i-3;
|
2022-09-03 13:01:36 -03:00
|
|
|
uint8_t arg_index = i + arg_offset;
|
2020-07-25 09:13:36 -03:00
|
|
|
switch(fmt_cat[index]) {
|
2020-02-09 09:40:26 -04:00
|
|
|
// logger varable types not available to scripting
|
|
|
|
// 'b': int8_t
|
|
|
|
// 'h': int16_t
|
|
|
|
// 'c': int16_t
|
|
|
|
// 'd': double
|
|
|
|
// 'H': uint16_t
|
|
|
|
// 'C': uint16_t
|
|
|
|
// 'Q': uint64_t
|
|
|
|
// 'q': int64_t
|
|
|
|
// 'a': arrays
|
|
|
|
case 'i':
|
|
|
|
case 'L':
|
|
|
|
case 'e': {
|
2022-09-03 13:01:36 -03:00
|
|
|
const lua_Integer tmp1 = luaL_checkinteger(L, arg_index);
|
|
|
|
luaL_argcheck(L, ((tmp1 >= INT32_MIN) && (tmp1 <= INT32_MAX)), arg_index, "argument out of range");
|
2020-02-09 09:40:26 -04:00
|
|
|
int32_t tmp = tmp1;
|
|
|
|
luaL_addlstring(&buffer, (char *)&tmp, sizeof(int32_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'f': {
|
2022-09-03 13:01:36 -03:00
|
|
|
float tmp = luaL_checknumber(L, arg_index);
|
|
|
|
luaL_argcheck(L, ((tmp >= -INFINITY) && (tmp <= INFINITY)), arg_index, "argument out of range");
|
2020-02-09 09:40:26 -04:00
|
|
|
luaL_addlstring(&buffer, (char *)&tmp, sizeof(float));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n': {
|
|
|
|
charlen = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'M':
|
|
|
|
case 'B': {
|
2022-09-03 13:01:36 -03:00
|
|
|
const lua_Integer tmp1 = luaL_checkinteger(L, arg_index);
|
|
|
|
luaL_argcheck(L, ((tmp1 >= 0) && (tmp1 <= UINT8_MAX)), arg_index, "argument out of range");
|
2020-02-09 09:40:26 -04:00
|
|
|
uint8_t tmp = static_cast<uint8_t>(tmp1);
|
|
|
|
luaL_addlstring(&buffer, (char *)&tmp, sizeof(uint8_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'I':
|
|
|
|
case 'E': {
|
2022-09-03 13:01:36 -03:00
|
|
|
const uint32_t tmp = coerce_to_uint32_t(L, arg_index);
|
2020-02-09 09:40:26 -04:00
|
|
|
luaL_addlstring(&buffer, (char *)&tmp, sizeof(uint32_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'N': {
|
|
|
|
charlen = 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Z': {
|
|
|
|
charlen = 64;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2022-09-03 13:01:36 -03:00
|
|
|
return luaL_error(L, "%c unsupported format",fmt_cat[arg_index-3]);
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (charlen != 0) {
|
2022-09-03 13:01:36 -03:00
|
|
|
const char *tmp = luaL_checkstring(L, arg_index);
|
2021-10-05 19:51:21 -03:00
|
|
|
const size_t slen = strlen(tmp);
|
|
|
|
if (slen > charlen) {
|
2022-09-03 13:01:36 -03:00
|
|
|
return luaL_error(L, "arg %i too long for %c format",arg_index,fmt_cat[arg_index-3]);
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
2021-10-05 19:51:21 -03:00
|
|
|
char tstr[charlen];
|
|
|
|
memcpy(tstr, tmp, slen);
|
|
|
|
if (slen < charlen) {
|
|
|
|
memset(&tstr[slen], 0, charlen-slen);
|
|
|
|
}
|
|
|
|
luaL_addlstring(&buffer, tstr, charlen);
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_logger->Safe_Write_Emit_FMT(f);
|
|
|
|
|
|
|
|
luaL_pushresult(&buffer);
|
|
|
|
AP_logger->WriteBlock(buffer.b,msg_len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_get_i2c_device(lua_State *L) {
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "i2c") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
const int args = lua_gettop(L) - arg_offset;
|
2021-03-03 03:43:45 -04:00
|
|
|
if (args < 2) {
|
|
|
|
return luaL_argerror(L, args, "require i2c bus and address");
|
|
|
|
}
|
|
|
|
if (args > 4) {
|
|
|
|
return luaL_argerror(L, args, "too many arguments");
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
const lua_Integer bus_in = luaL_checkinteger(L, 1 + arg_offset);
|
|
|
|
luaL_argcheck(L, ((bus_in >= 0) && (bus_in <= 4)), 1 + arg_offset, "bus out of range");
|
2021-03-03 03:43:45 -04:00
|
|
|
const uint8_t bus = static_cast<uint8_t>(bus_in);
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
const lua_Integer address_in = luaL_checkinteger(L, 2 + arg_offset);
|
|
|
|
luaL_argcheck(L, ((address_in >= 0) && (address_in <= 128)), 2 + arg_offset, "address out of range");
|
2021-03-03 03:43:45 -04:00
|
|
|
const uint8_t address = static_cast<uint8_t>(address_in);
|
|
|
|
|
|
|
|
// optional arguments, use the same defaults as the hal get_device function
|
|
|
|
uint32_t bus_clock = 400000;
|
|
|
|
bool use_smbus = false;
|
|
|
|
|
|
|
|
if (args > 2) {
|
2022-09-03 13:01:36 -03:00
|
|
|
bus_clock = coerce_to_uint32_t(L, 3 + arg_offset);
|
2021-03-03 03:43:45 -04:00
|
|
|
|
|
|
|
if (args > 3) {
|
2022-09-03 13:01:36 -03:00
|
|
|
use_smbus = static_cast<bool>(lua_toboolean(L, 4 + arg_offset));
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static_assert(SCRIPTING_MAX_NUM_I2C_DEVICE >= 0, "There cannot be a negative number of I2C devices");
|
|
|
|
if (AP::scripting()->num_i2c_devices >= SCRIPTING_MAX_NUM_I2C_DEVICE) {
|
2022-02-05 08:21:06 -04:00
|
|
|
return luaL_argerror(L, 1, "no i2c devices available");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-07-23 08:15:21 -03:00
|
|
|
return luaL_argerror(L, 1, "i2c device nullptr");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*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) {
|
2021-07-23 08:15:21 -03:00
|
|
|
return luaL_argerror(L, 1, "i2c device nullptr");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
new_AP_HAL__I2CDevice(L);
|
|
|
|
*check_AP_HAL__I2CDevice(L, -1) = AP::scripting()->_i2c_dev[AP::scripting()->num_i2c_devices]->get();
|
|
|
|
|
|
|
|
AP::scripting()->num_i2c_devices++;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:15:21 -03:00
|
|
|
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_get_CAN_device(lua_State *L) {
|
2021-07-23 08:15:21 -03:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "CAN") != NULL) ? 1 : 0;
|
2021-07-23 08:15:21 -03:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
binding_argcheck(L, 1 + arg_offset);
|
|
|
|
|
|
|
|
const uint32_t raw_buffer_len = coerce_to_uint32_t(L, 1 + arg_offset);
|
|
|
|
luaL_argcheck(L, ((raw_buffer_len >= 1U) && (raw_buffer_len <= 25U)), 1 + arg_offset, "argument out of range");
|
2021-07-23 08:15:21 -03:00
|
|
|
const uint32_t buffer_len = static_cast<uint32_t>(raw_buffer_len);
|
|
|
|
|
|
|
|
if (AP::scripting()->_CAN_dev == nullptr) {
|
2022-07-01 23:23:58 -03:00
|
|
|
AP::scripting()->_CAN_dev = new ScriptingCANSensor(AP_CANManager::Driver_Type::Driver_Type_Scripting);
|
2021-07-23 08:15:21 -03:00
|
|
|
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;
|
|
|
|
}
|
2022-07-01 23:23:58 -03:00
|
|
|
|
|
|
|
int lua_get_CAN_device2(lua_State *L) {
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "CAN") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 1 + arg_offset);
|
2022-07-01 23:23:58 -03:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
const uint32_t raw_buffer_len = coerce_to_uint32_t(L, 1 + arg_offset);
|
|
|
|
luaL_argcheck(L, ((raw_buffer_len >= 1U) && (raw_buffer_len <= 25U)), 1 + arg_offset, "argument out of range");
|
2022-07-01 23:23:58 -03:00
|
|
|
const uint32_t buffer_len = static_cast<uint32_t>(raw_buffer_len);
|
|
|
|
|
|
|
|
if (AP::scripting()->_CAN_dev2 == nullptr) {
|
|
|
|
AP::scripting()->_CAN_dev2 = new ScriptingCANSensor(AP_CANManager::Driver_Type::Driver_Type_Scripting2);
|
|
|
|
if (AP::scripting()->_CAN_dev2 == nullptr) {
|
|
|
|
return luaL_argerror(L, 1, "CAN device nullptr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_ScriptingCANBuffer(L);
|
|
|
|
*check_ScriptingCANBuffer(L, -1) = AP::scripting()->_CAN_dev2->add_buffer(buffer_len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2021-07-23 08:15:21 -03:00
|
|
|
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|