forked from solbach/ps4-ros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps4_ros.cpp
257 lines (215 loc) · 7.21 KB
/
ps4_ros.cpp
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
#include <string>
#include <ros/ros.h>
#include <sensor_msgs/Joy.h>
#include <geometry_msgs/Twist.h>
class PS4_ROS {
public:
/**
* @brief { PS4 to TWIST MESSAGES }
*
*/
PS4_ROS(ros::NodeHandle &n) {
// get ros param
ros::NodeHandle private_nh("~");
private_nh.param("scale_linear", this->scale_linear, 1.0);
private_nh.param("scale_angular", this->scale_angular, 1.0);
private_nh.param<std::string>("pub_topic", this->pubName, "/cmd_vel");
this->chat = n.advertise<geometry_msgs::Twist>(pubName, 1000);
this->sub = n.subscribe<sensor_msgs::Joy>("/joy", 10, &PS4_ROS::subscribePS4, this);
/* set calibration counter to zero */
this->calib1 = 0;
this->calib2 = 0;
this->calib = true;
this->maxVel = this->scale_linear;
this->maxVelR = this->scale_linear * -1;
ROS_INFO("maxVelR: %f", this->maxVelR);
ROS_INFO("scale_linear set to: %f", this->scale_linear);
ROS_INFO("scale_angular set to: %f", this->scale_angular);
ROS_INFO("PS4_ROS initialized");
}
~PS4_ROS() {
// std::cout << "Destroy the pointer" << std::endl;
}
void run() {
// if(this->calib) {
this->publishTwistMsg();
// }
}
void prepareData()
{
// Normalize velocity between ]-1.0, 1.0[
this->send_l2 = (-0.5 * this->l2) + 0.5;
this->send_r2 = (this->r2 - 1.0) * 0.5;
// // Apply rosparam "scale_linear"
// this->send_l2 = this->scale_linear * this->send_l2;
// this->send_r2 = this->scale_linear * this->send_r2;
//
// Apply rosparam "scale_angular"
// this->send_leftStickX = this->scale_linear * this->leftStickX;
// this->send_leftStickY = this->scale_linear * this->leftStickY;
// this->send_rightStickX = this->scale_angular * this->rightStickX;
this->send_leftStickX = this->leftStickX;
this->send_leftStickY = this->leftStickY;
this->send_rightStickX = this->rightStickX;
}
void publishTwistMsg() {
geometry_msgs::Twist msg;
msg.linear.x = 0.0;
msg.linear.y = 0.0;
msg.linear.z = 0.0;
msg.angular.x = 0.0;
msg.angular.y = 0.0;
msg.angular.z = 0.0;
prepareData();
//printRaw();
if(!this->buttonTouch) {
// if ((this->send_l2 >= 0.1) && (this->send_l2 <= maxVel)) {
// msg.linear.x = this->send_l2;
// } else if ((this->send_r2 <= 0.0) && (this->send_r2 >= maxVelR)) {
// msg.linear.x = this->send_r2;
// }
msg.linear.x = this->send_leftStickY * this->scale_linear;
// msg.linear.y = this->send_leftStickX;
msg.angular.z = this->send_rightStickX * this->scale_angular;
}
else{
//ROS_WARN("SENDING EMERGENCY STOP");
msg.linear.x = 0.0;
msg.linear.y = 0.0;
msg.linear.z = 0.0;
msg.angular.x = 0.0;
msg.angular.y = 0.0;
msg.angular.z = 0.0;
/* To Do */
}
this->chat.publish(msg);
}
void subscribePS4(const sensor_msgs::Joy::ConstPtr &joy) {
this->buttonSq = joy->buttons[0];
this->buttonX = joy->buttons[1];
this->buttonO = joy->buttons[2];
this->buttonTr = joy->buttons[3];
this->buttonTouch = joy->buttons[13];
this->l1 = joy->buttons[4];
this->r1 = joy->buttons[5];
this->arrowsX = joy->axes[9];
this->arrowsY = joy->axes[10];
this->l2 = joy->axes[3];
this->r2 = joy->axes[4];
this->leftStickX = joy->axes[0];
this->leftStickY = joy->axes[1];
this->rightStickX = joy->axes[2];
this->rightStickY = joy->axes[5];
//printRaw();
}
bool calibrate()
{
double progress = ((double) this->calib1 / this->calib_duration) * 100;
if( (this->l2 == -1.0) && (this->r2 == -1.0) )
{
ROS_WARN("Press L2 and R2 to calibrate: %i%% ", (int) progress);
this->calib1++;
this->calib2++;
}
else{
this->calib1 = 0;
this->calib2 = 0;
}
if( (this->calib1 > this->calib_duration) && (this->calib2 > this->calib_duration))
{
this->calib = true;
return true;
}
else
return false;
}
bool waitForRelease()
{
if( (this->l2 == 1.0) && (this->r2 == 1.0) )
{
return true;
}
else{
return false;
}
}
void printSend()
{
ROS_INFO("#####################################");
ROS_INFO("Send L2: %f", this->send_l2);
ROS_INFO("Send R2: %f", this->send_r2);
ROS_INFO("##################################### \n");
}
void printRaw()
{
ROS_INFO("#####################################");
ROS_INFO("Squared Button pressed: %i", this->buttonSq);
ROS_INFO("X Button pressed: %i", this->buttonX);
ROS_INFO("O Button pressed: %i", this->buttonO);
ROS_INFO("Triangel Button pressed: %i", this->buttonTr);
ROS_INFO("Left/Right Button pressed: %i", this->arrowsX);
ROS_INFO("Down/Up Button pressed: %i", this->arrowsY);
ROS_INFO("Touch Button pressed: %i", this->buttonTouch);
ROS_INFO("L1: %i", this->l1);
ROS_INFO("R1: %i", this->r1);
ROS_INFO("L2: %f", this->l2);
ROS_INFO("R2: %f", this->r2);
ROS_INFO("Left Stick Y: %f", this->leftStickY);
ROS_INFO("Left Stick X: %f", this->leftStickX);
ROS_INFO("Right Stick Y: %f", this->rightStickY);
ROS_INFO("Right Stick X: %f", this->rightStickX);
ROS_INFO("##################################### \n");
}
private:
ros::Publisher chat;
ros::Subscriber sub;
/* calibration variables */
int calib_duration = 20; // 1/10 sec
int calib1, calib2;
bool calib;
/* raw data */
double leftStickY, leftStickX, rightStickY, rightStickX, l2, r2;
int arrowsX, arrowsY, buttonSq, buttonX, buttonO, buttonTr,
buttonTouch, l1, r1;
/* rosparams */
double scale_linear, scale_angular;
std::string pubName;
double maxVel, maxVelR;
/* send data */
double send_leftStickX, send_leftStickY, send_rightStickX, send_l2, send_r2;
};
int main(int argc, char **argv) {
ros::init(argc, argv, "PS4_ROS");
ros::NodeHandle n;
// create ps4_ros object
PS4_ROS *ps4_ros = new PS4_ROS(n);
// // calibrate
// ROS_WARN("Press L2 and R2 to calibrate");
// bool ready = false;
// while(!ready)
// {
// ready = ps4_ros->calibrate();
// ros::spinOnce();
// ros::Duration(0.1).sleep();
// }
//
// ROS_WARN("Release L2 and R2");
// ros::Duration(2.0).sleep();
// ready = false;
// while(!ready)
// {
// ready = ps4_ros->waitForRelease();
// ros::spinOnce();
// ros::Duration(0.1).sleep();
// }
// ROS_INFO("Calibrated - Ready to use");
ros::Rate loop_rate(10);
while(ros::ok())
{
ps4_ros->run();
ros::spinOnce();
loop_rate.sleep();
}
delete ps4_ros;
return 0;
}