7#include "target_constants.h"
66 if(
port->getSpeedPreset() < 3){
67 port->setSpeedPreset(3);
69 this->
port->takePort();
78 this->
port->freePort();
87 }
else if (address == 1) {
89 }
else if (address == 2) {
101 filterConf.
extid =
true;
113 filterConf.
extid =
true;
143 return static_cast<Encoder*
>(
this);
164 uint16_t dataFlash = 0;
180 this->
posOffset = (float) ((int16_t) dataFlash / 10000.0);
183 bool moreThanHalfTurn = fabs(this->
posOffset) > 0.5 ? 1 : 0;
184 if (moreThanHalfTurn) {
193 uint16_t dataFlash = 0;
211 bool moreThanHalfTurn = fabs(storedOffset) > 0.5 ? 1 : 0;
212 if (moreThanHalfTurn) {
214 storedOffset += (storedOffset > 0) ? -1.0 : 1.0;
217 uint16_t dataFlash = ((int16_t) (storedOffset * 10000) & 0xFFFF);
264 std::vector<CommandReply> &replies) {
286 replies.emplace_back(
port->getSpeedPreset());
288 port->setSpeedPreset(std::max<uint8_t>(3,cmd.
val));
293 replies.emplace_back((uint32_t) this->
state);
297 replies.emplace_back((uint32_t) this->
encRate);
308 replies.emplace_back((int32_t) (
lastTorque * 10000));
323 replies.emplace_back((int32_t) (
posOffset * 10000));
332 replies.emplace_back(
voltage * 1000);
437 int32_t send_index = 0;
453 uint32_t
request = ((uint32_t) 1 << 15);
454 request |= ((uint32_t) 1 << 8);
480 bool moreThanHalfTurn = fabs(delta) > 180.0 ? 1 : 0;
481 if (moreThanHalfTurn) {
483 mtPos += (delta > 0) ? -1.0 : 1.0;
502 port->sendMessage(msg);
519 uint8_t fw_major =
buffer[ind++];
520 uint8_t fw_min =
buffer[ind++];
522 std::string test((
char*)
buffer + ind);
523 ind += test.length() + 1;
525 uint8_t uuid[12] = { 0 };
530 uint8_t isTestFw =
buffer[ind++];
537 bool compatible =
false;
539 compatible = ((fw_major << 8) | fw_min) >= (FW_MIN_RELEASE >> 8);
541 compatible = ((fw_major << 16) | (fw_min << 8) | isTestFw)
557 if (mask & ((uint32_t) 1 << 8)) {
560 if (mask & ((uint32_t) 1 << 15)) {
571 if (mask & ((uint32_t) 1 << 16)) {
608 uint16_t destCanID = msg.
header.
id & 0xFF;
609 uint8_t *rxBuf = msg.
data;
614 bool messageIsForThisVescAxis = destCanID == this->
OFFB_can_Id;
622 if (!messageIsForThisVescAxis){
635 uint32_t rxbuf_ind = (
unsigned int) rxBuf[0] << 8;
636 rxbuf_ind |= rxBuf[1];
637 if (rxbuf_ind < BUFFER_RX_SIZE) {
648 uint32_t rxbuf_length = (uint32_t) rxBuf[index++] << 8;
649 rxbuf_length |= (uint32_t) rxBuf[index++];
651 if (rxbuf_length > BUFFER_RX_SIZE) {
655 uint8_t crc_high = rxBuf[index++];
656 uint8_t crc_low = rxBuf[index++];
660 == ((uint8_t) crc_high << 8 | (uint8_t) crc_low)) {
700 buffer[(*index)++] = number >> 24;
701 buffer[(*index)++] = number >> 16;
702 buffer[(*index)++] = number >> 8;
703 buffer[(*index)++] = number;
708 buffer[(*index)++] = number >> 24;
709 buffer[(*index)++] = number >> 16;
710 buffer[(*index)++] = number >> 8;
711 buffer[(*index)++] = number;
715 uint32_t res = ((uint32_t)
buffer[*index]) << 24
716 | ((uint32_t)
buffer[*index + 1]) << 16
717 | ((uint32_t)
buffer[*index + 2]) << 8
718 | ((uint32_t)
buffer[*index + 3]);
724 int32_t res = ((uint32_t)
buffer[*index]) << 24
725 | ((uint32_t)
buffer[*index + 1]) << 16
726 | ((uint32_t)
buffer[*index + 2]) << 8
727 | ((uint32_t)
buffer[*index + 3]);
733 int16_t res = ((uint16_t)
buffer[*index]) << 8
734 | ((uint16_t)
buffer[*index + 1]);
void makeCrcTable(std::array< T, LEN > &table, const T crcpoly, const uint8_t bits, const bool refin=false, const bool refout=false)
uint16_t calculateCrc16_8(std::array< uint16_t, 256 > &crctable, uint8_t *buf, uint16_t len, uint16_t crc=0)
std::array< uint16_t, 256 > VescCAN::crc16_tab __attribute__((section(CCRAM_SEC)))
@ VESC_STATE_INCOMPATIBLE
@ CAN_PACKET_FILL_RX_BUFFER
@ CAN_PACKET_PROCESS_RX_BUFFER
@ CAN_PACKET_SET_CURRENT_REL
@ CAN_PACKET_POLL_ROTOR_POS
@ CAN_PACKET_PROCESS_SHORT_BUFFER
@ CAN_PACKET_FILL_RX_BUFFER_LONG
@ COMM_GET_VALUES_SELECTIVE
void registerCommand(const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
static CommandStatus handleGetSet(const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value)
CommandHandler(const char *clsname, uint16_t clsid, uint8_t instance=0)
virtual EncoderType getEncoderType()
virtual Encoder * getEncoder()
const ClassIdentifier getInfo()
static ClassIdentifier info
static bool isCreatable()
static bool isCreatable()
static ClassIdentifier info
const ClassIdentifier getInfo()
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index)
uint8_t buffer_rx[BUFFER_RX_SIZE]
bool motorReady() override
void decodeEncoderPosition(float newPos)
volatile uint8_t vescErrorFlag
int32_t getPos() override
void buffer_append_int32(uint8_t *buffer, int32_t number, int32_t *index)
void setTorque(float torque)
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index)
uint32_t getCpr() override
void askPositionEncoder()
VescFlashAddrs flashAddrs
void stopMotor() override
volatile uint32_t encCount
volatile uint32_t lastVescResponse
void decode_buffer(uint8_t *buffer, uint8_t len)
void turn(int16_t power) override
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index)
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index)
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index)
float getPos_f() override
void buffer_append_uint32(uint8_t *buffer, uint32_t number, int32_t *index)
void sendMsg(uint8_t cmd, uint8_t *buffer, uint8_t len)
static bool crcTableInitialized
static const uint16_t crcpoly
void saveFlash() override
bool hasIntegratedEncoder() override
static std::array< uint16_t, 256 > crc16_tab
void startMotor() override
void restoreFlash() override
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
void setPos(int32_t pos) override
void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index)
Encoder * getEncoder() override
EncoderType getEncoderType() override
volatile uint32_t encStartPeriod
void canRxPendCallback(CANPort *port, CAN_rx_msg &msg) override
void setAddress(uint8_t address)
void Delay(const TickType_t Delay)
Thread(const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
bool Flash_Write(uint16_t adr, uint16_t dat)
bool Flash_Read(uint16_t adr, uint16_t *buf, bool checkempty=true)
static void * memcpy(void *dst, const void *src, size_t n)
uint8_t data[CAN_MSGBUFSIZE]
uint8_t data[CAN_MSGBUFSIZE]
CFG_TUH_MEM_ALIGN tusb_control_request_t request