Smartcar Shield
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SmartCar Class Reference

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

#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.
 
 SmartCar (Runtime &runtime, Control &control, HeadingSensor &headingSensor, Odometer &odometerLeft, Odometer &odometerRight)
 Constructs a car equipped with a heading sensor and two odometers.
 
void update () override
 Adjusts the speed when cruise control is enabled and calculates the current heading.
 
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.
 
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.
 
- Public Member Functions inherited from DistanceCar
 DistanceCar (Runtime &runtime, Control &control, Odometer &odometer)
 Constructs a car equipped with a distance sensor.
 
 DistanceCar (Runtime &runtime, Control &control, Odometer &odometerLeft, Odometer &odometerRight)
 Constructs a car equipped with a distance sensor.
 
long getDistance ()
 Gets the car's travelled distance.
 
float getSpeed ()
 Gets the car's current speed in meters per second.
 
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.
 
void disableCruiseControl ()
 Disable cruise control.
 
- Public Member Functions inherited from SimpleCar
 SimpleCar (Control &control)
 Constructs a simple car.
 
void setAngle (int angle) override
 Set the car's driving angle.
 
- 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.
 
int getHeading ()
 Returns the car's current heading in degrees [0, 360)
 

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);
The class to programatically represent the Arduino runtime environment or alternatively calls to the ...
A brushed motor is controlled via two wires where the flow of current determines the direction of rot...
Class to represent controling a vehicle by applying different speed on each side, like a tank.
A class to represent the common - directionless - Odometers (speed encoders) that can be used to prim...
The class representing a GY-50 gyroscope module based on the L3G4200D sensor.
Definition GY50.hpp:34
A class to programmatically represent a vehicle equipped with odometers and a heading sensor.
Definition SmartCar.hpp:12
const BrushedMotorPins leftMotorPins
Definition Smartcar.h:50
const uint8_t leftOdometerPin
Definition Smartcar.h:52
const BrushedMotorPins rightMotorPins
Definition Smartcar.h:51

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);
const uint8_t rightOdometerPin
Definition Smartcar.h:53

Definition at line 13 of file SmartCar.cpp.

Member Function Documentation

◆ overrideMotorSpeed()

void DistanceCar::overrideMotorSpeed ( int  firstMotorSpeed,
int  secondMotorSpeed 
)
overridevirtual

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);

Reimplemented from DistanceCar.

Definition at line 187 of file DistanceCar.cpp.

◆ setSpeed()

void DistanceCar::setSpeed ( float  speed)
overridevirtual

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);

Reimplemented from DistanceCar.

Definition at line 113 of file DistanceCar.cpp.

Here is the call graph for this function:

◆ 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: