-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor_test.ino
308 lines (236 loc) · 5.81 KB
/
motor_test.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 15
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
unsigned long tag_on_sensor_time, previous_tag_on_sensor_time;
//motor A connected between A01 and A02
//motor B connected between B01 and B02
#define POS_MIN 20 //LOCK position
#define POS_MAX 450 //UNLOCK position
#define OUT_LIMIT POS_MIN
#define IN_LIMIT POS_MAX
#define RANGE POS_MAX - POS_MIN
#define MARGIN 5 //RANGE/10
#define ACTUATOR_SPEED 255
#define DIRECTION_OUT 1
#define DIRECTION_IN 0
#define STATE_STOPPED 1
#define STATE_CLOSED 2
#define STATE_RUNNING 3
#define STATE_OPENED 4
int STBY = 7; //standby
int current_state;
int button_old;
#define BUTTON_IN 2
long int time_started;
long int delta;
#define MAX_RUNNING_TIME_MS 1000
#define KEEP_CLUTCH_ON 0
//////////////// KEYS DEFINITION //////
String keys_table[] = {
"55 79 0B AB",
"FA 1A 90 AB",
"41 8E DC 2B",
"B3 2E DC 2B",
"70 72 DC 2B",
"FA 62 DC 2B"
};
#define KEYTABLESIZE 6
//////////////// END KEYS DEFINITION //////
//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction
//Motor B
int PWMB = 5; //Speed control
int BIN1 = 4; //Direction
int BIN2 = 6; //Direction
int position_in = A6;
int position = 0;
int command = DIRECTION_OUT;
void move(int motor, int speed, int direction);
boolean limits_ok(int direction);
void setup(){
pinMode (BUTTON_IN, INPUT);
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
Serial.begin(9600);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
current_state= STATE_STOPPED;
previous_tag_on_sensor_time = 0;
button_old = 0;
}
void loop(){
//Serial.print(digitalRead(BUTTON_IN));
//Serial.println();
RunProgram( DetectTag()|ButtonPressed() );
//limits_ok(0);
return;
}
boolean ButtonPressed()
{
int button = digitalRead(BUTTON_IN);
if ((!button_old) && button)
{
Serial.print ("pressed");
Serial.println();
button_old = button;
return true;
}
button_old = button;
return false;
}
boolean DetectTag()
{
if ( mfrc522.PICC_IsNewCardPresent())
{
mfrc522.PICC_ReadCardSerial();
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
//Serial.print("Message : ");
content.toUpperCase();
if (KeyIsKnown(content.substring(1)))
{
tag_on_sensor_time = millis();
}
}
else {
previous_tag_on_sensor_time = tag_on_sensor_time;
return false;
}
unsigned long delta = tag_on_sensor_time - previous_tag_on_sensor_time;
if (delta > 100)
{
Serial.println();
Serial.print("CLICK ");
previous_tag_on_sensor_time = tag_on_sensor_time;
return true;
}
return false;
}
void RunProgram(bool click)
{
//keep clutch on
//if (KEEP_CLUTCH_ON)
// move(2, 255, 1); //motor 2, full speed, left
switch (current_state)
{
case STATE_STOPPED :
//digitalWrite(STBY, LOW); //disable motors
//clutch off
move(2, 255, 0); //motor 2, full speed, left
move(1, 0, 1); //motor 1, stop,
if (click)
{
current_state= STATE_RUNNING;
command = !command;
time_started = millis();
}
break;
case STATE_RUNNING :
//clutch on
move(2, 255, 1); //motor 2, full speed, left
//timeout
delta = millis()-time_started;
//Serial.print("delta :");
//Serial.println(delta);
if ( delta >= MAX_RUNNING_TIME_MS)
{
current_state= STATE_STOPPED;
break;
}
if (!limits_ok(command))
{
//command = !command;
move(1, 0, 1); //motor 1, stop,
current_state= STATE_STOPPED;
}else
move(1, ACTUATOR_SPEED, command); //motor 1, half speed,
break;
}
}
bool KeyIsKnown (String received_key)
{
int i = 0;
for (i = 0; i < KEYTABLESIZE; i++)
if (received_key == keys_table[i])
return true;
return false;
}
/*
int StateEvaluate()
{
position = analogRead(A6);
Serial.println (position);
if (!motor_running)
{
if (position <= POS_MIN)
return STATE_CLOSED;
else if (position >= POS_MAX)
return STATE_OPENED;
else
return STATE_STOPPED;
}
else
return STATE_RUNNING;
}
*/
void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void stop(){
//enable standby
digitalWrite(STBY, LOW);
}
boolean limits_ok(int direction)
{
position = analogRead(A6);
Serial.print ("position:");
Serial.println (position);
if ((direction ==1)&& (position <= POS_MIN))
{ Serial.println("Lower limit STOP");
return false;
}
if ((direction ==0)&& (position >= POS_MAX))
{
Serial.println("Higher limit STOP");
return false;
}
return true;
}