Wyatt  1.0.1
Robot.cpp
1 //
2 // Created by Tucker Haydon on 4/25/17.
3 //
4 
5 #include "../include/Robot.h"
6 
7 
8 Robot::Robot() {
9 
10  /* Start wiring pi */
11  wiringPiSetup();
12 
13  /* Create AdafruitPWMServo controller */
14  this->pwmServoHat = new AdafruitPWMServoHat();
15 
16  /* Create hardware adapters */
17  this->leftMotorAdapter = new MotorAdapter(pwmServoHat, LEFT_MOTOR_FORWARD_PIN, LEFT_MOTOR_BACKWARD_PIN);
18  this->rightMotorAdapter = new MotorAdapter(pwmServoHat, RIGHT_MOTOR_FORWARD_PIN, RIGHT_MOTOR_BACKWARD_PIN);
19  this->leftEncoderAdapter = new EncoderAdapter(LEFT_ENCODER_PIN_A, LEFT_ENCODER_PIN_B, ENCODER_TICKS_PER_REVOLUTION, H_LEFT_ENCODER);
20  this->rightEncoderAdapter = new EncoderAdapter(RIGHT_ENCODER_PIN_A, RIGHT_ENCODER_PIN_B, ENCODER_TICKS_PER_REVOLUTION, H_RIGHT_ENCODER);
21 
22  /* Create the sensors */
23  this->leftEncoderSensor = new EncoderSensor();
24  this->rightEncoderSensor = new EncoderSensor();
25 
26  /* Create the sensor manager and communicator */
27  this->sensorManager = new SensorManager();
28  this->communicator = new Communicator(sensorManager);
29 
30  /* Register the hardware adapters with the communicator */
31  this->communicator->registerHardware(H_LEFT_MOTOR, this->leftMotorAdapter);
32  this->communicator->registerHardware(H_RIGHT_MOTOR, this->rightMotorAdapter);
33  this->communicator->registerHardware(H_LEFT_ENCODER, this->leftEncoderAdapter);
34  this->communicator->registerHardware(H_RIGHT_ENCODER, this->rightEncoderAdapter);
35 
36  /* Register the sensors with the sensor manager */
37  sensorManager->addSensor(H_LEFT_ENCODER, this->leftEncoderSensor);
38  sensorManager->addSensor(H_RIGHT_ENCODER, this->rightEncoderSensor);
39 
40  /* Start the hardware adapters */
41  this->rightEncoderAdapter->start();
42  this->leftEncoderAdapter->start();
43 
44  /* Start the communicator */
45  this->communicator->start();
46 
47  /* Create and start the command manager */
48  this->commander = new CommandManager();
49 
50 }
51 
52 void Robot::run() {
53 
54  double radius = 60; // cm
55  int duration_ms = 10000; // ms
56  double rotationRate = 2 * 3.14159 / (duration_ms / 1000.0); // 1 rotation in X seconds
57 
58  Command* driveArc = new DriveRobotCommand(
59  this->communicator,
60  this->leftEncoderSensor,
61  this->rightEncoderSensor,
62  radius,
63  rotationRate,
64  duration_ms,
65  WHEEL_DIAMETER,
66  DRIVETRAIN_DIAMETER);
67 
68  this->commander->runCommand(driveArc);
69 
70  while(!driveArc->isFinished());
71 
72  pwmServoHat->stopAllMotors();
73  usleep(500000);
74  pwmServoHat->stopAllMotors();
75 }
76 
77 
78 
80 
81 }
~Robot()
Definition: Robot.cpp:79
virtual void addSensor(Hardware hardware, ISensor *sensor)=0
void run()
Definition: Robot.cpp:52
void registerHardware(Hardware hardware, IHardwareInterface *interface)
Definition: Command.h:6
bool runCommand(Command *command)
bool isFinished()
Definition: Command.cpp:64