Open FFBoard
Open source force feedback firmware
MotorPWM Class Reference

#include <MotorPWM.h>

Inheritance diagram for MotorPWM:
MotorDriver CommandHandler PersistentStorage ChoosableClass

Public Member Functions

 MotorPWM ()
 
virtual ~MotorPWM ()
 
const ClassIdentifier getInfo ()
 Command handlers always have class infos. Works well with ChoosableClass. More...
 
void turn (int16_t power)
 
void stopMotor ()
 
void startMotor ()
 
void setPwmSpeed (SpeedPWM_DRV spd)
 
SpeedPWM_DRV getPwmSpeed ()
 
void setMode (ModePWM_DRV mode)
 
ModePWM_DRV getMode ()
 
void saveFlash ()
 
void restoreFlash ()
 
CommandStatus command (const ParsedCommand &cmd, std::vector< CommandReply > &replies)
 
virtual std::string getHelpstring ()
 
void setPWM (uint32_t value, uint8_t ccr)
 
- Public Member Functions inherited from MotorDriver
 MotorDriver ()
 
virtual ~MotorDriver ()
 
const ClassType getClassType () override
 returns the used classchooser selection id of this instance More...
 
virtual void emergencyStop (bool reset=false)
 
virtual bool motorReady ()
 
virtual EncodergetEncoder ()
 
virtual void setEncoder (std::shared_ptr< Encoder > &encoder)
 
virtual bool hasIntegratedEncoder ()
 
- Public Member Functions inherited from ChoosableClass
virtual ~ChoosableClass ()
 
uint16_t getSelectionID ()
 
- 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 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)
 
- Public Member Functions inherited from PersistentStorage
 PersistentStorage ()
 
virtual ~PersistentStorage ()
 
void restoreFlashDelayed ()
 

Static Public Member Functions

static bool isCreatable ()
 
- Static Public Member Functions inherited from ChoosableClass
static bool isCreatable ()
 
- 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 Member Functions inherited from PersistentStorage
static std::vector< PersistentStorage * > & getFlashHandlers ()
 
static void restoreFlashStartupCb ()
 

Static Public Attributes

static ClassIdentifier info
 
static bool pwmDriverInUse = false
 
static PWMConfigconfig
 
- 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 CommandHandler
static bool logEnabled = true
 

Private Types

enum class  MotorPWM_commands : uint32_t { mode , freq , dir }
 

Static Private Member Functions

static std::pair< uint16_t, uint16_t > freqToPeriodPsc (uint32_t freq)
 

Private Attributes

float tFreq = 1
 
int32_t period = 20000
 
int32_t prescaler = 95
 
bool invertDir = false
 
SpeedPWM_DRV pwmspeed = SpeedPWM_DRV::LOW
 
ModePWM_DRV mode = ModePWM_DRV::CENTERED_PWM
 
bool active = false
 

Static Private Attributes

static const PWMConfig timerConfig
 

Additional Inherited Members

- 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 CommandHandler
bool commandsEnabled = true
 
std::vector< CmdHandlerCommanddefregisteredCommands
 
CmdHandlerInfo cmdHandlerInfo
 
- Protected Attributes inherited from PersistentStorage
bool restoreDelayedFlag = false
 
- Static Protected Attributes inherited from PersistentStorage
static bool startupComplete = false
 

Detailed Description

Contains motor drivers methods based on PWM generation.

Can output different types of PWM signals based on a torque input.

Definition at line 54 of file MotorPWM.h.

Member Enumeration Documentation

◆ MotorPWM_commands

enum class MotorPWM::MotorPWM_commands : uint32_t
strongprivate
Enumerator
mode 
freq 
dir 

Definition at line 55 of file MotorPWM.h.

Constructor & Destructor Documentation

◆ MotorPWM()

MotorPWM::MotorPWM ( )

Definition at line 35 of file MotorPWM.cpp.

◆ ~MotorPWM()

MotorPWM::~MotorPWM ( )
virtual

Definition at line 48 of file MotorPWM.cpp.

Member Function Documentation

◆ command()

CommandStatus MotorPWM::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 248 of file MotorPWM.cpp.

◆ freqToPeriodPsc()

std::pair< uint16_t, uint16_t > MotorPWM::freqToPeriodPsc ( uint32_t  freq)
staticprivate

Calculates period and prescaler for a timer based on frequency

Definition at line 113 of file MotorPWM.cpp.

◆ getHelpstring()

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

Returns a description of this class

Reimplemented from CommandHandler.

Definition at line 80 of file MotorPWM.h.

◆ getInfo()

const ClassIdentifier MotorPWM::getInfo ( )
virtual

Command handlers always have class infos. Works well with ChoosableClass.

Implements CommandHandler.

Definition at line 26 of file MotorPWM.cpp.

◆ getMode()

ModePWM_DRV MotorPWM::getMode ( )

Definition at line 242 of file MotorPWM.cpp.

◆ getPwmSpeed()

SpeedPWM_DRV MotorPWM::getPwmSpeed ( )

Definition at line 197 of file MotorPWM.cpp.

◆ isCreatable()

bool MotorPWM::isCreatable ( )
static

Definition at line 30 of file MotorPWM.cpp.

◆ restoreFlash()

void MotorPWM::restoreFlash ( )
virtual

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

Reimplemented from PersistentStorage.

Definition at line 212 of file MotorPWM.cpp.

◆ saveFlash()

void MotorPWM::saveFlash ( )
virtual

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 204 of file MotorPWM.cpp.

◆ setMode()

void MotorPWM::setMode ( ModePWM_DRV  mode)

Definition at line 237 of file MotorPWM.cpp.

◆ setPWM()

void MotorPWM::setPWM ( uint32_t  value,
uint8_t  ccr 
)

Updates pwm pulse length

Definition at line 184 of file MotorPWM.cpp.

◆ setPwmSpeed()

void MotorPWM::setPwmSpeed ( SpeedPWM_DRV  spd)

Setup the timer for different frequency presets.

Definition at line 123 of file MotorPWM.cpp.

◆ startMotor()

void MotorPWM::startMotor ( )
virtual

Enable the motor driver

Reimplemented from MotorDriver.

Definition at line 226 of file MotorPWM.cpp.

◆ stopMotor()

void MotorPWM::stopMotor ( )
virtual

Disable the motor driver

Reimplemented from MotorDriver.

Definition at line 231 of file MotorPWM.cpp.

◆ turn()

void MotorPWM::turn ( int16_t  val)
virtual

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 59 of file MotorPWM.cpp.

Member Data Documentation

◆ active

bool MotorPWM::active = false
private

Definition at line 97 of file MotorPWM.h.

◆ config

PWMConfig& MotorPWM::config
static

Definition at line 86 of file MotorPWM.h.

◆ info

ClassIdentifier MotorPWM::info
static
Initial value:
= {
.name = "PWM" ,
.id=CLSID_MOT_PWM,
}

Definition at line 62 of file MotorPWM.h.

◆ invertDir

bool MotorPWM::invertDir = false
private

Definition at line 92 of file MotorPWM.h.

◆ mode

ModePWM_DRV MotorPWM::mode = ModePWM_DRV::CENTERED_PWM
private

Definition at line 96 of file MotorPWM.h.

◆ period

int32_t MotorPWM::period = 20000
private

Definition at line 89 of file MotorPWM.h.

◆ prescaler

int32_t MotorPWM::prescaler = 95
private

Definition at line 90 of file MotorPWM.h.

◆ pwmDriverInUse

bool MotorPWM::pwmDriverInUse = false
static

Definition at line 84 of file MotorPWM.h.

◆ pwmspeed

SpeedPWM_DRV MotorPWM::pwmspeed = SpeedPWM_DRV::LOW
private

Definition at line 95 of file MotorPWM.h.

◆ tFreq

float MotorPWM::tFreq = 1
private

Definition at line 88 of file MotorPWM.h.

◆ timerConfig

const PWMConfig MotorPWM::timerConfig
staticprivate

The timer channels are mapped in the cpp_target_config files

Definition at line 102 of file MotorPWM.h.


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