Smartcar Shield
Public Member Functions | List of all members
SmartCar Class Reference

#include <SmartCar.hpp>

Inheritance diagram for SmartCar:
Inheritance graph
Collaboration diagram for SmartCar:
Collaboration graph

Public Member Functions

 SmartCar (Runtime &runtime, Control &control, HeadingSensor &headingSensor, Odometer &odometer)
 Constructs a car equipped with a heading sensor and an odometer. More...
 
 SmartCar (Runtime &runtime, Control &control, HeadingSensor &headingSensor, Odometer &odometerLeft, Odometer &odometerRight)
 Constructs a car equipped with a heading sensor and two odometers. More...
 
void update () override
 Adjusts the speed when cruise control is enabled and calculates the current heading. More...
 
void overrideMotorSpeed (int firstMotorSpeed, int secondMotorSpeed) override
 Sets the motor speed individually as a percentage of the motors` total power unless cruise control is enabled in which case has no effect. More...
 
void setSpeed (float speed) override
 Sets the car's speed in meters per second if cruise control is enabled otherwise as a percentage of the motor speed. More...
 
- Public Member Functions inherited from DistanceCar
 DistanceCar (Runtime &runtime, Control &control, Odometer &odometer)
 Constructs a car equipped with a distance sensor. More...
 
 DistanceCar (Runtime &runtime, Control &control, Odometer &odometerLeft, Odometer &odometerRight)
 Constructs a car equipped with a distance sensor. More...
 
long getDistance ()
 Gets the car's travelled distance. More...
 
void setSpeed (float speed) override
 Sets the car's speed in meters per second if cruise control is enabled otherwise as a percentage of the motor speed. More...
 
float getSpeed ()
 Gets the car's current speed in meters per second. More...
 
void enableCruiseControl (float proportional=smartcarlib::constants::car::kDefaultProportional, float integral=smartcarlib::constants::car::kDefaultIntegral, float derivative=smartcarlib::constants::car::kDefaultDerivative, unsigned long frequency=smartcarlib::constants::car::kDefaultPidFrequency)
 Enables the car to move with a stable speed using the odometers. More...
 
void disableCruiseControl ()
 Disable cruise control. More...
 
void overrideMotorSpeed (int firstMotorSpeed, int secondMotorSpeed) override
 Sets the motor speed individually as a percentage of the motors` total power unless cruise control is enabled in which case has no effect. More...
 
- Public Member Functions inherited from SimpleCar
 SimpleCar (Control &control)
 Constructs a simple car. More...
 
void setSpeed (float speed) override
 Sets the car's driving speed as a percentage of the motors total speed where the sign indicates direction. More...
 
void setAngle (int angle) override
 Set the car's driving angle. More...
 
void overrideMotorSpeed (int firstMotorSpeed, int secondMotorSpeed) override
 Set the motor speed individually as a percentage of the motors` total power. More...
 
- Public Member Functions inherited from Car
virtual ~Car ()=default
 
- Public Member Functions inherited from HeadingCar
 HeadingCar (Control &control, HeadingSensor &headingSensor)
 Constructs a car equipped with a heading sensor. More...
 
int getHeading ()
 Returns the car's current heading in degrees [0, 360) More...
 

Detailed Description

A class to programmatically represent a vehicle equipped with odometers and a heading sensor

Definition at line 11 of file SmartCar.hpp.

Constructor & Destructor Documentation

◆ SmartCar() [1/2]

SmartCar::SmartCar ( Runtime runtime,
Control control,
HeadingSensor headingSensor,
Odometer odometer 
)

Constructs a car equipped with a heading sensor and an odometer.

Parameters
runtimeThe runtime environment you want to run the class for
controlThe car's control
headingSensorThe heading sensor
odometerThe odometer

Example:

ArduinoRuntime arduinoRuntime;
DifferentialControl control(leftMotor, rightMotor);
GY50 gyroscope(arduinoRuntime, 37);
const auto pulsesPerMeter = 600;
arduinoRuntime, smartcarlib::pins::v2::leftOdometerPin, []() { leftOdometer.update(); },
pulsesPerMeter);
SmartCar car(arduinoRuntime, control, gyroscope, odometer);

Definition at line 3 of file SmartCar.cpp.

◆ SmartCar() [2/2]

SmartCar::SmartCar ( Runtime runtime,
Control control,
HeadingSensor headingSensor,
Odometer odometerLeft,
Odometer odometerRight 
)

Constructs a car equipped with a heading sensor and two odometers.

Parameters
runtimeThe runtime environment you want to run the class for
controlThe car's control
headingSensorThe heading sensor
odometerLeftThe left odometer
odometerRightThe right odometer

Example:

ArduinoRuntime arduinoRuntime;
DifferentialControl control(leftMotor, rightMotor);
GY50 gyroscope(arduinoRuntime, 37);
const auto pulsesPerMeter = 600;
DirectionlessOdometer leftOdometer(
arduinoRuntime, smartcarlib::pins::v2::leftOdometerPin, []() { leftOdometer.update(); },
pulsesPerMeter);
DirectionlessOdometer rightOdometer(
arduinoRuntime, smartcarlib::pins::v2::rightOdometerPin, []() { rightOdometer.update();
}, pulsesPerMeter);
SmartCar car(arduinoRuntime, control, gyroscope, leftOdometer, rightOdometer);

Definition at line 13 of file SmartCar.cpp.

Member Function Documentation

◆ overrideMotorSpeed()

void DistanceCar::overrideMotorSpeed
override

Sets the motor speed individually as a percentage of the motors` total power unless cruise control is enabled in which case has no effect.

Use with caution.

Parameters
firstMotorSpeedThe speed of the motor passed as first argument argument to the car's control class [-100, 100]
secondMotorSpeedThe speed of the motor passed as second argument argument to the car's control class [-100, 100]

Example:

// Make the car spin around
car.overrideMotorSpeed(100, -100);

Definition at line 210 of file DistanceCar.cpp.

◆ setSpeed()

void DistanceCar::setSpeed
override

Sets the car's speed in meters per second if cruise control is enabled otherwise as a percentage of the motor speed.

Sign in both cases determines direction.

Parameters
speedThe car's speed

Example (with cruise control):

car.enableCruiseControl();
float speedInMetersPerSecond = -0.8;
// Set speed to 0.8 meters per second backward
car.setSpeed(speedInMetersPerSecond);

Example (without cruise control):

float speedInPercentageOfMotorPower = 50;
// Set speed to half of the maximum motor speed forward
car.setSpeed(speedInPercentageOfMotorPower);

Definition at line 42 of file DistanceCar.cpp.

◆ update()

void SmartCar::update ( )
overridevirtual

Adjusts the speed when cruise control is enabled and calculates the current heading.

You must have this being executed as often as possible for highest accuracy of heading calculations and cruise control.

Example:

void loop() {
// Update the car readings as often as possible
car.update();
// Other functionality
}

Reimplemented from DistanceCar.

Definition at line 24 of file SmartCar.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files:
DirectionlessOdometer
Definition: DirectionlessOdometer.hpp:14
smartcarlib::pins::v2::leftMotorPins
const BrushedMotorPins leftMotorPins
Definition: Smartcar.h:50
SmartCar
Definition: SmartCar.hpp:11
BrushedMotor
Definition: BrushedMotor.hpp:41
GY50
Definition: GY50.hpp:33
smartcarlib::pins::v2::rightOdometerPin
const uint8_t rightOdometerPin
Definition: Smartcar.h:53
ArduinoRuntime
Definition: ArduinoRuntime.hpp:11
DifferentialControl
Definition: DifferentialControl.hpp:11
smartcarlib::pins::v2::rightMotorPins
const BrushedMotorPins rightMotorPins
Definition: Smartcar.h:51
smartcarlib::pins::v2::leftOdometerPin
const uint8_t leftOdometerPin
Definition: Smartcar.h:52