-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRover_One_Control_Sketch.ino
More file actions
164 lines (146 loc) · 5.04 KB
/
Rover_One_Control_Sketch.ino
File metadata and controls
164 lines (146 loc) · 5.04 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// # Editor : Steven Griset
// # Data : 09.04.2016
// # Sonar System
// # URM37 V3.2 Ultrasonic Sensor from dfRobot
// # Product SKU:SEN0001
// # Version : 0.2
// # Motor Driver Shield
// # Ardumoto from Sparkfun
// # Product SKU:RTL-09896
// # Description:
// # Robotic sound sensor control for autonomous system
// # Robot travels forward until it encounters a obsatcle within 10cm
// # Interrupt subroutine is triggered and robot stops, reverses and turns left
// # In this example will use the ISR function using a single pin Interrupt example
// # Robot also uses whisker triggers to dectect objects that make it pass the sonar system
// # Whisker Trigger system D8 left bumper and D6 is the right bumper
// # Interrupt subroutine is triggered when switch is closed cart will take appropriate maneuver
// # Sonar Connection:
// # Connection:
// # Pin 1 VCC (URM V3.2) -> VCC (Arduino)
// # Pin 2 GND (URM V3.2) -> GND (Arduino)
// # Pin 6 COMP/TRIG (URM V3.2) -> Pin 2 (Arduino)
// # Whisker Triggers Connection:
//# Left Bumper -> Pin 8 (Arduino)
// # Right Bumper -> Pin 6 (Arduino)
// # Working Mode: Autonomous trigger mode
// # If it is your first time to use sonar module,please make sure the two jumpers to the right hand
// # side of the device are set to TTL mode. You'll also find a secondary jumper on
// # the left hand side, you must break this connection or you may damage your device.
// # This code has println code it for testing purposes
#include <Servo.h>
Servo servo1; //Create a servo object
Servo servo2; //Create a second servo object
boolean motion = false; //maneuver variable
const int bumpLeft = 8; // Left bumper digital pin 8
const int bumpRight = 6; // Right bumper digital pin 6
const int Sonar =2; //Sonar digital pin 2
boolean bLeft = false; // boolean variable to hold left bumper switch state
boolean bRight = false; // boolean varibale to hold right bumper switch state
int speed1= 3; //left motor
int speed2 = 11; //right motor
int direction1 = 12; //left motor direction
int direction2 = 13; //right motor direction
void stopMotor(){
// turn both motors off
analogWrite(speed1, 0);
analogWrite(speed2, 0);
}
void forwardMotor(){
//Both motors forward at full duty cycle
digitalWrite(direction1, LOW);
digitalWrite(direction2, LOW);
analogWrite(speed1, 255);
analogWrite(speed2, 255);
}
void reverseMotor(){
//Both motors forward at full duty cycle
digitalWrite(direction1, HIGH);
digitalWrite(direction2, HIGH);
analogWrite(speed1, 255);
analogWrite(speed2, 255);
}
void leftfwdMotor(){
//Both motors left turn forward at full duty cycle
digitalWrite(direction1, LOW);
digitalWrite(direction2, HIGH);
analogWrite(speed1, 255);
analogWrite(speed2, 255);
}
void rightfwdMotor(){
//Both motors right turn forward at full duty cycle
digitalWrite(direction1, HIGH);
digitalWrite(direction2, LOW);
analogWrite(speed1, 255);
analogWrite(speed2, 255);
}
void maneuver(){ //manuever around obstacle with the following motion stop, reverse, trun left
stopMotor();
delay (1000);
reverseMotor();
delay(2000);
leftfwdMotor();
delay(2000);
return;
}
void setup() {
servo1.attach(5); //Attaches the servo on pin 5 to the servo1 object
servo2.attach(4); //Attaches the servo on pin 4 to the servo2 object
bitSet(PCICR,PCIE2); // enable pin change interrupt bank 2
bitSet(PCMSK2, PCINT18); //enable pin change interrupt on PCINT18/D2
pinMode(bumpLeft, INPUT);
pinMode(bumpRight, INPUT);
bitSet(PCMSK2, PCINT22); //enable pin change interrupt on PCINT22/D6
bitSet(PCICR,PCIE0); // enable pin change interrupt bank 0
bitSet(PCMSK0, PCINT0); //enable pin change interrupt on PCINT0/D8
pinMode(speed1,OUTPUT);
pinMode(speed2,OUTPUT);
pinMode(direction1,OUTPUT);
pinMode(direction2,OUTPUT);
servo1.write(110); // Put servo1 at home position
servo2.write(100); // Put servo2 at home position
Serial.begin(9600);
Serial.flush();
}
void loop() {
if(motion == true){ // Sonar maneuver to avoid obstacle
Serial.println("Maneuvering");
maneuver();
motion = false;
}
if(bRight == true){ //Right bumper hit routine
Serial.println("Reversing");
reverseMotor();
delay(500);
Serial.println("Left Forward");
leftfwdMotor();
delay (1500);
bRight= false;
}
if(bLeft == true){ //Right bumper hit routine
Serial.println("Reversing");
reverseMotor();
delay(500);
Serial.println("Right Forward");
rightfwdMotor();
delay (1500);
bLeft = false;
}
Serial.println("Forward");
forwardMotor();
}
// Pin change interrupt handlers for bank 2
ISR(PCINT2_vect){
if(digitalRead(Sonar) == LOW){ //interrupt handler when obstacle within 10cm or less
motion = true;
}
if(digitalRead(bumpRight) == HIGH){ // Pin change Interrupt for right bumper
bRight = true;
}
}
// Pin change interrupt handlers for bank 0
ISR(PCINT0_vect){
if(digitalRead(bumpLeft) == HIGH){ //Pin change Interrupt for left bumper
bLeft = true;
}
}