TinyRobotics
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CarDifferential< N, T, MotorMT > Class Template Reference

Car model with differential drive. The direction is controlled by adjusting the speed of the motors. Even motors are on the left side, odd motors are on the right side. More...

#include <CarDifferential.h>

Inheritance diagram for CarDifferential< N, T, MotorMT >:
Inheritance graph
[legend]
Collaboration diagram for CarDifferential< N, T, MotorMT >:
Collaboration graph
[legend]

Public Member Functions

void setPins (int motor, int in1, int in2)
 Set the pins for a specific motor (0=front left, 1=front right, 2=rear left, 3=rear right)
 
void setSpeed (float percent)
 Set forward/reverse speed for all motors (percent, -100 to 100). Positive = forward, negative = reverse.
 
void setSteeringAgle (float angle)
 Set turn (percent, -100 to 100). Positive = right, negative = left. This slows down the motors on one side to turn the car.
 
void setSteeringAngle (Angle angle)
 
void end () override
 Stop all motors and reset speed and turn state.
 
void setMotorGain (int motor, float gain)
 Set a calibration gain for a specific motor (default 1.0).
 
bool isPinsSet () const
 Check if the necessary pins for the vehicle's actuators have been set (pure virtual).
 
bool onMessage (const Message< float > &msg) override
 Handle an incoming message (pure virtual).
 
std::vector< MessageContent > getControls () const override
 
MotorMT & getMotor (size_t index)
 
std::vector< IMotor< T > * > getMotors () override
 
- Public Member Functions inherited from Vehicle< MotorT >
bool begin ()
 Initialize the vehicle.
 
virtual void end ()=0
 Reset the vehicle to a safe or neutral state (pure virtual).
 
virtual bool isPinsSet () const =0
 Check if the necessary pins for the vehicle's actuators have been set (pure virtual).
 
virtual std::vector< MessageContent > getControls () const =0
 
float getSpeedFactor () const
 Get the speed factor (scaling for speed commands).
 
void setSpeedFactor (float factor)
 Set the speed factor (scaling for speed commands).
 
virtual std::vector< IMotor< MotorT > * > getMotors ()=0
 
- Public Member Functions inherited from MessageHandler
virtual bool onMessage (const Message< float > &msg)=0
 Handle an incoming message (pure virtual).
 
virtual bool onMessage (const Message< Coordinate< float > > &msg)
 
virtual bool onMessage (const Message< GPSCoordinate > &msg)
 
virtual bool onMessage (const Message< MotionState3D > &msg)
 
- Public Member Functions inherited from MessageSource
void subscribe (MessageHandler &handler, MessageOrigin origin=MessageOrigin::Undefined, MessageContent content=MessageContent::Undefined)
 Subscribe a message handler to this source, with optional filtering.
 
void unsubscribeAll ()
 Remove all registered message handlers.
 
void sendMessage (Message< float > &msg)
 Publish a message to all registered handlers.
 
void sendMessage (const Message< Coordinate< float > > &msg)
 Publish a message to all registered handlers.
 
void sendMessage (const Message< GPSCoordinate > &msg)
 Publish a message to all registered handlers.
 
void sendMessage (const Message< MotionState3D > &msg)
 Overload for MotionState3D messages.
 

Protected Member Functions

void updateMotors ()
 Update all motors based on speed and turn.
 

Protected Attributes

MotorMT motors_ [N]
 
float motorGain_ [N] = {1.0f, 1.0f, 1.0f, 1.0f}
 
float speed_
 
float turn_
 
- Protected Attributes inherited from Vehicle< MotorT >
float speedFactor_ = 1.0f
 
- Protected Attributes inherited from MessageSource
std::vector< MessageHandlerEntrymessageHandlers_
 

Detailed Description

template<size_t N = 4, typename T = float, typename MotorMT = BrushedMotor<T>>
class tinyrobotics::CarDifferential< N, T, MotorMT >

Car model with differential drive. The direction is controlled by adjusting the speed of the motors. Even motors are on the left side, odd motors are on the right side.

This class abstracts a simple N-wheel-drive car:

Usage Example:

CarDifferentialWD<4> car;
car.setPins(0, 2, 3); // motor 0 (front left): in1=2, in2=3
car.setPins(1, 5, 6); // motor 1 (front right): in1=5, in2=6
car.setPins(2, 8, 9); // motor 2 (rear left): in1=8, in2=9
car.setPins(3, 11, 12); // motor 3 (rear right): in1=11, in2=12
car.setSpeed(60); // 60% forward
car.setTurn(30); // turn right by slowing left motors
car.end(); // brake all motors

Member Function Documentation

◆ end()

void end ( )
inlineoverridevirtual

Stop all motors and reset speed and turn state.

Implements Vehicle< MotorT >.

◆ getControls()

std::vector< MessageContent > getControls ( ) const
inlineoverridevirtual

Implements Vehicle< MotorT >.

◆ getMotors()

std::vector< IMotor< T > * > getMotors ( )
inlineoverridevirtual

Implements Vehicle< MotorT >.

◆ isPinsSet()

bool isPinsSet ( ) const
inlinevirtual

Check if the necessary pins for the vehicle's actuators have been set (pure virtual).

Implements Vehicle< MotorT >.

◆ onMessage()

bool onMessage ( const Message< float > &  msg)
inlineoverridevirtual

Handle an incoming message (pure virtual).

This method should be implemented by derived classes to process messages.

Parameters
msgThe message to handle.
Returns
true if the message was handled successfully, false otherwise.

Implements MessageHandler.

◆ setMotorGain()

void setMotorGain ( int  motor,
float  gain 
)
inline

Set a calibration gain for a specific motor (default 1.0).

Parameters
motorMotor index (0=front left, 1=front right, 2=rear left, 3=rear right)
gainGain factor (e.g., 1.05 for +5% output)

◆ updateMotors()

void updateMotors ( )
inlineprotected

Update all motors based on speed and turn.

  • m1: front left
  • m2: front right
  • m3: rear left
  • m4: rear right

To turn right, left motors go slower, right motors go faster (and vice versa).


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