-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDriveShield.cpp
109 lines (81 loc) · 2.52 KB
/
DriveShield.cpp
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
106
107
108
109
/*Written by Jacob Smith and Matthew Millendorf for Brandeis Robotics club
Provides wrapper functions for the Zumo32U4Motors class. It is documented
to explain its function to a general audience. Documentation is provided in the header file
April 2 2018
randomSeed(analogRead(5)); // randomize using noise from analog pin 5
https://www.tutorialspoint.com/arduino/arduino_random_numbers.htm
*/
#if defined (ARDUINO_AVR_UNO) | defined(ARDUINO_ESP8266_WEMOS_D1R1)
//includes the libraries of code necessary to make this one work
#include <Arduino.h>
#include <Wire.h>
#include <Timer.h>
#include "DriveShield.h"
//creates a new Drive object, which won't have random motor power
DriveShield::DriveShield(){
power=200;
}
//returns the motor power the robot is using
int DriveShield::getPower(){
return power;
}
//sets the motor power the robot is using
void DriveShield::setPower(int power){
this->power=power;
}
//the main driving method, performs one of public commands, such as driving forward
//sets the robot to perform the basic action, waits for the specified period of time, and stops the robot
void DriveShield::move(void (DriveShield::*command) (),int time){
driveTimer.resetTime();
while (driveTimer.getTime()<time){
(*this.*command)();
}
stopDrive();
}
//public methods to command the robot to perform an action for a specified period of time
//refer to private move helper method
void DriveShield::driveForward (int time) {
move(&DriveShield::driveForward,time);
}
void DriveShield::driveBackward(int time) {
move(&DriveShield::driveBackward,time);
}
void DriveShield::pivotRight(int time) {
move(&DriveShield::pivotRight,time);
}
void DriveShield::pivotLeft(int time){
move(&DriveShield::pivotLeft,time);
}
void DriveShield::turnRight(int time) {
move(&DriveShield::turnRight,time);
}
void DriveShield::turnLeft(int time){
move(&DriveShield::turnLeft,time);
}
void DriveShield::stopDrive (int time) {
move(&DriveShield::stopDrive,time);
}
//sets the robot to perform the basic action, using the ZUMO32U4 class to interface with the robots
void DriveShield::driveForward() {
drive.setSpeeds(power, power);
}
void DriveShield::driveBackward() {
drive.setSpeeds(-power, -power);
}
void DriveShield::pivotRight(){
drive.setSpeeds(0,power);
}
void DriveShield::pivotLeft(){
drive.setSpeeds(power,0);
}
void DriveShield::turnRight() {
drive.setSpeeds(power, -power);
}
void DriveShield::turnLeft() {
drive.setSpeeds(-power, power);
}
void DriveShield::stopDrive () {
drive.setSpeeds(0, 0);
}
#else
#endif