altimeter_publisher.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <iostream>
3 #include <ros/ros.h>
4 #include <nav_msgs/Odometry.h>
5 #include "../include/yocto/yocto_api/yocto_altitude.h"
6 #include "../include/yocto/yocto_api/yocto_api.h"
7 #include "../include/yocto/yocto_api/yocto_genericsensor.h"
8 #include "../include/yocto/yocto_api/yocto_temperature.h"
9 #include <yoctopuce_altimeter/yocto_msg.h>
10 
11 int main (int argc, char *argv[])
12 {
13  ros::init(argc, argv, "altimeter_pub_node");
14 
16 
17  ros::Publisher raw_pub = n.advertise<yoctopuce_altimeter::yocto_msg>("/yocto/raw", 1000);
18  ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("/yocto/odom", 1000);
19 
20  ros::Rate loop_rate(10);
21 
22  string errmsg, target;
23  YAltitude *asensor;
24  YTemperature *tsensor;
25 
26  target = "any";
27 
28  // Setup the API to use local USB devices
29  if (yRegisterHub("usb", errmsg) != YAPI_SUCCESS)
30  {
31  std::cerr << "RegisterHub error: " << errmsg << std::endl;
32  return 1;
33  }
34 
35  if (target == "any")
36  {
37  asensor = yFirstAltitude();
38  tsensor = yFirstTemperature();
39  if (asensor == NULL)
40  {
41  ROS_ERROR_STREAM("No module connected (check USB cable)" << std::endl);
42  return 1;
43  }
44  }
45  else {
46  asensor = yFindAltitude(target + ".altitude");
47  tsensor = yFindTemperature(target + ".temperature");
48  }
49 
50  while(ros::ok())
51  {
52  if(!asensor->isOnline() && !tsensor->isOnline()) {
53  ROS_ERROR_STREAM("Module not connected (check identification and USB cable)");
54  break;
55  }
56 
57  double alt = asensor->get_currentValue(); // m
58  double pressure = asensor->get_qnh(); // hPa
59  pressure = pressure * 100.0; // convert to Pa
60  double temp = tsensor->get_currentValue(); // C
61 
62  yoctopuce_altimeter::yocto_msg raw_msg;
63  nav_msgs::Odometry odom;
64 
65  raw_msg.header.frame_id = "base_link";
66  raw_msg.altitude = alt;
67  raw_msg.pressure.header.frame_id = "base_link";
68  raw_msg.pressure.fluid_pressure = pressure;
69  raw_msg.pressure.variance = 0;
70  raw_msg.temperature.header.frame_id = "base_link";
71  raw_msg.temperature.temperature = temp;
72  raw_msg.temperature.variance = 0;
73  raw_pub.publish(raw_msg);
74 
75  odom.header.frame_id = "odom";
76  odom.child_frame_id = "odom";
77  odom.pose.pose.position.z = alt;
78  odom_pub.publish(odom);
79 
80  ros::spinOnce();
81 
82  loop_rate.sleep();
83 
84  // Uncomment below to recieve debug information
85  // ROS_INFO_STREAM("Current altitude: " << asensor->get_currentValue() << " m (QNH= " << asensor->get_qnh() << " hPa)" << std::endl);
86 
87  ySleep(1000, errmsg);
88  }
89 
90  yFreeAPI();
91  return 0;
92 }
YAltitude * yFirstAltitude(void)
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
YTemperature * yFindTemperature(const string &func)
double get_qnh(void)
double get_currentValue(void)
Definition: yocto_api.cpp:7091
YRETCODE ySleep(unsigned ms_duration, string &errmsg)
Definition: yocto_api.h:3498
YAltitude * yFindAltitude(const string &func)
YRETCODE yRegisterHub(const string &url, string &errmsg)
Definition: yocto_api.h:3392
ROSCPP_DECL bool ok()
void yFreeAPI()
Definition: yocto_api.h:3255
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool isOnline(void)
Definition: yocto_api.cpp:3291
int main(int argc, char *argv[])
YTemperature * yFirstTemperature(void)
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:10