-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTPV_Controller.ino
180 lines (139 loc) · 4.12 KB
/
TPV_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
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
#include <stdlib.h>
#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/UInt16.h>
#include <std_msgs/Float64.h>
#include <Servo.h>
#define KP -0.2
#define G_X 0.7
#define G_Y 0.7
//Command to send
float cmd1 = 0;
float cmd2 = 0;
//Live position of servos
float pos1 = 0;
float pos2 = 0;
ros::NodeHandle nh;
Servo servo1;
Servo servo2;
long lasttime;
void messageServo1(const std_msgs::Float64& toggle_msg){
cmd1 = toggle_msg.data;
}
void messageServo2(const std_msgs::Float64& toggle_msg){
cmd2 = toggle_msg.data;
}
std_msgs::Float64 tpv_pos_x;
std_msgs::Float64 tpv_pos_y;
ros::Subscriber<std_msgs::Float64> sub1("tpv_x", &messageServo1);
ros::Subscriber<std_msgs::Float64> sub2("tpv_y", &messageServo2);
ros::Publisher pub1("tpv_pos_x", &tpv_pos_x);
ros::Publisher pub2("tpv_pos_y", &tpv_pos_y);
float angle1, angle2;
void setup()
{
servo1.attach(5);
servo2.attach(6);
nh.getHardware()->setBaud(57600);
nh.initNode();
nh.subscribe(sub1);
nh.subscribe(sub2);
nh.advertise(pub1);
nh.advertise(pub2);
attachInterrupt(digitalPinToInterrupt(2), interupt1, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), interupt2, CHANGE);
}
int unitsFC = 360; // Units in a full circle
int dutyScale = 1000; // Scale duty cycle to 1/1000ths
int dcMin = 31; // Minimum duty cycle
int dcMax = 971; // Maximum duty cycle
int q2min = unitsFC/4; // For checking if in 1st quadrant
int q3max = q2min * 3; // For checking if in 4th quadrant
int turns1 = 0;
unsigned long rise1, fall1, tLow1, tHigh1;
unsigned long rise2, fall2, tLow2, tHigh2;
float thetaPre1, thetaPre2;
int turns2 = 0;
void loop()
{
//----------------------------------------------------------------
if(1){
int tCycle = tHigh1 + tLow1;
if((tCycle > 1000) && (tCycle < 1200)){
float dc = (dutyScale * tHigh1) / (float)tCycle;
float theta = (unitsFC - 1) - ((dc - dcMin) * unitsFC) / (dcMax - dcMin + 1);
if(theta < 0.0)
theta = 0.0;
else if(theta > (unitsFC - 1.0))
theta = unitsFC - 1.0;
if((theta < q2min) && (thetaPre1 > q3max))
turns1++;
else if((thetaPre1 < q2min) && (theta > q3max))
turns1--;
if(turns1 >= 0)
angle1 = (turns1 * unitsFC) + theta;
else if(turns1 < 0)
angle1 = ((turns1 + 1) * unitsFC) - (unitsFC - theta);
thetaPre1 = theta;
}
}
//----------------------------------------------------------------
if(1){
int tCycle = tHigh2 + tLow2;
if((tCycle > 1000) && (tCycle < 1200)){
float dc = (dutyScale * tHigh2) / (float)tCycle;
float theta = (unitsFC - 1) - ((dc - dcMin) * unitsFC) / (dcMax - dcMin + 1);
if(theta < 0.0)
theta = 0.0;
else if(theta > (unitsFC - 1.0))
theta = unitsFC - 1.0;
if((theta < q2min) && (thetaPre2 > q3max))
turns2++;
else if((thetaPre2 < q2min) && (theta > q3max))
turns2--;
if(turns1 >= 0)
angle2 = (turns2 * unitsFC) + theta;
else if(turns1 < 0)
angle2 = ((turns2 + 1) * unitsFC) - (unitsFC - theta);
thetaPre2 = theta;
}
}
//-------------------------------------------------------------------
if(millis() > 200){
pos1 = pos1 + G_X*cmd1*(millis() - lasttime);
pos2 = pos2 + G_Y*cmd2*(millis() - lasttime);
servo1.write(constrain(((angle1-pos1)*KP) + 90, 0 , 180));
servo2.write(constrain(((angle2-pos2)*KP) + 90, 0 , 180));
tpv_pos_x.data = angle1;
pub1.publish(&tpv_pos_x);
tpv_pos_y.data = angle2;
pub1.publish(&tpv_pos_y);
lasttime = millis();
nh.spinOnce();
delay(1);
}
else{
pos1 = angle1;
pos2 = angle2;
}
}
void interupt1(){
if(digitalRead(2)) {
rise1 = micros();
tLow1 = rise1 - fall1;
}
else{
fall1 = micros();
tHigh1 = fall1 - rise1;
}
}
void interupt2(){
if(digitalRead(3)) {
rise2 = micros();
tLow2 = rise2 - fall2;
}
else{
fall2 = micros();
tHigh2 = fall2 - rise2;
}
}