mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-12 00:37:34 +08:00
293 lines
7.1 KiB
C++
293 lines
7.1 KiB
C++
/****************************************************************************
|
|
*
|
|
* Copyright (c) 2022 PX4 Development Team. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#include "TmotorAlphaEsc.hpp"
|
|
|
|
#include <termios.h>
|
|
|
|
using namespace time_literals;
|
|
|
|
TmotorAlphaEsc::TmotorAlphaEsc(const char *device) :
|
|
ModuleParams(nullptr),
|
|
ScheduledWorkItem(MODULE_NAME, px4::serial_port_to_wq(device))
|
|
{
|
|
strncpy(_device, device, sizeof(_device) - 1);
|
|
_device[sizeof(_device) - 1] = '\0';
|
|
}
|
|
|
|
TmotorAlphaEsc::~TmotorAlphaEsc()
|
|
{
|
|
perf_free(_cycle_perf);
|
|
}
|
|
|
|
int TmotorAlphaEsc::init()
|
|
{
|
|
// status
|
|
int ret = 0;
|
|
|
|
do { // create a scope to handle exit conditions using break
|
|
|
|
// open fd
|
|
_fd = ::open(_device, O_RDWR | O_NOCTTY);
|
|
|
|
if (_fd < 0) {
|
|
PX4_ERR("Error opening fd");
|
|
return -1;
|
|
}
|
|
|
|
// baudrate 115200, 8 bits, no parity, 1 stop bit
|
|
unsigned speed = B115200;
|
|
termios uart_config{};
|
|
int termios_state{};
|
|
|
|
tcgetattr(_fd, &uart_config);
|
|
|
|
// clear ONLCR flag (which appends a CR for every LF)
|
|
uart_config.c_oflag &= ~ONLCR;
|
|
|
|
// set baud rate
|
|
if ((termios_state = cfsetispeed(&uart_config, speed)) < 0) {
|
|
PX4_ERR("CFG: %d ISPD", termios_state);
|
|
ret = -1;
|
|
break;
|
|
}
|
|
|
|
if ((termios_state = cfsetospeed(&uart_config, speed)) < 0) {
|
|
PX4_ERR("CFG: %d OSPD\n", termios_state);
|
|
ret = -1;
|
|
break;
|
|
}
|
|
|
|
if ((termios_state = tcsetattr(_fd, TCSANOW, &uart_config)) < 0) {
|
|
PX4_ERR("baud %d ATTR", termios_state);
|
|
ret = -1;
|
|
break;
|
|
}
|
|
|
|
uart_config.c_cflag |= (CLOCAL | CREAD); // ignore modem controls
|
|
uart_config.c_cflag &= ~CSIZE;
|
|
uart_config.c_cflag |= CS8; // 8-bit characters
|
|
uart_config.c_cflag &= ~PARENB; // no parity bit
|
|
uart_config.c_cflag &= ~CSTOPB; // only need 1 stop bit
|
|
uart_config.c_cflag &= ~CRTSCTS; // no hardware flowcontrol
|
|
|
|
// setup for non-canonical mode
|
|
uart_config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
|
uart_config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
|
uart_config.c_oflag &= ~OPOST;
|
|
|
|
// fetch bytes as they become available
|
|
uart_config.c_cc[VMIN] = 1;
|
|
uart_config.c_cc[VTIME] = 1;
|
|
|
|
if (_fd < 0) {
|
|
PX4_ERR("FAIL: laser fd");
|
|
ret = -1;
|
|
break;
|
|
}
|
|
} while (0);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int TmotorAlphaEsc::task_spawn(int argc, char *argv[])
|
|
{
|
|
bool error_flag = false;
|
|
|
|
int myoptind = 1;
|
|
int ch;
|
|
const char *myoptarg = nullptr;
|
|
const char *device_name = nullptr;
|
|
|
|
while ((ch = px4_getopt(argc, argv, "d:", &myoptind, &myoptarg)) != EOF) {
|
|
switch (ch) {
|
|
case 'd':
|
|
device_name = myoptarg;
|
|
break;
|
|
|
|
case '?':
|
|
error_flag = true;
|
|
break;
|
|
|
|
default:
|
|
PX4_WARN("unrecognized flag");
|
|
error_flag = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (error_flag) {
|
|
return -1;
|
|
}
|
|
|
|
if (device_name && (access(device_name, R_OK | W_OK) == 0)) {
|
|
TmotorAlphaEsc *instance = new TmotorAlphaEsc(device_name);
|
|
|
|
if (instance == nullptr) {
|
|
PX4_ERR("alloc failed");
|
|
return PX4_ERROR;
|
|
}
|
|
|
|
_object.store(instance);
|
|
_task_id = task_id_is_work_queue;
|
|
|
|
instance->ScheduleOnInterval(_current_update_interval);
|
|
|
|
return PX4_OK;
|
|
|
|
} else {
|
|
if (device_name) {
|
|
PX4_ERR("invalid device (-d) %s", device_name);
|
|
|
|
} else {
|
|
PX4_INFO("valid device required");
|
|
}
|
|
}
|
|
|
|
return PX4_ERROR;
|
|
}
|
|
|
|
void TmotorAlphaEsc::Run()
|
|
{
|
|
if (should_exit()) {
|
|
exit_and_cleanup();
|
|
return;
|
|
}
|
|
|
|
if (!_initialized) {
|
|
if (init() == PX4_OK) {
|
|
_initialized = true;
|
|
|
|
} else {
|
|
PX4_ERR("init failed");
|
|
exit_and_cleanup();
|
|
}
|
|
|
|
} else {
|
|
|
|
perf_begin(_cycle_perf);
|
|
|
|
// Check if parameters have changed
|
|
if (_parameter_update_sub.updated()) {
|
|
// clear update
|
|
parameter_update_s param_update;
|
|
_parameter_update_sub.copy(¶m_update);
|
|
|
|
updateParams();
|
|
}
|
|
|
|
uint8_t buffer[120];
|
|
|
|
int newBytes = ::read(_fd, &buffer[0], sizeof(buffer));
|
|
|
|
if (newBytes > 0) {
|
|
_bytes_rx += newBytes;
|
|
}
|
|
|
|
|
|
|
|
// TODO:
|
|
esc_status_s esc_status{};
|
|
|
|
|
|
// self.motor_ix = data[0]
|
|
// self.channel_bag_number = int.from_bytes(data[1:3], 'big')
|
|
// self.rx_throttle = int.from_bytes(data[3:5], 'big')*(100/1024)
|
|
// self.actual_throttle = int.from_bytes(data[5:7], 'big')*(100/1024)
|
|
// self.electric_rpm = int(int.from_bytes(data[7:9], 'big')*(10/pole_pairs))
|
|
// self.bus_voltage = int.from_bytes(data[9:11], 'big')/10
|
|
// self.bus_current = round(c_int16(int.from_bytes(data[11:13], 'big')).value/64, 1)
|
|
// self.phase_current = round(c_int16(int.from_bytes(data[13:15], 'big')).value/64, 1)
|
|
// self.mos_temperature = get_temp(data[15])
|
|
// self.cap_temperature = get_temp(data[16])
|
|
// self.status_byte = data[17:]
|
|
// # status_bits = "{:08b}".format(int(status_byte.hex(),16))
|
|
|
|
|
|
esc_status.timestamp = hrt_absolute_time();
|
|
_esc_status_pub.publish(esc_status);
|
|
|
|
|
|
|
|
perf_end(_cycle_perf);
|
|
|
|
ScheduleDelayed(100_ms);
|
|
}
|
|
}
|
|
|
|
int TmotorAlphaEsc::custom_command(int argc, char *argv[])
|
|
{
|
|
return print_usage("unknown command");
|
|
}
|
|
|
|
int TmotorAlphaEsc::print_status()
|
|
{
|
|
PX4_INFO("Max update rate: %u Hz", 1000000 / _current_update_interval);
|
|
|
|
if (_device[0] != '\0') {
|
|
PX4_INFO("UART device: %s", _device);
|
|
PX4_INFO("UART RX bytes: %" PRIu32, _bytes_rx);
|
|
}
|
|
|
|
perf_print_counter(_cycle_perf);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
TmotorAlphaEsc::print_usage(const char *reason)
|
|
{
|
|
if (reason) {
|
|
PX4_WARN("%s\n", reason);
|
|
}
|
|
|
|
PRINT_MODULE_DESCRIPTION(
|
|
R"DESCR_STR(
|
|
### Description
|
|
|
|
)DESCR_STR");
|
|
|
|
PRINT_MODULE_USAGE_NAME("tmotor_alpha_esc", "driver");
|
|
PRINT_MODULE_USAGE_COMMAND("start");
|
|
PRINT_MODULE_USAGE_PARAM_STRING('d', "/dev/ttyS3", "<file:dev>", "device", true);
|
|
|
|
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
|
|
|
return 0;
|
|
}
|
|
|
|
extern "C" __EXPORT int tmotor_alpha_esc_main(int argc, char *argv[])
|
|
{
|
|
return TmotorAlphaEsc::main(argc, argv);
|
|
}
|