123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include <AP_HAL/AP_HAL.h>
- #include "AP_RangeFinder_Wasp.h"
- #include <AP_SerialManager/AP_SerialManager.h>
- #include <ctype.h>
- extern const AP_HAL::HAL& hal;
- const AP_Param::GroupInfo AP_RangeFinder_Wasp::var_info[] = {
-
-
-
-
-
- AP_GROUPINFO("WSP_MAVG", 1, AP_RangeFinder_Wasp, mavg, 4),
-
-
-
-
-
- AP_GROUPINFO("WSP_MEDF", 2, AP_RangeFinder_Wasp, medf, 4),
-
-
-
-
-
- AP_GROUPINFO("WSP_FRQ", 3, AP_RangeFinder_Wasp, frq, 20),
-
-
-
-
-
- AP_GROUPINFO("WSP_AVG", 4, AP_RangeFinder_Wasp, avg, 2),
-
-
-
-
-
- AP_GROUPINFO("WSP_THR", 5, AP_RangeFinder_Wasp, thr, -1),
-
-
-
-
-
- AP_GROUPINFO("WSP_BAUD", 6, AP_RangeFinder_Wasp, baud, 0),
- AP_GROUPEND
- };
- AP_RangeFinder_Wasp::AP_RangeFinder_Wasp(RangeFinder::RangeFinder_State &_state,
- AP_RangeFinder_Params &_params,
- uint8_t serial_instance) :
- AP_RangeFinder_Backend(_state, _params) {
- AP_Param::setup_object_defaults(this, var_info);
- const AP_SerialManager &serial_manager = AP::serialmanager();
- uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance);
- if (uart != nullptr) {
- uart->begin(115200);
-
- state.var_info = var_info;
- configuration_state = WASP_CFG_PROTOCOL;
- }
- }
- bool AP_RangeFinder_Wasp::detect(uint8_t serial_instance) {
- return AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance) != nullptr;
- }
- bool AP_RangeFinder_Wasp::get_reading(uint16_t &reading_cm) {
- if (uart == nullptr) {
- return false;
- }
-
- float sum = 0;
- uint16_t count = 0;
- int16_t nbytes = uart->available();
- while (nbytes-- > 0) {
- char c = uart->read();
- if (c == '\n') {
- linebuf[linebuf_len] = 0;
- linebuf_len = 0;
- state.last_reading_ms = AP_HAL::millis();
- if (isalpha(linebuf[0])) {
- parse_response();
- } else {
- float read_value = (float)atof(linebuf);
- if (read_value > 0) {
- sum += read_value;
- count++;
- }
- }
- } else if (isalnum(c) || c == '.' || c == '-') {
- linebuf[linebuf_len++] = c;
- }
-
- if (linebuf_len == sizeof(linebuf)) {
- linebuf_len = 0;
- }
- }
- if (configuration_state == WASP_CFG_RATE && uart->tx_pending() == 0) {
- configuration_state = WASP_CFG_ENCODING;
- }
- if (count == 0) {
- return false;
- }
- reading_cm = 100 * sum / count;
- set_status(RangeFinder::RangeFinder_Good);
- return true;
- }
- #define COMMAND_BUFFER_LEN 15
- void AP_RangeFinder_Wasp::update(void) {
- if (!get_reading(state.distance_cm)) {
- set_status(RangeFinder::RangeFinder_NoData);
- }
- if (AP_HAL::millis() - state.last_reading_ms > 500) {
-
- configuration_state = WASP_CFG_RATE;
- }
- char command[COMMAND_BUFFER_LEN] = {};
- switch (configuration_state) {
- case WASP_CFG_RATE:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">BAUD %s\n", baud > 0 ? "HIGH" : "LOW");
- break;
- case WASP_CFG_ENCODING:
- uart->end();
- uart->begin(baud > 0 ? 921600 : 115200);
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">LBE LITTLE\n");
- break;
- case WASP_CFG_PROTOCOL:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">FMT ASCII\n");
- break;
- case WASP_CFG_FRQ:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">FRQ %d\n", constrain_int16(frq, 20, baud > 0 ? 10000 : 1440));
- break;
- case WASP_CFG_GO:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">GO\n");
- break;
- case WASP_CFG_AUT:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">AUT %d\n", thr >= 0 ? 0 : 1);
- break;
- case WASP_CFG_THR:
- if (thr >= 0) {
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">THR %d\n", constrain_int16(thr, 0,255));
- } else {
- configuration_state = WASP_CFG_MAVG;
- }
- break;
- case WASP_CFG_MAVG:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">MAVG %d\n", constrain_int16(mavg, 0, 255));
- break;
- case WASP_CFG_MEDF:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">MEDF %d\n", constrain_int16(medf, 0, 255));
- break;
- case WASP_CFG_AVG:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">AVG %d\n", constrain_int16(avg, 0, 255));
- break;
- case WASP_CFG_AUV:
- hal.util->snprintf(command, COMMAND_BUFFER_LEN, ">AUV 1\n");
- break;
- case WASP_CFG_DONE:
- return;
- }
- if (command[0] != 0) {
- uart->write((uint8_t *)command, strlen(command));
- }
- }
- void AP_RangeFinder_Wasp::parse_response(void) {
- switch (configuration_state) {
- case WASP_CFG_RATE:
- configuration_state = WASP_CFG_ENCODING;
- break;
- case WASP_CFG_ENCODING:
- if (strncmp(linebuf, "LBE", 3) == 0) {
- configuration_state = WASP_CFG_PROTOCOL;
- }
- break;
- case WASP_CFG_PROTOCOL:
- if (strncmp(linebuf, "FMT", 3) == 0) {
- configuration_state = WASP_CFG_FRQ;
- }
- break;
- case WASP_CFG_FRQ:
- if (strncmp(linebuf, "FRQ", 3) == 0) {
- configuration_state = WASP_CFG_GO;
- }
- break;
- case WASP_CFG_GO:
- if (strncmp(linebuf, "GO", 2) == 0) {
- configuration_state = WASP_CFG_AUT;
- }
- break;
- case WASP_CFG_AUT:
- if (strncmp(linebuf, "AUT", 3) == 0) {
- configuration_state = WASP_CFG_THR;
- }
- break;
- case WASP_CFG_THR:
- if (strncmp(linebuf, "THR", 3) == 0) {
- configuration_state = WASP_CFG_MAVG;
- }
- break;
- case WASP_CFG_MAVG:
- if (strncmp(linebuf, "MAVG", 4) == 0) {
- configuration_state = WASP_CFG_MEDF;
- }
- break;
- case WASP_CFG_MEDF:
- if (strncmp(linebuf, "MEDF", 4) == 0) {
- configuration_state = WASP_CFG_AVG;
- }
- break;
- case WASP_CFG_AVG:
- if (strncmp(linebuf, "AVG", 3) == 0) {
- configuration_state = WASP_CFG_AUV;
- }
- break;
- case WASP_CFG_AUV:
- if (strncmp(linebuf, "AUV", 3) == 0) {
- configuration_state = WASP_CFG_DONE;
- }
- break;
- case WASP_CFG_DONE:
- return;
- }
- }
|