TinyRobotics
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
GenericMotor< T > Class Template Reference

Motor abstraction for integrating external/custom motor drivers using callbacks. More...

#include <GenericMotor.h>

Inheritance diagram for GenericMotor< T >:
Inheritance graph
[legend]
Collaboration diagram for GenericMotor< T >:
Collaboration graph
[legend]

Public Member Functions

 GenericMotor (uint8_t id, void *driver=nullptr)
 
bool begin () override
 Start the motor using the optional begin callback.
 
bool setValuePercent (T percent) override
 Set value as percentage (-100 to 100)
 
getValuePercent () const override
 Get current value percentage.
 
void setAngle (T angle)
 For Servo: Set angle in degrees (-90 to 90) by mapping it to percentage.
 
getAngle ()
 For Servo: Get angle in degrees by mapping percentage back to angle.
 
void end () override
 Stop the motor using the optional end callback.
 
void setValueCallback (void(*valueCB)(T value, GenericMotor &motor))
 Mandatory callback to define speed/angle control behavior.
 
void setBeginCallback (bool(*beginCB)(GenericMotor &motor))
 Optional callback to define begin behavior.
 
void setEndCallback (void(*endCB)(GenericMotor &motor))
 Optional callback to define end behavior.
 
template<typename DriverT >
DriverT & getDriver ()
 Provides the user motor object.
 
bool isPinsSet () const override
 Not supported.
 
void setPin (int pin)
 Not supported.
 
void setPins (int, int, int na=-1)
 Not supported.
 
- Public Member Functions inherited from IMotor< T >
virtual bool begin ()=0
 
virtual void end ()=0
 
virtual bool isPinsSet () const =0
 
virtual bool setValuePercent (T percent)=0
 
virtual T getValuePercent () const =0
 
void setID (uint8_t id)
 
uint8_t getID () const
 

Protected Member Functions

void notSupported (const char *method)
 Log an error for unsupported methods.
 

Static Protected Member Functions

static void defaultEnd (GenericMotor &motor)
 Default logic for end processing: just stop the motor!
 

Protected Attributes

void * driver = nullptr
 
value = 0.0f
 
bool(* beginCB )(GenericMotor &motor) = nullptr
 
void(* endCB )(GenericMotor &motor)=defaultEnd
 
void(* valueCB )(T value, GenericMotor &motor)
 
- Protected Attributes inherited from IMotor< T >
uint8_t id = 0
 

Detailed Description

template<typename T = float>
class tinyrobotics::GenericMotor< T >

Motor abstraction for integrating external/custom motor drivers using callbacks.

This class provides a flexible interface for controlling motors that do not fit the standard interface. The user supplies an external motor object (as a void pointer) and callback functions for starting/stopping the motor and for setting the speed or angle. This allows integration of custom motor drivers or hardware with the TinyRobotics framework.

Example usage:

MyMotorDriver driver;
GenericMotor motor(0, &driver);
motor.setValueCallback([](int8_t value, GenericMotor& m) {
MyMotorDriver* drv = m.getMotor<MyMotorDriver>();
drv->setPWM(value);
});
motor.setBeginCallback([](GenericMotor& m) {
// Custom start logic
return true;
});
motor.setEndCallback([](GenericMotor& m) {
// Custom stop logic
});
Motor abstraction for integrating external/custom motor drivers using callbacks.
Definition: GenericMotor.h:45

Member Function Documentation

◆ begin()

bool begin ( )
inlineoverridevirtual

Start the motor using the optional begin callback.

Implements IMotor< T >.

◆ end()

void end ( )
inlineoverridevirtual

Stop the motor using the optional end callback.

Implements IMotor< T >.

◆ getValuePercent()

T getValuePercent ( ) const
inlineoverridevirtual

Get current value percentage.

Implements IMotor< T >.

◆ isPinsSet()

bool isPinsSet ( ) const
inlineoverridevirtual

Not supported.

Implements IMotor< T >.

◆ setValuePercent()

bool setValuePercent ( percent)
inlineoverridevirtual

Set value as percentage (-100 to 100)

Implements IMotor< T >.

Member Data Documentation

◆ valueCB

void(* valueCB) (T value, GenericMotor &motor)
protected
Initial value:
=
nullptr

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