-
Notifications
You must be signed in to change notification settings - Fork 1
/
sweep_node.cpp
72 lines (57 loc) · 1.55 KB
/
sweep_node.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
#include <ros/ros.h>
#include <std_msgs/Header.h>
#include <std_msgs/Float64.h>
#include <dynamixel_msgs/JointState.h>
#include <tf/transform_datatypes.h>
#include <tf/transform_broadcaster.h>
#define ID 1
#define PI 1.570796327
#define CORR -0.15
using namespace ros;
bool up;
Publisher* gCommandPublisher;
tf::TransformBroadcaster* gTfBroadcaster;
const double min = -PI + CORR;
const double max = PI + CORR;
void statusHandler(const dynamixel_msgs::JointStateConstPtr& jointState)
{
// Send Transform
double angle = jointState->current_pos - CORR;
tf::Transform transform(tf::createQuaternionFromRPY(angle, 0, 0));
gTfBroadcaster->sendTransform(tf::StampedTransform(transform, jointState->header.stamp, "dxl_base", "dxl_rotor"));
// Send command
if(!up && jointState->current_pos < min)
{
std_msgs::Float64 cmd;
cmd.data = max + 0.1;
gCommandPublisher->publish(cmd);
up = true;
}else if(up && jointState->current_pos > max)
{
std_msgs::Float64 cmd;
cmd.data = min - 0.1;
gCommandPublisher->publish(cmd);
up = false;
}
}
int main(int argc, char **argv)
{
init(argc, argv, "Sweep");
NodeHandle n;
tf::TransformBroadcaster tfbc;
gTfBroadcaster = &tfbc;
Subscriber statusSubscriber = n.subscribe("status", 10, statusHandler);
Publisher commandPublisher = n.advertise<std_msgs::Float64>("command", 10);
gCommandPublisher = &commandPublisher;
Rate r(1);
while(commandPublisher.getNumSubscribers() == 0)
{
r.sleep();
}
std_msgs::Float64 cmd;
cmd.data = max + 0.1;
commandPublisher.publish(cmd);
up = true;
spin();
return 0;
}