38 #include <std_msgs/String.h> 44 #include <sensor_msgs/JointState.h> 45 #include "phidgets_high_speed_encoder/EncoderDecimatedSpeed.h" 75 const int serial_number = Phidget::getDeviceSerialNumber();
76 const int version = Phidget::getDeviceVersion();
77 const int num_encoders = Encoder::getEncoderCount();
78 const std::string dev_type = Phidget::getDeviceType();
79 const int num_inputs = Encoder::getInputCount();
81 ROS_INFO(
"Device type : %s", dev_type.c_str());
82 ROS_INFO(
"Device serial : %d", serial_number);
83 ROS_INFO(
"Device version : %d", version);
84 ROS_INFO(
"Number of encoders: %d", num_encoders);
85 ROS_INFO(
"Number of inputs : %d", num_inputs);
91 const int serial_number = Phidget::getDeviceSerialNumber();
92 const std::string name = Phidget::getDeviceName();
93 const int inputcount = Encoder::getEncoderCount();
99 ROS_INFO(
"%s Serial number %d attached with %d encoders!",
100 name.c_str(), serial_number, inputcount);
103 for (
int i = 0 ; i < inputcount ; i++)
104 Encoder::setEnabled(i,
true);
109 const int serial_number = Phidget::getDeviceSerialNumber();
110 const std::string name = Phidget::getDeviceName();
111 ROS_INFO(
"%s Serial number %d detached!",
112 name.c_str(), serial_number);
117 ROS_ERROR(
"Error handled. %d - %s", ErrorCode,
118 Phidget::getErrorDescription(ErrorCode).c_str());
123 const int Position = Encoder::getPosition(index);
124 ROS_DEBUG(
"Encoder %d Count %d", index, Position);
146 int main(
int argc,
char *argv[])
148 ros::init(argc, argv,
"phidgets_high_speed_encoder");
152 std::vector<std::string> joint_names = {
"joint0",
"joint1",
"joint2",
"joint3" };
153 std::vector<double> joint_tick2rad = { 1.0, 1.0, 1.0, 1.0 };
154 for (
unsigned int i = 0; i < joint_names.size(); i++)
157 sprintf(str,
"joint%u_name", i);
160 sprintf(str,
"joint%u_tick2rad", i);
161 nh.
getParam(str, joint_tick2rad[i]);
163 ROS_INFO(
"Channel %u: '%s'='%s'", i, str, joint_names[i].c_str());
166 int serial_number = -1;
167 if (serial_number == -1)
169 nh.
getParam(
"serial_number", serial_number);
172 std::string frame_id;
174 ROS_INFO(
"frame_id = '%s'", frame_id.c_str());
180 const std::string topic_path =
"joint_states";
181 ROS_INFO(
"Publishing state to topic: %s", topic_path.c_str());
184 std::vector<ros::Publisher> encoder_decimspeed_pubs;
190 encoder_node.
open(serial_number);
193 if (serial_number == -1)
195 ROS_INFO(
"Waiting for any High Speed Encoder Phidget to be attached.");
199 ROS_INFO(
"Waiting for High Speed Encoder Phidget %d to be attached....",
208 ROS_ERROR(
"Problem waiting for attachment: %s", sError.c_str());
230 if (encoder_decimspeed_pubs.size() != N)
232 encoder_decimspeed_pubs.resize(N);
233 for (
unsigned int i = 0; i < N; i++)
235 std::string
s = topic_path;
237 sprintf(buf,
"_ch%u_decim_speed", i);
239 ROS_INFO(
"Publishing decimated speed of channel %u to topic: %s", i, s.c_str());
240 encoder_decimspeed_pubs[i] =
241 n.
advertise<phidgets_high_speed_encoder::EncoderDecimatedSpeed>(
247 sensor_msgs::JointState js_msg;
248 static uint32_t seq_cnt = 0;
249 js_msg.header.seq = (seq_cnt++);
251 js_msg.header.frame_id = frame_id;
253 js_msg.name.resize(N);
254 for (
unsigned int i = 0; i < std::min<size_t>(joint_names.size(), N); i++)
255 js_msg.name[i] = joint_names[i];
257 js_msg.position.resize(N);
258 js_msg.velocity.resize(N);
259 js_msg.effort.clear();
261 for (
unsigned int i = 0; i < N; i++)
265 js_msg.position[i] = spc.
tickPos * joint_tick2rad[i];
276 phidgets_high_speed_encoder::EncoderDecimatedSpeed e;
278 e.header.frame_id = frame_id;
280 encoder_decimspeed_pubs[i].publish(e);
292 phidgets_high_speed_encoder::EncoderDecimatedSpeed e;
294 e.header.frame_id = frame_id;
295 e.avr_speed = avrg * joint_tick2rad[i];
296 encoder_decimspeed_pubs[i].publish(e);
302 encoder_pub.publish(js_msg);
std::vector< double > speeds_buffer
void detachHandler() override
unsigned int loops_without_update_speed_buffer
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool speed_buffer_updated
int open(int serial_number)
void display_properties()
int waitForAttachment(int timeout)
static std::string getErrorDescription(int errorCode)
void indexHandler(int index, int indexPosition) override
void attachHandler() override
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int SPEED_FILTER_IDLE_ITER_LOOPS_BEFORE_RESET
int main(int argc, char *argv[])
void errorHandler(int ErrorCode) override
std::vector< TStatePerChannel > encoder_states
bool getParam(const std::string &key, std::string &s) const
double instantaneousSpeed
int SPEED_FILTER_SAMPLES_LEN
void positionChangeHandler(int index, int time, int positionChange) override
std::mutex encoder_states_mux
ROSCPP_DECL void spinOnce()