7#include "target_constants.h"
66 filterConf.
extid =
true;
72 if(
port->getSpeedPreset() < 3){
73 port->setSpeedPreset(3);
75 this->
port->takePort();
84 this->
port->freePort();
91 }
else if (address == 1) {
93 }
else if (address == 2) {
115 return static_cast<Encoder*
>(
this);
136 uint16_t dataFlash = 0;
152 this->
posOffset = (float) ((int16_t) dataFlash / 10000.0);
155 bool moreThanHalfTurn = fabs(this->
posOffset) > 0.5 ? 1 : 0;
156 if (moreThanHalfTurn) {
165 uint16_t dataFlash = 0;
183 bool moreThanHalfTurn = fabs(storedOffset) > 0.5 ? 1 : 0;
184 if (moreThanHalfTurn) {
186 storedOffset += (storedOffset > 0) ? -1.0 : 1.0;
189 uint16_t dataFlash = ((int16_t) (storedOffset * 10000) & 0xFFFF);
236 std::vector<CommandReply> &replies) {
250 replies.emplace_back(
port->getSpeedPreset());
252 port->setSpeedPreset(std::max<uint8_t>(3,cmd.
val));
257 replies.emplace_back((uint32_t) this->
state);
261 replies.emplace_back((uint32_t) this->
encRate);
272 replies.emplace_back((int32_t) (
lastTorque * 10000));
287 replies.emplace_back((int32_t) (
posOffset * 10000));
296 replies.emplace_back(
voltage * 1000);
401 int32_t send_index = 0;
417 uint32_t
request = ((uint32_t) 1 << 15);
418 request |= ((uint32_t) 1 << 8);
444 bool moreThanHalfTurn = fabs(delta) > 180.0 ? 1 : 0;
445 if (moreThanHalfTurn) {
447 mtPos += (delta > 0) ? -1.0 : 1.0;
466 port->sendMessage(msg);
483 uint8_t fw_major =
buffer[ind++];
484 uint8_t fw_min =
buffer[ind++];
486 std::string test((
char*)
buffer + ind);
487 ind += test.length() + 1;
489 uint8_t uuid[12] = { 0 };
494 uint8_t isTestFw =
buffer[ind++];
501 bool compatible =
false;
503 compatible = ((fw_major << 8) | fw_min) >= (FW_MIN_RELEASE >> 8);
505 compatible = ((fw_major << 16) | (fw_min << 8) | isTestFw)
521 if (mask & ((uint32_t) 1 << 8)) {
524 if (mask & ((uint32_t) 1 << 15)) {
535 if (mask & ((uint32_t) 1 << 16)) {
572 uint16_t destCanID = msg.
header.
id & 0xFF;
573 uint8_t *rxBuf = msg.
data;
578 bool messageIsForThisVescAxis = destCanID == this->
OFFB_can_Id;
586 if (!messageIsForThisVescAxis)
598 uint32_t rxbuf_ind = (
unsigned int) rxBuf[0] << 8;
599 rxbuf_ind |= rxBuf[1];
600 if (rxbuf_ind < BUFFER_RX_SIZE) {
611 uint32_t rxbuf_length = (uint32_t) rxBuf[index++] << 8;
612 rxbuf_length |= (uint32_t) rxBuf[index++];
614 if (rxbuf_length > BUFFER_RX_SIZE) {
618 uint8_t crc_high = rxBuf[index++];
619 uint8_t crc_low = rxBuf[index++];
623 == ((uint8_t) crc_high << 8 | (uint8_t) crc_low)) {
663 buffer[(*index)++] = number >> 24;
664 buffer[(*index)++] = number >> 16;
665 buffer[(*index)++] = number >> 8;
666 buffer[(*index)++] = number;
671 buffer[(*index)++] = number >> 24;
672 buffer[(*index)++] = number >> 16;
673 buffer[(*index)++] = number >> 8;
674 buffer[(*index)++] = number;
678 uint32_t res = ((uint32_t)
buffer[*index]) << 24
679 | ((uint32_t)
buffer[*index + 1]) << 16
680 | ((uint32_t)
buffer[*index + 2]) << 8
681 | ((uint32_t)
buffer[*index + 3]);
687 int32_t res = ((uint32_t)
buffer[*index]) << 24
688 | ((uint32_t)
buffer[*index + 1]) << 16
689 | ((uint32_t)
buffer[*index + 2]) << 8
690 | ((uint32_t)
buffer[*index + 3]);
696 int16_t res = ((uint16_t)
buffer[*index]) << 8
697 | ((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