-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaincode.ino
231 lines (181 loc) · 3.88 KB
/
maincode.ino
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <ros.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Int64.h>
#include <robot_control/coord.h>
#include <robot_control/accelerometr.h>
#include <dynamixel_msgs/JointState.h>
#include <Wire.h>
#include <LSM303.h>
const int Xpin = 0;
const int motorPin = 12;
const int buttonPin = 13;
const int butPin = 3;
double X;
int a = 8;
int b = 9;
int c = 2;
int d = 11;
int e = 4;
int f = 6;
int g = 7;
int button = 0;
LSM303 compass;
char report[80];
int buttonState = 0;
ros::NodeHandle nh;
double pos1;
double goalpos1;
double prevPos1 = 5;
double diff = 0;
double prevdiff = 5;
int pos2;
int pos3;
int pos4;
int pos5;
int isMoving;
int N;
int j[5] = { 0 , 0 , 0, 0, 0 };
int l = 0;
int k = 0;
std_msgs::Int64 pos;
void numberCb(const std_msgs::Int64& msgN) {
N = msgN.data;
}
void isMovingCb(const std_msgs::Int64& msg) {
k = msg.data;
}
robot_control::coord coordinates;
robot_control::coord coo;
robot_control::accelerometr acceler;
ros::Publisher coord("coord", &coordinates);
ros::Publisher accel("accel", &acceler);
ros::Publisher but("isPressed", &pos);
ros::Subscriber<std_msgs::Int64> number("/motorN", numberCb);
ros::Subscriber<std_msgs::Int64> moving("/isStuck", isMovingCb);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Xpin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(butPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(2, OUTPUT);
pinMode(11, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
Wire.begin();
compass.init();
compass.enableDefault();
nh.initNode();
nh.subscribe(number);
nh.subscribe(moving);
nh.advertise(accel);
nh.advertise(coord);
nh.advertise(but);
}
void loop() {
// put your main code here, to run repeatedly:
j[l] = k;
l++;
if (l>4){
l = 0;
}
button = digitalRead(butPin);
analogWrite(motorPin, 0);
X = analogRead(Xpin);
buttonState = digitalRead(buttonPin);
if (j[0] == 1 && j[1] == 1 && j[2] == 1 && j[3] == 1 && j[4] == 1){
analogWrite(motorPin, 180);
}
compass.read();
snprintf(report, sizeof(report), "A: %6d %6d %6d",
compass.a.x, compass.a.y, compass.a.z);
coordinates.x = X;
coordinates.y = buttonState;
pos.data = button;
acceler.y = compass.a.y;
acceler.x = compass.a.z;
coo.x = j[0];
coo.y = j[1];
coo.vert = j[2];
accel.publish( &acceler);
but.publish( &pos);
coord.publish( &coordinates);
nh.spinOnce();
if (N == 0){
led1();
}else if(N == 1){
led2();
}else if(N == 2){
led3();
}else if(N == 3){
led4();
}else if(N == 4){
led5();
}
delay(1);
}
void led0(){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
void led1(){
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void led2(){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void led3(){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void led4(){
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void led5(){
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}