Open FFBoard
Open source force feedback firmware
VescCAN Class Reference

#include <VescCAN.h>

Inheritance diagram for VescCAN:
MotorDriver PersistentStorage Encoder CanHandler CommandHandler cpp_freertos::Thread ChoosableClass ChoosableClass VESC_1 VESC_2

Public Member Functions

 VescCAN (uint8_t address)
 
virtual ~VescCAN ()
 
void setAddress (uint8_t address)
 
void turn (int16_t power) override
 
void stopMotor () override
 
void startMotor () override
 
EncodergetEncoder () override
 
bool hasIntegratedEncoder () override
 
EncoderType getEncoderType () override
 
bool motorReady () override
 
void saveFlash () override
 
void restoreFlash () override
 
float getPos_f () override
 
uint32_t getCpr () override
 
int32_t getPos () override
 
void setPos (int32_t pos) override
 
void canRxPendCallback (CANPort *port, CAN_rx_msg &msg) override
 
CommandStatus command (const ParsedCommand &cmd, std::vector< CommandReply > &replies)
 
void registerCommands ()
 
virtual std::string getHelpstring ()
 
void Run ()
 
- Public Member Functions inherited from MotorDriver
 MotorDriver ()
 
virtual ~MotorDriver ()
 
const ClassIdentifier getInfo ()
 
const ClassType getClassType () override
 returns the used classchooser selection id of this instance More...
 
virtual void emergencyStop (bool reset=false)
 
virtual void setEncoder (std::shared_ptr< Encoder > &encoder)
 
- Public Member Functions inherited from ChoosableClass
virtual ~ChoosableClass ()
 
uint16_t getSelectionID ()
 
- Public Member Functions inherited from PersistentStorage
 PersistentStorage ()
 
virtual ~PersistentStorage ()
 
void restoreFlashDelayed ()
 
- Public Member Functions inherited from Encoder
 Encoder ()
 
virtual ~Encoder ()
 
const ClassIdentifier getInfo ()
 
virtual int32_t getPosAbs ()
 
virtual float getPosAbs_f ()
 
virtual const ClassType getClassType () override
 returns the used classchooser selection id of this instance More...
 
- Public Member Functions inherited from CanHandler
 CanHandler ()
 
virtual ~CanHandler ()
 
virtual void canErrorCallback (CANPort *port, uint32_t errcode)
 
virtual void canRxFullCallback (CANPort *port, uint32_t fifo)
 
virtual void canTxCpltCallback (CANPort *port, uint32_t mailbox)
 
virtual void canTxAbortCallback (CANPort *port, uint32_t mailbox)
 
- Public Member Functions inherited from CommandHandler
virtual const ClassType getClassType ()
 
 CommandHandler (const char *clsname, uint16_t clsid, uint8_t instance=0)
 
virtual ~CommandHandler ()
 
virtual bool hasCommands ()
 
virtual void setCommandsEnabled (bool enable)
 
void registerCommands ()
 
virtual CommandStatus internalCommand (const ParsedCommand &cmd, std::vector< CommandReply > &replies)
 
virtual const ClassIdentifier getInfo ()=0
 Command handlers always have class infos. Works well with ChoosableClass. More...
 
virtual std::string getCommandsHelpstring ()
 
virtual std::string getCsvHelpstring ()
 
virtual uint8_t getCommandHandlerInstance ()
 
void broadcastCommandReply (CommandReply reply, uint32_t cmdId, CMDtype type)
 
void sendCommandReplyAsync (CommandReply reply, uint32_t cmdId, CMDtype type, CommandInterface *interface=nullptr)
 
virtual uint16_t getCommandHandlerID ()
 
virtual CmdHandlerInfogetCommandHandlerInfo ()
 
virtual bool isValidCommandId (uint32_t cmdid, uint32_t ignoredFlags=0, uint32_t requiredFlag=0)
 
virtual CmdHandlerCommanddefgetCommandFromName (const std::string &cmd, uint32_t ignoredFlags=0)
 
virtual CmdHandlerCommanddefgetCommandFromId (const uint32_t id, uint32_t ignoredFlags=0)
 
template<typename ID >
void registerCommand (const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
 

Private Member Functions

void saveFlashOffset ()
 
void getFirmwareInfo ()
 
bool isFirmwareCompatible ()
 
void sendPing ()
 
void setTorque (float torque)
 
void decodeEncoderPosition (float newPos)
 
void askGetValue ()
 
void askPositionEncoder ()
 
void sendMsg (uint8_t cmd, uint8_t *buffer, uint8_t len)
 
void decode_buffer (uint8_t *buffer, uint8_t len)
 
void buffer_append_float32 (uint8_t *buffer, float number, float scale, int32_t *index)
 
void buffer_append_int32 (uint8_t *buffer, int32_t number, int32_t *index)
 
void buffer_append_uint32 (uint8_t *buffer, uint32_t number, int32_t *index)
 
uint32_t buffer_get_uint32 (const uint8_t *buffer, int32_t *index)
 
int32_t buffer_get_int32 (const uint8_t *buffer, int32_t *index)
 
int16_t buffer_get_int16 (const uint8_t *buffer, int32_t *index)
 
float buffer_get_float16 (const uint8_t *buffer, float scale, int32_t *index)
 
float buffer_get_float32 (const uint8_t *buffer, float scale, int32_t *index)
 
- Private Member Functions inherited from cpp_freertos::Thread
 Thread (const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
 
 Thread (const char *Name, uint16_t StackDepth, UBaseType_t Priority)
 
 Thread (uint16_t StackDepth, UBaseType_t Priority)
 
bool Start ()
 
virtual ~Thread ()
 
TaskHandle_t GetHandle ()
 
void Suspend ()
 
void Resume ()
 
void ResumeFromISR ()
 
void Notify ()
 
void NotifyFromISR ()
 
uint32_t WaitForNotification (TickType_t Timeout=portMAX_DELAY)
 
UBaseType_t GetPriority ()
 
UBaseType_t GetPriorityFromISR ()
 
void SetPriority (UBaseType_t NewPriority)
 
std::string GetName ()
 
char * GetName ()
 
virtual void Cleanup ()
 
void Delay (const TickType_t Delay)
 
void DelayUntil (const TickType_t Period)
 
void ResetDelayUntil ()
 
bool Wait (ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY)
 

Private Attributes

VescFlashAddrs flashAddrs
 
volatile VescState state = VescState::VESC_STATE_UNKNOWN
 
volatile uint8_t vescErrorFlag
 
bool activeMotor = false
 
float lastTorque
 
float voltage
 
bool useEncoder = true
 
float lastPos = 0
 
float mtPos = 0
 
float prevPos360 = 0
 
float posOffset = 0
 
volatile uint32_t encCount = 0
 
volatile uint32_t encStartPeriod = 0
 
volatile float encRate = 0
 
volatile uint32_t lastVescResponse = 0
 
CANPortport = &canport
 
int32_t filterId = 0
 
uint8_t OFFB_can_Id = 0x40
 
uint8_t VESC_can_Id = 0xFF
 
uint8_t buffer_rx [BUFFER_RX_SIZE]
 

Static Private Attributes

static std::array< uint16_t, 256 > crc16_tab
 
static const uint16_t crcpoly = 0x1021
 
static bool crcTableInitialized = false
 

Additional Inherited Members

- Static Public Member Functions inherited from ChoosableClass
static bool isCreatable ()
 
- Static Public Member Functions inherited from PersistentStorage
static std::vector< PersistentStorage * > & getFlashHandlers ()
 
static void restoreFlashStartupCb ()
 
- Static Public Member Functions inherited from CanHandler
static std::vector< CanHandler * > & getCANHandlers ()
 
- Static Public Member Functions inherited from CommandHandler
static void logSerial (std::string string)
 Send a log formatted sequence. More...
 
static void logSerialDebug (std::string string)
 Send a log formatted sequence if debug is on. More...
 
static bool logsEnabled ()
 
static void setLogsEnabled (bool enabled)
 
static uint32_t getClassIdFromName (const char *name)
 
static const char * getClassNameFromId (const uint32_t id)
 
static CommandHandlergetHandlerFromHandlerId (const uint16_t cmdhandlerID)
 
static CommandHandlergetHandlerFromId (const uint16_t id, const uint8_t instance=0xFF)
 
static CommandHandlergetHandlerFromClassName (const char *name, const uint8_t instance=0xFF)
 
static std::vector< CommandHandler * > getHandlersFromClassName (const char *name)
 
static std::vector< CommandHandler * > getHandlersFromId (const uint16_t id)
 
static bool isInHandlerList (CommandHandler *handler)
 
static std::string getAllHelpstrings ()
 
static std::vector< CommandHandler * > & getCommandHandlers ()
 
template<typename TVal >
static CommandStatus handleGetSet (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value)
 
template<typename TVal , class cls , class cls1 >
static CommandStatus handleGetSetFunc (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value, void(cls1::*setfunc)(TVal), cls *obj)
 
template<typename TVal , class cls , class cls1 , class cls2 >
static CommandStatus handleGetFuncSetFunc (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal(cls1::*getfunc)(), void(cls2::*setfunc)(TVal), cls *obj)
 
template<typename TVal , class cls , class cls1 >
static CommandStatus handleGetFuncSet (const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value, TVal(cls1::*getfunc)(), cls *obj)
 
- Static Public Attributes inherited from MotorDriver
static ClassIdentifier info ={.name = "None" , .id=CLSID_MOT_NONE, .visibility = ClassVisibility::visible}
 
static const std::vector< class_entry< MotorDriver > > all_drivers
 
- Static Public Attributes inherited from ChoosableClass
static ClassIdentifier info
 
- Static Public Attributes inherited from Encoder
static ClassIdentifier info ={.name = "None" , .id=CLSID_ENCODER_NONE, .visibility = ClassVisibility::visible}
 
static const std::vector< class_entry< Encoder > > all_encoders
 
- Static Public Attributes inherited from CommandHandler
static bool logEnabled = true
 
- Protected Member Functions inherited from CommandHandler
void setInstance (uint8_t instance)
 
virtual void addCommandHandler ()
 
virtual void removeCommandHandler ()
 
- Static Protected Member Functions inherited from CommandHandler
static std::vector< uint16_t > & getCommandHandlerIds ()
 
- Protected Attributes inherited from MotorDriver
std::shared_ptr< EncoderdrvEncoder = std::make_shared<Encoder>()
 
- Protected Attributes inherited from ChoosableClass
uint16_t selectionId
 Should only be written by ClassChooser during creation. More...
 
- Protected Attributes inherited from PersistentStorage
bool restoreDelayedFlag = false
 
- Protected Attributes inherited from Encoder
uint32_t cpr = 0
 
- Protected Attributes inherited from CommandHandler
bool commandsEnabled = true
 
std::vector< CmdHandlerCommanddefregisteredCommands
 
CmdHandlerInfo cmdHandlerInfo
 
- Static Protected Attributes inherited from PersistentStorage
static bool startupComplete = false
 
- Static Private Member Functions inherited from cpp_freertos::Thread
static void Yield ()
 
static void StartScheduler ()
 
static void EndScheduler ()
 

Detailed Description

Definition at line 67 of file VescCAN.h.

Constructor & Destructor Documentation

◆ VescCAN()

VescCAN::VescCAN ( uint8_t  address)

Definition at line 52 of file VescCAN.cpp.

◆ ~VescCAN()

VescCAN::~VescCAN ( )
virtual

Definition at line 82 of file VescCAN.cpp.

Member Function Documentation

◆ askGetValue()

void VescCAN::askGetValue ( )
private

Definition at line 412 of file VescCAN.cpp.

◆ askPositionEncoder()

void VescCAN::askPositionEncoder ( )
private

Ask a position with the new system Call the new short response command with a 2 messages communications

Definition at line 431 of file VescCAN.cpp.

◆ buffer_append_float32()

void VescCAN::buffer_append_float32 ( uint8_t *  buffer,
float  number,
float  scale,
int32_t *  index 
)
private

Definition at line 656 of file VescCAN.cpp.

◆ buffer_append_int32()

void VescCAN::buffer_append_int32 ( uint8_t *  buffer,
int32_t  number,
int32_t *  index 
)
private

Definition at line 661 of file VescCAN.cpp.

◆ buffer_append_uint32()

void VescCAN::buffer_append_uint32 ( uint8_t *  buffer,
uint32_t  number,
int32_t *  index 
)
private

Definition at line 669 of file VescCAN.cpp.

◆ buffer_get_float16()

float VescCAN::buffer_get_float16 ( const uint8_t *  buffer,
float  scale,
int32_t *  index 
)
private

Definition at line 707 of file VescCAN.cpp.

◆ buffer_get_float32()

float VescCAN::buffer_get_float32 ( const uint8_t *  buffer,
float  scale,
int32_t *  index 
)
private

Definition at line 702 of file VescCAN.cpp.

◆ buffer_get_int16()

int16_t VescCAN::buffer_get_int16 ( const uint8_t *  buffer,
int32_t *  index 
)
private

Definition at line 695 of file VescCAN.cpp.

◆ buffer_get_int32()

int32_t VescCAN::buffer_get_int32 ( const uint8_t *  buffer,
int32_t *  index 
)
private

Definition at line 686 of file VescCAN.cpp.

◆ buffer_get_uint32()

uint32_t VescCAN::buffer_get_uint32 ( const uint8_t *  buffer,
int32_t *  index 
)
private

Definition at line 677 of file VescCAN.cpp.

◆ canRxPendCallback()

void VescCAN::canRxPendCallback ( CANPort port,
CAN_rx_msg msg 
)
overridevirtual

New message received in fifo number

Reimplemented from CanHandler.

Definition at line 566 of file VescCAN.cpp.

◆ command()

CommandStatus VescCAN::command ( const ParsedCommand cmd,
std::vector< CommandReply > &  replies 
)
virtual
Parameters
[in]cmdThe parsed command to be executed.
[out]repliesA vector to return one or multiple reply objects into. Replies to the interface will be generated based on the reply objects. A string reply may not contain start, end and separation markers: [,],| Other characters are allowed.

Reimplemented from CommandHandler.

Definition at line 235 of file VescCAN.cpp.

◆ decode_buffer()

void VescCAN::decode_buffer ( uint8_t *  buffer,
uint8_t  len 
)
private

Definition at line 469 of file VescCAN.cpp.

◆ decodeEncoderPosition()

void VescCAN::decodeEncoderPosition ( float  newPos)
private

Definition at line 441 of file VescCAN.cpp.

◆ getCpr()

uint32_t VescCAN::getCpr ( )
overridevirtual

Gets the amount of counts per full rotation of the encoder

Reimplemented from Encoder.

Definition at line 215 of file VescCAN.cpp.

◆ getEncoder()

Encoder * VescCAN::getEncoder ( )
overridevirtual

Returns the encoder of this motor driver. Either the integrated encoder or an external encoder assigned to this motor driver passed externally

Reimplemented from MotorDriver.

Definition at line 113 of file VescCAN.cpp.

◆ getEncoderType()

EncoderType VescCAN::getEncoderType ( )
overridevirtual

Returns the type of the encoder. Must override this and NOT return NONE in other classes

Reimplemented from Encoder.

Definition at line 124 of file VescCAN.cpp.

◆ getFirmwareInfo()

void VescCAN::getFirmwareInfo ( )
private

Get the firmware vesc information

Definition at line 369 of file VescCAN.cpp.

◆ getHelpstring()

virtual std::string VescCAN::getHelpstring ( )
inlinevirtual

Returns a description of this class

Reimplemented from CommandHandler.

Definition at line 103 of file VescCAN.h.

◆ getPos()

int32_t VescCAN::getPos ( )
overridevirtual

Returns the encoder position as raw counts

Reimplemented from Encoder.

Definition at line 211 of file VescCAN.cpp.

◆ getPos_f()

float VescCAN::getPos_f ( )
overridevirtual

Returns a float position in rotations

Reimplemented from Encoder.

Definition at line 204 of file VescCAN.cpp.

◆ hasIntegratedEncoder()

bool VescCAN::hasIntegratedEncoder ( )
overridevirtual

If returned true it signals that this motor driver contains its own encoder and does not require an external encoder

Reimplemented from MotorDriver.

Definition at line 120 of file VescCAN.cpp.

◆ isFirmwareCompatible()

bool VescCAN::isFirmwareCompatible ( )
private

◆ motorReady()

bool VescCAN::motorReady ( )
overridevirtual

Reimplemented from MotorDriver.

Definition at line 131 of file VescCAN.cpp.

◆ registerCommands()

void VescCAN::registerCommands ( )

Definition at line 219 of file VescCAN.cpp.

◆ restoreFlash()

void VescCAN::restoreFlash ( )
overridevirtual

Should be implemented to restore settings. This is not automatically called and should be called when appropriate.

Reimplemented from PersistentStorage.

Definition at line 135 of file VescCAN.cpp.

◆ Run()

void VescCAN::Run ( )
virtual

Implementation of your actual thread code. You must override this function.

Note
If INCLUDE_vTaskDelete is defined, then you may return from your Run method. This will cause the task to be deleted from FreeRTOS, however you are still responsible to delete the task object. If this is not defined, then retuning from your Run() method will result in an assert.

Implements cpp_freertos::Thread.

Definition at line 317 of file VescCAN.cpp.

◆ saveFlash()

void VescCAN::saveFlash ( )
overridevirtual

Called when the user uses the "save" command and presses the save button in the configurator Automatically called by the command parser thread for every class that inherits from PersistentStorage

Reimplemented from PersistentStorage.

Definition at line 164 of file VescCAN.cpp.

◆ saveFlashOffset()

void VescCAN::saveFlashOffset ( )
private

Definition at line 179 of file VescCAN.cpp.

◆ sendMsg()

void VescCAN::sendMsg ( uint8_t  cmd,
uint8_t *  buffer,
uint8_t  len 
)
private

send the message to the CAN with an Extended Can message

Definition at line 459 of file VescCAN.cpp.

◆ sendPing()

void VescCAN::sendPing ( )
private

Send a ping command to the vesc to check if it's here.

Definition at line 382 of file VescCAN.cpp.

◆ setAddress()

void VescCAN::setAddress ( uint8_t  address)

Definition at line 88 of file VescCAN.cpp.

◆ setPos()

void VescCAN::setPos ( int32_t  pos)
overridevirtual

Must be in encoder cpr if not just used to zero the axis

Reimplemented from Encoder.

Definition at line 197 of file VescCAN.cpp.

◆ setTorque()

void VescCAN::setTorque ( float  torque)
private

Send a torque command to the vesc : value is [-1,1] range relative to max torque settings in the vesc fw.

Definition at line 394 of file VescCAN.cpp.

◆ startMotor()

void VescCAN::startMotor ( )
overridevirtual

Enable the motor driver

Reimplemented from MotorDriver.

Definition at line 109 of file VescCAN.cpp.

◆ stopMotor()

void VescCAN::stopMotor ( )
overridevirtual

Disable the motor driver

Reimplemented from MotorDriver.

Definition at line 104 of file VescCAN.cpp.

◆ turn()

void VescCAN::turn ( int16_t  val)
overridevirtual

Turn the motor with positive/negative power. Range should be full signed 16 bit A value of 0 should have no torque. The sign is the direction.

Reimplemented from MotorDriver.

Definition at line 98 of file VescCAN.cpp.

Member Data Documentation

◆ activeMotor

bool VescCAN::activeMotor = false
private

Definition at line 116 of file VescCAN.h.

◆ buffer_rx

uint8_t VescCAN::buffer_rx[BUFFER_RX_SIZE]
private

Definition at line 140 of file VescCAN.h.

◆ crc16_tab

std::array<uint16_t,256> VescCAN::crc16_tab
staticprivate

Definition at line 162 of file VescCAN.h.

◆ crcpoly

const uint16_t VescCAN::crcpoly = 0x1021
staticprivate

Definition at line 163 of file VescCAN.h.

◆ crcTableInitialized

bool VescCAN::crcTableInitialized = false
staticprivate

Definition at line 164 of file VescCAN.h.

◆ encCount

volatile uint32_t VescCAN::encCount = 0
private

Definition at line 127 of file VescCAN.h.

◆ encRate

volatile float VescCAN::encRate = 0
private

Definition at line 129 of file VescCAN.h.

◆ encStartPeriod

volatile uint32_t VescCAN::encStartPeriod = 0
private

Definition at line 128 of file VescCAN.h.

◆ filterId

int32_t VescCAN::filterId = 0
private

Definition at line 137 of file VescCAN.h.

◆ flashAddrs

VescFlashAddrs VescCAN::flashAddrs
private

Definition at line 113 of file VescCAN.h.

◆ lastPos

float VescCAN::lastPos = 0
private

Definition at line 123 of file VescCAN.h.

◆ lastTorque

float VescCAN::lastTorque
private

Definition at line 117 of file VescCAN.h.

◆ lastVescResponse

volatile uint32_t VescCAN::lastVescResponse = 0
private

Definition at line 130 of file VescCAN.h.

◆ mtPos

float VescCAN::mtPos = 0
private

Definition at line 124 of file VescCAN.h.

◆ OFFB_can_Id

uint8_t VescCAN::OFFB_can_Id = 0x40
private

Definition at line 138 of file VescCAN.h.

◆ port

CANPort* VescCAN::port = &canport
private

Definition at line 136 of file VescCAN.h.

◆ posOffset

float VescCAN::posOffset = 0
private

Definition at line 126 of file VescCAN.h.

◆ prevPos360

float VescCAN::prevPos360 = 0
private

Definition at line 125 of file VescCAN.h.

◆ state

volatile VescState VescCAN::state = VescState::VESC_STATE_UNKNOWN
private

Definition at line 114 of file VescCAN.h.

◆ useEncoder

bool VescCAN::useEncoder = true
private

Definition at line 122 of file VescCAN.h.

◆ VESC_can_Id

uint8_t VescCAN::VESC_can_Id = 0xFF
private

Definition at line 139 of file VescCAN.h.

◆ vescErrorFlag

volatile uint8_t VescCAN::vescErrorFlag
private

Definition at line 115 of file VescCAN.h.

◆ voltage

float VescCAN::voltage
private

Definition at line 118 of file VescCAN.h.


The documentation for this class was generated from the following files: