wpi-32u4-library
main.cpp
Go to the documentation of this file.
1
7#include <Arduino.h>
8#include <wpi-32u4-lib.h>
9
10#include <IRdecoder.h>
11#include <ir_codes.h>
12
13#include <Chassis.h>
14
15// Include Servo32u4 library
16#include <Servo32u4.h>
17
18// Declare a chassis object with nominal dimensions
19// In practice, adjust the parameters: wheel diam, encoder counts, wheel track
20Chassis chassis(7.0, 1440, 14.9);
21
22// These can be any two of: 5, 6, 12, 13, EXCEPT both 6 and 12 at the same time (see library for reason)
25
26// Setup the IR receiver/decoder object
27const uint8_t IR_DETECTOR_PIN = 1;
29
30// Define the states
33
34// A helper function to stop the motors
35void idle(void)
36{
37 Serial.println("idle()");
38
39 //stop motors
40 chassis.idle();
41
42 servoA.detach();
43 servoB.detach();
44
45 //set state to idle
47}
48
49// Function to adjust all servos
50void adjustServos(uint16_t uSeconds)
51{
53 {
54 servoA.writeMicroseconds(uSeconds);
55 servoB.writeMicroseconds(uSeconds);
56 }
57}
58
59// Handles a key press on the IR remote
60void handleKeyPress(int16_t keyPress)
61{
62 Serial.println("Key: " + String(keyPress));
63
64 //ENTER_SAVE idles, regardless of state -- E-stop
65 if(keyPress == ENTER_SAVE) idle();
66
67 switch(robotState)
68 {
69 case ROBOT_IDLE:
70 if(keyPress == PLAY_PAUSE)
71 {
73 servoA.attach();
74 servoB.attach();
75 }
76 break;
77
79 if(keyPress == VOLplus) //VOL+ increases speed
80 {
81 adjustServos(2000);
82 }
83
84 if(keyPress == VOLminus) //VOL- decreases speed
85 {
86 adjustServos(1000);
87 }
88
89 break;
90
91 default:
92 break;
93 }
94}
95
96/*
97 * This is the standard setup function that is called when the board is rebooted
98 * It is used to initialize anything that needs to be done once.
99 */
100void setup()
101{
102 // This will initialize the Serial at a baud rate of 115200 for prints
103 // Be sure to set your Serial Monitor appropriately
104 Serial.begin(115200);
105
106 // initialize the chassis (which also initializes the motors)
107 chassis.init();
108 idle();
109
110 // these can be undone for the student to adjust
112
113 // Setup the servos
114 servoA.attach();
115 servoB.attach();
116
117 // initialize the IR decoder
118 decoder.init();
119
120 Serial.println("/setup()");
121}
122
123/*
124 * The main loop for the program. The loop function is repeatedly called
125 * after setup() is complete.
126 */
127void loop()
128{
129 // Check for a key press on the remote
130 int16_t keyPress = decoder.getKeyCode();
131 if(keyPress >= 0) handleKeyPress(keyPress);
132
133 // A basic state machine
134 switch(robotState)
135 {
136 default:
137 break;
138 }
139}
void idle(void)
Idles chassis. Motors will stop.
Definition: Chassis.cpp:46
void init(void)
Initializes the chassis. Must be called in setup().
Definition: Chassis.cpp:13
void setMotorPIDcoeffs(float kp, float ki)
Sets PID coefficients for the motors. Not independent.
Definition: Chassis.cpp:35
int16_t getKeyCode(bool acceptRepeat=false)
Definition: IRdecoder.h:65
void init(void)
Definition: IRdecoder.cpp:12
A servo class to control a servo on pin 5.
Definition: servo32u4.h:68
void attach(void)
Definition: servo32u4.cpp:14
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:42
void detach(void)
Definition: servo32u4.cpp:31
A servo class to control a servo on pin 6.
Definition: servo32u4.h:95
void detach(void)
Definition: servo32u4.cpp:69
void attach(void)
Definition: servo32u4.cpp:55
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:82
#define VOLminus
Definition: ir_codes.h:3
#define ENTER_SAVE
Definition: ir_codes.h:12
#define PLAY_PAUSE
Definition: ir_codes.h:4
#define VOLplus
Definition: ir_codes.h:5
void idle(void)
Definition: main.cpp:35
Chassis chassis(7.0, 1440, 14.9)
Servo32U4Pin5 servoA
Definition: main.cpp:23
void adjustServos(uint16_t uSeconds)
Definition: main.cpp:50
ROBOT_STATE robotState
Definition: main.cpp:32
ROBOT_STATE
Definition: main.cpp:31
@ ROBOT_IDLE
Definition: main.cpp:31
@ ROBOT_SERVO_TEST
Definition: main.cpp:31
Servo32U4Pin6 servoB
Definition: main.cpp:24
void setup()
Definition: main.cpp:100
const uint8_t IR_DETECTOR_PIN
Definition: main.cpp:27
void handleKeyPress(int16_t keyPress)
Definition: main.cpp:60
IRDecoder decoder(IR_DETECTOR_PIN)
void loop()
Definition: main.cpp:127