Skip to content

Commit 3e50df0

Browse files
Add source code
TASK.md contains the task that was given to me for this assignment.
1 parent fdce111 commit 3e50df0

File tree

3 files changed

+296
-0
lines changed

3 files changed

+296
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# ce243_emoro
22
Emoro Robot Source Code for my CE243 Assignment 2
3+
Check out the video here!

TASK.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
To design and write a C program to control an EMoRo robot. Your program should use the
2+
push buttons on its GLAM board to input commands and use its LCD to display messages.
3+
The functions of four buttons are specified below:
4+
- SW_1: Forward motion and Obstacle avoidance
5+
- SW_2: Line following (move along a circle track)
6+
- SW_3: Line following (move along a square track)
7+
- SW_4: Stop the motion of the robot and reset the computer for next button input.
8+
Button: The input to your program will be the four push buttons, SW_1, SW_2, SW_3 and
9+
SW_4. These buttons will trigger the EMoRo robot to implement different tasks.
10+
More specifically,
11+
Pressing SW_1 triggers the EMoRo robot to move forward and avoid any obstacle
12+
detected by its on-board sonar sensors.
13+
Pressing SW_2 triggers the EMoRo robot to move along a circle track which is
14+
very smooth.
15+
Pressing SW_3 triggers the EMoRo robot to move along a square track that has
16+
four sharp turns at 90 degrees.
17+
Pressing SW_4 will stop the movement of the EMoRo robot and reset the
18+
embedded computer for next button input.
19+
LCD: All messages should be displayed on the LCD device on the robot.
20+
1) When the robot is switched on or after SW_4 is pressed, the LED should
21+
display the following message:
22+
1. Obst 2. Circle 3. Square 4. Reset
23+
2) After SW_1 is pressed, the LCD should display moving forward. When an
24+
obstacle is detected, the LCD should display obstacle detected.
25+
3) After SW_2 is pressed, the LCD should display Circle track following.
26+
4) After SW_3 is pressed, the LCD should display Square track following. When
27+
the robot is turning at the corners of the square track, the LCD shows Sharp
28+
turning *** degrees. When the robot moves along the straight line sections,
29+
the LCD shows Square track following.
30+
Libraries: You should use the EMoRo2560 library provided in Moodle to write your C
31+
program.

main.ino

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
// LCD Menu items
2+
char obstacle[16] = "1.Obst";
3+
char circle[16] = "2.Circle";
4+
char squ[16] = "3.Square";
5+
char reset[16] = "4.Reset";
6+
7+
unsigned char ReadSwitch(unsigned char sw);
8+
9+
// Declare variables
10+
int servoPort[2] = {SERVO_0, SERVO_1};
11+
int waitState = 1000;
12+
int lServ = 2000;
13+
int rServ = 1000;
14+
int sensorPin = GPP_0;
15+
int cm;
16+
static int active = -1; // Check button pressed constant
17+
18+
// Values for moving bot back or forward
19+
static int Lbck = 2500;
20+
static int Rbck = 1000;
21+
static int Lfwd = 1000;
22+
static int Rfwd = 2000;
23+
24+
25+
void setup() {
26+
27+
// Initialise serial, servos, servos and IR sensors
28+
Serial.begin(9600);
29+
EmoroServo.attach(servoPort[0]);
30+
EmoroServo.attach(servoPort[1]);
31+
Ultrasonic.attach(sensorPin);
32+
33+
pinMode(IO_0, INPUT_PULLUP);
34+
pinMode(IO_1, INPUT_PULLUP);
35+
36+
// Call mainMenu function to print the main menu
37+
mainMenu();
38+
}
39+
40+
void loop() {
41+
// Read sensor pin distance in CM
42+
cm = Ultrasonic.read(sensorPin);
43+
if (ReadEmoroHardware() & SW_AVAILABLE) {
44+
// Check if switches are pressed, if pressed, set active to x
45+
if (ReadSwitch(SW_1) == 1)
46+
{
47+
active = 1;
48+
}
49+
else if (ReadSwitch(SW_2) == 1)
50+
{
51+
active = 2;
52+
}
53+
else if (ReadSwitch(SW_3) == 1)
54+
{
55+
active = 3;
56+
}
57+
else if (ReadSwitch(SW_4) == 1)
58+
{
59+
active = 4;
60+
}
61+
}
62+
63+
if (active == 1) {
64+
Serial.println("Obst");
65+
Lcd.clear();
66+
Lcd.locate(0, 0);
67+
Lcd.print("Moving...");
68+
moveBot();
69+
70+
// Debug
71+
Serial.print("Servo pulse width: ");
72+
Serial.print(EmoroServo.read(servoPort[0]));
73+
Serial.print(" us\n");
74+
Serial.print("Servo pulse width: ");
75+
Serial.print(EmoroServo.read(servoPort[1]));
76+
Serial.print(" us\n");
77+
Serial.println("Moving...");
78+
79+
// Calls the objDetected function that stops bot
80+
// Then moves away from the object
81+
objDetected();
82+
}
83+
else if (active == 2) {
84+
Serial.println("Switch SW_2 is activated");
85+
Lcd.clear();
86+
Lcd.locate(0, 0);
87+
Lcd.print("Circle Track");
88+
Lcd.locate(1, 0);
89+
Lcd.print("Following");
90+
91+
doCircle();
92+
}
93+
else if (active == 3) {
94+
Serial.println("Switch SW_3 is activated");
95+
Lcd.clear();
96+
Lcd.locate(0, 0);
97+
Lcd.print("Square Track");
98+
Lcd.locate(1, 0);
99+
Lcd.print("Following");
100+
101+
doSquare();
102+
}
103+
else if (active == 4) {
104+
Serial.println("Robot Reset");
105+
stopBot();
106+
Lcd.locate(0, 0);
107+
Lcd.print("Resetting...");
108+
Lcd.clear();
109+
active = -1;
110+
111+
mainMenu();
112+
}
113+
}
114+
115+
/*********************
116+
* EMORO FUNCTIONS *
117+
*********************/
118+
119+
// Function to enable return to main menu
120+
void mainMenu()
121+
{
122+
if (ReadEmoroHardware() & LCD_AVAILABLE) { // if LCD and switch avaliable
123+
Serial.println("LCD Ready");
124+
Lcd.locate(0, 0);
125+
Lcd.print(obstacle);
126+
Lcd.locate(0, 8);
127+
Lcd.print(circle);
128+
Lcd.locate(1, 0);
129+
Lcd.print(squ);
130+
Lcd.locate(1, 9);
131+
Lcd.print(reset);
132+
} else {
133+
Serial.println("LCD Not Ready");
134+
}
135+
}
136+
137+
// Function to stop the bot
138+
void stopBot()
139+
{
140+
// Set servo pulse to 1500us. For position servo motors this will set the middle position,
141+
// for servo motors with continuous rotation this will stop rotation
142+
EmoroServo.write(servoPort[0], 1500);
143+
EmoroServo.write(servoPort[1], 1500);
144+
}
145+
146+
// Function to move bot
147+
void moveBot()
148+
{
149+
// Set servo pulse to 1000us. For position servo motors this will set the left position,
150+
// for servo motors with continuous rotation this will set continuous rotation in CCW direction
151+
EmoroServo.write(servoPort[0], lServ);
152+
EmoroServo.write(servoPort[1], rServ);
153+
}
154+
155+
// Function to detect obstacles
156+
void objDetected()
157+
{
158+
cm = Ultrasonic.read(sensorPin);
159+
// Stop robot if less than 25cm
160+
if (cm <= 25)
161+
{
162+
Serial.println("Object Detected!");
163+
Lcd.clear();
164+
Lcd.locate(2, 0);
165+
Lcd.print("Object Detected!");
166+
167+
// Calls stopBot function to stop bot if less than 25cm
168+
stopBot();
169+
delay(85);
170+
// Moves the bot back
171+
EmoroServo.write(servoPort[0], 500);
172+
EmoroServo.write(servoPort[1], 2500);
173+
delay(200);
174+
// Turns bot
175+
EmoroServo.write(servoPort[0], 500);
176+
EmoroServo.write(servoPort[1], 500);
177+
delay(250);
178+
// Moves forward and away form the object
179+
EmoroServo.write(servoPort[0], lServ);
180+
EmoroServo.write(servoPort[1], rServ);
181+
//delay(100);
182+
}
183+
}
184+
185+
// Function for following circle
186+
void doCircle()
187+
{
188+
// If Both sensors detect white, go forward.
189+
if (digitalRead(IO_0) == 1 && digitalRead(IO_1) == 1) {
190+
EmoroServo.write(servoPort[0], Lfwd);
191+
EmoroServo.write(servoPort[1], Rfwd);
192+
Lcd.locate(0, 0);
193+
Serial.print("White");
194+
Lcd.print("White");
195+
Lcd.locate(1, 0);
196+
Lcd.print(digitalRead(IO_0));
197+
Lcd.locate(1, 2);
198+
Lcd.print(digitalRead(IO_1));
199+
}
200+
// Only left sensor is on the black, turn right.
201+
else if (digitalRead(IO_0) == 1) {
202+
EmoroServo.write(SERVO_0, Lbck);
203+
EmoroServo.write(SERVO_1, Rfwd);
204+
Lcd.locate(0, 0);
205+
Lcd.print("Left Sensor");
206+
Lcd.locate(1, 0);
207+
Lcd.print(digitalRead(IO_0));
208+
Lcd.locate(1, 2);
209+
Lcd.print(digitalRead(IO_1));
210+
delay(10);
211+
}
212+
// Only right sensor is on the black, turn left.
213+
else if (digitalRead(IO_1) == 1) {
214+
EmoroServo.write(SERVO_0, Lfwd);
215+
EmoroServo.write(SERVO_1, Rbck);
216+
Lcd.locate(0, 0);
217+
Lcd.print("Right Sensor");
218+
Lcd.locate(1, 0);
219+
Lcd.print(digitalRead(IO_0));
220+
Lcd.locate(1, 2);
221+
Lcd.print(digitalRead(IO_1));
222+
delay(10);
223+
}
224+
}
225+
// Function for following square
226+
void doSquare()
227+
{
228+
// If Both sensors detect white, go forward.
229+
if (digitalRead(IO_0) == 1 && digitalRead(IO_1) == 1) {
230+
EmoroServo.write(servoPort[0], Lfwd);
231+
EmoroServo.write(servoPort[1], Rfwd);
232+
Lcd.locate(0, 0);
233+
Serial.print("White");
234+
Lcd.print("White");
235+
Lcd.locate(1, 0);
236+
Lcd.print(digitalRead(IO_0));
237+
Lcd.locate(1, 2);
238+
Lcd.print(digitalRead(IO_1));
239+
}
240+
// Only left sensor is on the black, turn 90 deg right.
241+
else if (digitalRead(IO_0) == 1) {
242+
EmoroServo.write(SERVO_0, Lbck);
243+
EmoroServo.write(SERVO_1, Rfwd);
244+
delay(500);
245+
Lcd.locate(0, 0);
246+
Lcd.print("Left Sensor");
247+
Lcd.locate(1, 0);
248+
Lcd.print(digitalRead(IO_0));
249+
Lcd.locate(1, 2);
250+
Lcd.print(digitalRead(IO_1));
251+
}
252+
// Only right sensor is on the black, turn 90 deg left.
253+
else if (digitalRead(IO_1) == 1) {
254+
EmoroServo.write(SERVO_0, Lfwd);
255+
EmoroServo.write(SERVO_1, Rbck);
256+
delay(500);
257+
Lcd.locate(0, 0);
258+
Lcd.print("Right Sensor");
259+
Lcd.locate(1, 0);
260+
Lcd.print(digitalRead(IO_0));
261+
Lcd.locate(1, 2);
262+
Lcd.print(digitalRead(IO_1));
263+
}
264+
}

0 commit comments

Comments
 (0)