-
Notifications
You must be signed in to change notification settings - Fork 27
/
E80_Lab_01.ino
105 lines (79 loc) · 2.71 KB
/
E80_Lab_01.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/********
Default E80 Code
Current Author:
Wilson Ives (wives@g.hmc.edu) '20 (contributed in 2018)
Previous Contributors:
Christopher McElroy (cmcelroy@g.hmc.edu) '19 (contributed in 2017)
Josephine Wong (jowong@hmc.edu) '18 (contributed in 2016)
Apoorva Sharma (asharma@hmc.edu) '17 (contributed in 2016)
*/
/* Libraries */
// general
#include <Arduino.h>
#include <Wire.h>
#include <Pinouts.h>
// E80-specific
#include <SensorIMU.h>
#include <MotorDriver.h>
#include <Logger.h>
#include <Printer.h>
/* Global Variables */
// period in ms of logger and printer
#define LOOP_PERIOD 100
// Motors
MotorDriver motorDriver;
// IMU
SensorIMU imu;
// Logger
Logger logger;
bool keepLogging = true;
// Printer
Printer printer;
// loop start recorder
int loopStartTime;
void setup() {
printer.init();
/* Initialize the Logger */
logger.include(&imu);
logger.include(&motorDriver);
logger.init();
/* Initialise the sensors */
imu.init();
/* Initialize motor pins */
motorDriver.init();
/* Keep track of time */
printer.printMessage("Starting main loop",10);
loopStartTime = millis();
}
void loop() {
int currentTime = millis() - loopStartTime;
/////////// Don't change code above here! ////////////////////
// write code here to make the robot fire its motors in the sequence specified in the lab manual
// the currentTime variable contains the number of ms since the robot was turned on
// The motorDriver.drive function takes in 3 inputs arguments motorA_power, motorB_power, motorC_power:
// void motorDriver.drive(int motorA_power,int motorB_power,int motorC_power);
// the value of motorX_power can range from -255 to 255, and sets the PWM applied to the motor
// The following example will turn on motor B for four seconds between seconds 4 and 8
if (currentTime > 4000 && currentTime <8000) {
motorDriver.drive(0,120,0);
} else {
motorDriver.drive(0,0,0);
}
// DONT CHANGE CODE BELOW THIS LINE
// --------------------------------------------------------------------------
if ( currentTime-printer.lastExecutionTime > LOOP_PERIOD ) {
printer.lastExecutionTime = currentTime;
printer.printValue(0,imu.printAccels());
printer.printValue(1,imu.printRollPitchHeading());
printer.printValue(2,motorDriver.printState());
printer.printToSerial(); // To stop printing, just comment this line out
}
if ( currentTime-imu.lastExecutionTime > LOOP_PERIOD ) {
imu.lastExecutionTime = currentTime;
imu.read(); // this is a sequence of blocking I2C read calls
}
if ( currentTime-logger.lastExecutionTime > LOOP_PERIOD && logger.keepLogging) {
logger.lastExecutionTime = currentTime;
logger.log();
}
}