main.cpp
Go to the documentation of this file.
1 #include <Arduino.h>
2 #include <Servo.h>
3 
4 // Pins to Left Wheels
5 #define pinL1 13
6 #define pinL2 12
7 #define pinL3 11
8 // Pins to Right Wheels
9 #define pinR1 9
10 #define pinR2 8
11 #define pinR3 7
12 // Pin to the Servo
13 #define pinServo 5
14 
15 // PWM specs of the Spark motor controller. Spark manual:
16 // http://www.revrobotics.com/content/docs/LK-ATFF-SXAO-UM.pdf
17 #define sparkMax 1000 // Default full-reverse input pulse
18 #define sparkMin 2000 // Default full-forward input pulse
19 
20 Servo theServo; // the servo used to control a camera that can look around the robot
21 Servo leftOne;
22 Servo leftTwo;
23 Servo leftThree;
24 Servo rightOne;
25 Servo rightTwo;
26 Servo rightThree;
27 
28 struct DATA {
29  float linear;
30  float rotation;
31  float servo;
32 } received;
33 
34 void setWheelVelocity(int left, int right) {
35  leftOne.writeMicroseconds(map(left, -100, 100, sparkMin, sparkMax));
36  leftTwo.writeMicroseconds(map(left, -100, 100, sparkMin, sparkMax));
37  leftThree.writeMicroseconds(map(left, -100, 100, sparkMin, sparkMax));
38  rightOne.writeMicroseconds(map(right, -100, 100, sparkMin, sparkMax));
39  rightTwo.writeMicroseconds(map(right, -100, 100, sparkMin, sparkMax));
40  rightThree.writeMicroseconds(map(right, -100, 100, sparkMin, sparkMax));
41 }
42 
43 void setup() {
44  Serial.begin(9600);
45  leftOne.attach(pinL1);
46  leftTwo.attach(pinL2);
47  leftThree.attach(pinL3);
48  rightOne.attach(pinR1);
49  rightTwo.attach(pinR2);
50  rightThree.attach(pinR3);
51  theServo.attach(pinServo);
52 }
53 
54 void loop() {
55  if (Serial.available() >= sizeof(uint8_t)) {
56  delayMicroseconds(10);
57  uint8_t cmd = (uint8_t) Serial.read();
58  // Serial.print("GOT a cmd");
59  // Serial.println(cmd);
60  // TWIST MOTOR COMMAND
61  if (cmd == 0) {
62  Serial.readBytes((char *) &received.linear, sizeof(float));
63  Serial.readBytes((char *) &received.rotation, sizeof(float));
64  // Serial.print("GOT TWIST");
65  // Serial.println((int) received.linear);
66  // Serial.println((int) received.rotation);
67  setWheelVelocity((int) ((received.linear + received.rotation) * 100), (int) ((received.linear - received.rotation) * 100));
68  }
69  // SERVO MOTOR COMMAND
70  else if (cmd == 2) {
71  Serial.readBytes((char *) &received.servo, sizeof(float));
72  // Serial.print("GOT SERVO");
73  // Serial.println((int) received.servo);
74  theServo.write((int) received.servo);
75  }
76  }
77 }
78 
Definition: main.cpp:28
Servo leftOne
Definition: main.cpp:21
#define sparkMax
Definition: main.cpp:17
struct DATA received
#define pinL3
Definition: main.cpp:7
void loop()
Definition: main.cpp:54
void setup()
Definition: main.cpp:43
float rotation
Definition: main.cpp:30
Servo leftTwo
Definition: main.cpp:22
void setWheelVelocity(int left, int right)
Definition: main.cpp:34
#define pinR1
Definition: main.cpp:9
#define pinR3
Definition: main.cpp:11
Servo rightThree
Definition: main.cpp:26
float servo
Definition: main.cpp:31
#define pinL2
Definition: main.cpp:6
#define pinL1
Definition: main.cpp:5
Servo leftThree
Definition: main.cpp:23
float linear
Definition: main.cpp:29
#define pinR2
Definition: main.cpp:10
Servo rightOne
Definition: main.cpp:24
#define pinServo
Definition: main.cpp:13
Servo rightTwo
Definition: main.cpp:25
Servo theServo
Definition: main.cpp:20
#define sparkMin
Definition: main.cpp:18


simple_drive
Author(s): Daniel Snider , Matthew Mirvish
autogenerated on Mon Jun 10 2019 15:05:01