RoboidControl for Arduino
Loading...
Searching...
No Matches
SwitchesConfiguration.h
Go to the documentation of this file.
1#include "DifferentialDrive.h"
2#include "Roboid.h"
3
4// This type of BB2B robot uses switches at the front to control the motors.
5// Different kinds of switches can be used:
6// * Microswitches
7// * IR distance sensor
8//
9// As long as they can use used to drive a digital input.
10
11// Hardware setup
12// ==============
13// In the configuration we first configure which hardware devices we use and how
14// they are connected to the board: The default setup uses a DRV8833 motor
15// driver:
16#include "DRV8833.h"
17DRV8833 drv8833 = DRV8833(9, // pinAIn1
18 10, // pinAIn2
19 6, // pinBIn1
20 5 // pinBIn2
21);
22// The `AIn` and `BIn` pins are connected to pin 14/12 and 13/15 respectively.
23// We do not drive the standby pin, so this needs to be pulled `high` to make
24// sure the motor driver is enabled.
25
26// Then we define the two swiches we use as input:
27#include <Switch.h>
28Switch sensorLeft = Switch(12 // pinInput
29);
30Switch sensorRight = Switch(2 // pinInput
31);
32// The left sensor is attached to pin 12 of the microcontroller and the right
33// sensor to pin 2.
34
35// Robooid
36// =======
37
38Roboid* RoboidSetup() {
39 Placement sensors[] = {Placement(&sensorLeft, -30),
40 Placement(&sensorRight, 30)};
41 Perception* perception = new Perception(sensors);
42 // The sensor on the left is directed to the ... left and the right
43 // sensor is directed to the right.
44
45 Propulsion* propulsion = (Propulsion*)new DifferentialDrive(
46 Placement(&drv8833.motorA, Vector3::left),
47 Placement(&drv8833.motorB, Vector3::right));
48 // The motor connected to the `A` port of the motor controller is found on
49 // the right side of the robot, while the motor on the left side of the
50 // robot is connected to the `B` port of the motor controller.
51
52 Roboid* roboid = new Roboid(perception, propulsion);
53 return roboid;
54}
55
56void UpdateRoboid(Roboid* roboid) {
57 // each motor rotates forward with max speed (value: 1),
58 // unless an object is detected nearby.
59 // The left motor is controlled by the sensor on the right
60 // and vice versa
61 bool obstacleLeft = roboid->perception->SwitchOn(-45, 0);
62 bool obstacleRight = roboid->perception->SwitchOn(0, 45);
63
64 float leftMotorSpeed = obstacleRight ? -1.0F : 1.0F;
65 float rightMotorSpeed = obstacleLeft ? -1.0F : 1.0F;
66
67 DifferentialDrive* diffDrive = (DifferentialDrive*)&roboid->propulsion;
68 diffDrive->SetTargetSpeeds(leftMotorSpeed, rightMotorSpeed);
69}
Propulsion propulsion
Definition DefaultRoboidConfiguration.h:3
Roboid * RoboidSetup()
Definition SwitchesConfiguration.h:38
DRV8833 drv8833
Definition SwitchesConfiguration.h:17
Switch sensorLeft
Definition SwitchesConfiguration.h:28
Switch sensorRight
Definition SwitchesConfiguration.h:30
void UpdateRoboid(Roboid *roboid)
Definition SwitchesConfiguration.h:56
Roboid * roboid
Definition main.cpp:9
static const Vector3 left
A vector3 with values (-1, 0, 0)
Definition Vector3.h:70
static const Vector3 right
A vector with values (1, 0, 0)
Definition Vector3.h:66