wpi-32u4-library
examples
servo
src
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
20
Chassis
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)
23
Servo32U4Pin5
servoA
;
24
Servo32U4Pin6
servoB
;
25
26
// Setup the IR receiver/decoder object
27
const
uint8_t
IR_DETECTOR_PIN
= 1;
28
IRDecoder
decoder
(
IR_DETECTOR_PIN
);
29
30
// Define the states
31
enum
ROBOT_STATE
{
ROBOT_IDLE
,
ROBOT_SERVO_TEST
};
32
ROBOT_STATE
robotState
=
ROBOT_IDLE
;
33
34
// A helper function to stop the motors
35
void
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
46
robotState
=
ROBOT_IDLE
;
47
}
48
49
// Function to adjust all servos
50
void
adjustServos
(uint16_t uSeconds)
51
{
52
if
(
robotState
==
ROBOT_SERVO_TEST
)
53
{
54
servoA
.
writeMicroseconds
(uSeconds);
55
servoB
.
writeMicroseconds
(uSeconds);
56
}
57
}
58
59
// Handles a key press on the IR remote
60
void
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
{
72
robotState
=
ROBOT_SERVO_TEST
;
73
servoA
.
attach
();
74
servoB
.
attach
();
75
}
76
break
;
77
78
case
ROBOT_SERVO_TEST
:
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
*/
100
void
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
111
chassis
.
setMotorPIDcoeffs
(5, 0.5);
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
*/
127
void
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
}
Chassis.h
IRdecoder.h
Chassis
Definition:
Chassis.h:19
Chassis::idle
void idle(void)
Idles chassis. Motors will stop.
Definition:
Chassis.cpp:46
Chassis::init
void init(void)
Initializes the chassis. Must be called in setup().
Definition:
Chassis.cpp:13
Chassis::setMotorPIDcoeffs
void setMotorPIDcoeffs(float kp, float ki)
Sets PID coefficients for the motors. Not independent.
Definition:
Chassis.cpp:35
IRDecoder
Definition:
IRdecoder.h:21
IRDecoder::getKeyCode
int16_t getKeyCode(bool acceptRepeat=false)
Definition:
IRdecoder.h:65
IRDecoder::init
void init(void)
Definition:
IRdecoder.cpp:12
Servo32U4Pin5
A servo class to control a servo on pin 5.
Definition:
servo32u4.h:68
Servo32U4Pin5::attach
void attach(void)
Definition:
servo32u4.cpp:14
Servo32U4Pin5::writeMicroseconds
void writeMicroseconds(uint16_t microseconds)
Definition:
servo32u4.cpp:42
Servo32U4Pin5::detach
void detach(void)
Definition:
servo32u4.cpp:31
Servo32U4Pin6
A servo class to control a servo on pin 6.
Definition:
servo32u4.h:95
Servo32U4Pin6::detach
void detach(void)
Definition:
servo32u4.cpp:69
Servo32U4Pin6::attach
void attach(void)
Definition:
servo32u4.cpp:55
Servo32U4Pin6::writeMicroseconds
void writeMicroseconds(uint16_t microseconds)
Definition:
servo32u4.cpp:82
ir_codes.h
VOLminus
#define VOLminus
Definition:
ir_codes.h:3
ENTER_SAVE
#define ENTER_SAVE
Definition:
ir_codes.h:12
PLAY_PAUSE
#define PLAY_PAUSE
Definition:
ir_codes.h:4
VOLplus
#define VOLplus
Definition:
ir_codes.h:5
idle
void idle(void)
Definition:
main.cpp:35
chassis
Chassis chassis(7.0, 1440, 14.9)
servoA
Servo32U4Pin5 servoA
Definition:
main.cpp:23
adjustServos
void adjustServos(uint16_t uSeconds)
Definition:
main.cpp:50
robotState
ROBOT_STATE robotState
Definition:
main.cpp:32
ROBOT_STATE
ROBOT_STATE
Definition:
main.cpp:31
ROBOT_IDLE
@ ROBOT_IDLE
Definition:
main.cpp:31
ROBOT_SERVO_TEST
@ ROBOT_SERVO_TEST
Definition:
main.cpp:31
servoB
Servo32U4Pin6 servoB
Definition:
main.cpp:24
setup
void setup()
Definition:
main.cpp:100
IR_DETECTOR_PIN
const uint8_t IR_DETECTOR_PIN
Definition:
main.cpp:27
handleKeyPress
void handleKeyPress(int16_t keyPress)
Definition:
main.cpp:60
decoder
IRDecoder decoder(IR_DETECTOR_PIN)
loop
void loop()
Definition:
main.cpp:127
wpi-32u4-lib.h
Generated by
1.9.3