-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.ino
69 lines (64 loc) · 1.63 KB
/
controller.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
#include <Servo.h>
Servo xservo;
Servo yservo;
#define trigPin 3
#define echoPin 5
#define gnd 7
////////////////////////variables
long x;
int y;
int posx = 0;
int posy = 0;
///////////////////setup
void setup() {
// Define inputs and outputs:
xservo.attach(10);
yservo.attach(11);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
}
///////////////////////////////main
void loop() {
String x1;
String y1;
if(Serial.available()){
x1 = Serial.readStringUntil(':');
int x = x1.toInt();
// Serial.println("x:");
// Serial.println(x1);
y1 = Serial.readStringUntil('$');
int y = y1.toInt();
// Serial.println("y:");
// Serial.println(y);
x=x-369;
y=y-261;
if(x<500 && y<500){
/////////////////////////pid
const float targetx = 0;
const float targety = 0;
static float xlastError = 0;
static float xintegral = 0;
static float ylastError = 0;
static float yintegral = 0;
float xerror = x - targetx;
float yerror = y - targety;
xintegral += xerror;
yintegral += yerror;
float xerrorDifference = xerror - xlastError;
float yerrorDifference = yerror - ylastError;
posx = 97-0.05*(xerror * 1.5+ xerrorDifference * 20+ xintegral *0);
posy = 82+0.05*(yerror * 1.5+ yerrorDifference * 20+ yintegral *0);
posx = constrain(posx, 87, 117);
posy = constrain(posy, 72,92);
// Serial.print(posx);
// Serial.print(posy);
xservo.write(posx);
yservo.write(posy);// tell servo to go to position in variable 'pos'
xlastError = xerror;
ylastError = yerror;
// Serial.println("x, y");
// Serial.println(x);
// Serial.println(y);
}
}
}