-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_bot.h
158 lines (132 loc) · 4.36 KB
/
smart_bot.h
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
/*
* smart_bot.h
*
*
*
*/
#ifndef SMART_STATE_H
#define SMART_STATE_H
#include "DEBUGME.h"
#include "smart_motor.h"
#include "state_machine.h"
#include "csensor.h"
#include "color.h"
#include "lights.h"
void collision_detect ();
void hall_detect ();
void nothing ();
class smart_bot
{
public:
int routine;
lights leds;
int comms_sending;
int comms_receiving;
smart_motor rmotor;
smart_motor lmotor;
int mpinE; // motor enable
int hall_pin;
int collision_pin;
csensor sensors;
state_machine bot_state_machine;
bool hardware_on;
bool hall_interrupt;
bool collision_interrupt;
smart_bot(
// LEDS
int yellow_path_pin, int blue_path_pin, int red_path_pin,
int green_state_pin, int blue_state_pin, int red_state_pin,
int headlights_pin, int breaklights_pin,
int lturn_pin, int rturn_pin,
// MOTORS
int mpin1_R, int mpin2_R, float m1r,
int mpin1_L, int mpin2_L, float m2r, int mpinE,
int comms_sending, int comms_receiving,
// COLOR SENSORS
int sensor_pin_left, int sensor_pin_right,
int hall_pin, int collision_pin,
int red_pin, int blue_pin, int stabilization_time,
const Betas &redB_left , const Betas &bluB_left,
const Betas &redB_right, const Betas &bluB_right) :
routine(0),
leds (yellow_path_pin, blue_path_pin, red_path_pin,
green_state_pin, blue_state_pin, red_state_pin,
headlights_pin, breaklights_pin,
lturn_pin, rturn_pin),
comms_sending(comms_sending),
comms_receiving(comms_receiving),
rmotor(mpin1_R,mpin2_R,m1r),
lmotor(mpin1_L,mpin2_L,m2r), mpinE(mpinE),
hall_pin(hall_pin), collision_pin(collision_pin),
sensors(sensor_pin_left,sensor_pin_right,
red_pin, blue_pin, stabilization_time,
redB_left,bluB_left,redB_right,bluB_right),
hardware_on(true), hall_interrupt(0), collision_interrupt(0)
{pinMode (mpinE,OUTPUT);digitalWrite(mpinE,HIGH);}
template <int N>
void run_ (state_initializer (&si)[N],int routine)
{
this-> routine = routine;
bot_state_machine.setup(si,routine);
while (!bot_state_machine.complete()) {
if (hardware_on) {
poll_sensors();
} else {
Serial.println("HARDWARE OFF");
digitalWrite(mpinE,LOW);
rmotor.off();
lmotor.off();
sensors.off();
}
maintain_hardware();
bot_state_machine.execute();
}
}
void handle_hall_interrupt()
{
bot_state_machine.stack_push(HALL_INTERRUPT);
}
void handle_collision_interrupt()
{
bot_state_machine.stack_push(COLLISION_INTERRUPT);
}
void maintain_hardware ()
{
// maintains the motor behavior
rmotor.maintain();
lmotor.maintain();
if (rmotor.decelerating() && lmotor.decelerating()) leds.breaklights.on_solid();
leds.maintain();
leds.reset();
}
void show_following_path(color C)
{
switch (C) {
case RED:
leds.red_path.on_solid();
break;
case BLUE:
leds.blue_path.on_solid();
break;
case YELLOW:
leds.yellow_path.on_solid();
break;
}
}
void sending_on() {digitalWrite(comms_sending, HIGH);}
void sending_off() {digitalWrite(comms_sending, LOW );}
int receive () {return analogRead(comms_receiving);}
void poll_sensors () {sensors.sense();}
void disable_hall_interrupt () {attachInterrupt(digitalPinToInterrupt(hall_pin), nothing, FALLING);}
void disable_collision_interrupt () {attachInterrupt(digitalPinToInterrupt(collision_pin), nothing, FALLING);}
void enable_hall_interrupt ()
{
hall_interrupt = false;
attachInterrupt(digitalPinToInterrupt(hall_pin), hall_detect, FALLING);
}
void enable_collision_interrupt () {
collision_interrupt = false;
attachInterrupt(digitalPinToInterrupt(collision_pin), collision_detect, FALLING);
}
};
#endif