marty_core.hpp
Go to the documentation of this file.
1 
9 #ifndef MARTY_CORE_HPP
10 #define MARTY_CORE_HPP
11 
12 // System
13 #include <deque>
14 #include <fstream>
15 #include <sstream>
16 
17 // ROS
18 #include <ros/ros.h>
19 #include <ros/package.h>
20 #include <tf2_ros/transform_broadcaster.h>
21 #include <tf2_ros/transform_listener.h>
22 #include <tf2/LinearMath/Quaternion.h>
23 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
24 
25 // Messages
26 #include <std_msgs/Bool.h>
27 #include <std_msgs/String.h>
28 #include <std_msgs/Float32.h>
29 #include <std_srvs/SetBool.h>
30 #include <sensor_msgs/JointState.h>
31 #include <nav_msgs/Odometry.h>
32 #include <marty_msgs/Accelerometer.h>
33 #include <marty_msgs/GPIOs.h>
34 #include <marty_msgs/MotorCurrents.h>
35 #include <marty_msgs/Output.h>
36 #include <marty_msgs/ServoMsg.h>
37 #include <marty_msgs/ServoMsgArray.h>
38 #include <marty_msgs/GPIOConfig.h>
39 #include <marty_msgs/Sound.h>
40 #include <marty_msgs/SoundArray.h>
41 #include <marty_msgs/Keyframe.h>
42 #include <marty_msgs/KeyframeArray.h>
43 #include <geometry_msgs/TransformStamped.h>
44 
45 // MARTY
47 
48 struct robotJoint {
49  int cmdZero;
50  int cmdMin;
51  int cmdMax;
52  int cmdDir;
53  float cmdMult;
55 };
56 
57 class MartyCore {
58  protected:
59  ros::NodeHandle nh_;
60  // NodeHandle instance must be created before this line.
61  // Otherwise strange error may occur.
62 
63  void loadParams();
64  void init();
65  void rosSetup();
66 
67  public:
68  MartyCore(ros::NodeHandle& nh);
69  ~MartyCore();
70  void loadSound(std::string sound_name);
71  void loadKeyframes(std::string keyframes_name, int move_time = 0);
72  void readySound();
73  void celebSound(float sound_time);
74  int jointPosToServoCmd(int id, float pos);
75  void setServoJointPos(std::string name, int pos);
76  void setServoPos(int channel, int pos);
77  // bool stopServo(uint8_t jointIndex);
78  void enableRobot();
79  void stopRobot();
80  bool setServo(int id, float angle);
81  void setServos(std::map<int, float> angles);
82 
83  void playSound(float frequency, float duration, float freq2 = -1);
84  void playSoundArray(marty_msgs::SoundArray sound_array);
85  void stopSound();
86 
87  // Getters/Setters
88  bool hasFallen() {return falling_.data;}
89  bool fallDisabled() {return fall_disable_;}
90  float getBattery();
91 
92  // Public Variables
93  robotJoint joint_[NUMJOINTS]; // Internal Joint Data
94  std::deque<float> jangles_;
95 
96  private:
97  void setupJointStates();
98  void setupOdometry();
99  void jointCB(const marty_msgs::ServoMsg::ConstPtr& msg);
100  void jointsCB(const marty_msgs::ServoMsgArray::ConstPtr& msg);
101  void accelCB(const marty_msgs::Accelerometer::ConstPtr& msg);
102  void battCB(const std_msgs::Float32::ConstPtr& msg);
103  void gpioCB(const marty_msgs::GPIOs::ConstPtr& msg);
104  void soundCB(const std_msgs::String::ConstPtr& msg)
105  {this->loadSound(msg->data);}
106  void sendKeyframes(marty_msgs::KeyframeArray k_array);
107  void kfCB(const std_msgs::String::ConstPtr& msg)
108  {this->loadKeyframes(msg->data);}
109  void tfCB(const ros::TimerEvent& e);
110 
111  void updateJointState(marty_msgs::ServoMsg servo);
112  void updateOdom();
113  bool setFallDetector(std_srvs::SetBool::Request& req,
114  std_srvs::SetBool::Response& res);
115 
116  // Flags
117  bool calibrated_; // Check whether Marty has been calibrated yet
118  bool odom_setup_; // Check whether odometry tfs have been setup
119 
120  // Parameters
121  bool fall_disable_; // If true disables motor commands when fallen
122  double fall_thr_; // Threshold for determining if Marty is falling
123  bool odom_accel_; // Combine accelerometer data with odometry
124  bool simulated_; // Whether Marty is living in a dream world
125  // double batt_thr_;
126  bool camera_; // If true camera tf and functions are enabled
127  double camera_ori_; // Camera orientation relative to base_link
128 
129  // Variables
130  marty_msgs::Accelerometer accel_;
131  std_msgs::Bool falling_;
135  marty_msgs::GPIOs gpios_val_;
136  std::deque<double> roll_;
137  std::deque<double> pitch_;
138  std::deque<double> yaw_;
139 
140  // ROS
141  std_msgs::Bool enable_robot_;
142  marty_msgs::ServoMsg servo_msg_;
143  sensor_msgs::JointState joints_; // ROS Joint State msg
144  // marty_msgs::ServoMsgArray servo_msg_array_;
145 
146  ros::Publisher enable_pub_;
147  ros::Publisher falling_pub_;
148  ros::Publisher servo_pub_;
149  ros::Publisher servo_array_pub_;
150  ros::Publisher joints_pub_;
151  ros::Publisher sound_pub_;
152  ros::Publisher keyframe_pub_;
153  ros::Subscriber joint_sub_;
154  ros::Subscriber joints_sub_;
155  ros::Subscriber accel_sub_;
156  ros::Subscriber batt_sub_;
157  ros::Subscriber gpio_sub_;
158  ros::Subscriber sound_sub_;
159  ros::Subscriber keyframe_sub_;
160 
161  ros::ServiceServer fall_dis_srv_;
162  ros::ServiceClient set_gpio_config_;
163  ros::ServiceClient get_gpio_config_;
164 
165  // TF
166  double r_, p_, y_;
167  tf2::Quaternion quat_ori_;
168  tf2_ros::TransformBroadcaster tf_br_;
169  tf2_ros::TransformBroadcaster odom_br_;
170  tf2_ros::Buffer tf_buff_;
171  tf2_ros::TransformListener tf_ls_;
172  ros::Timer tf_timer_;
173  geometry_msgs::TransformStamped cam_tf_;
174  geometry_msgs::TransformStamped odom_tf_;
175  geometry_msgs::TransformStamped l_f_tf_;
176  geometry_msgs::TransformStamped r_f_tf_;
177 };
178 
179 #endif /* MARTY_CORE_HPP */
tf2_ros::TransformBroadcaster odom_br_
Definition: marty_core.hpp:169
ros::Subscriber keyframe_sub_
Definition: marty_core.hpp:159
void kfCB(const std_msgs::String::ConstPtr &msg)
Definition: marty_core.hpp:107
std::deque< float > jangles_
Definition: marty_core.hpp:94
tf2_ros::TransformListener tf_ls_
Definition: marty_core.hpp:171
bool camera_
Definition: marty_core.hpp:126
ros::Subscriber sound_sub_
Definition: marty_core.hpp:158
geometry_msgs::TransformStamped cam_tf_
Definition: marty_core.hpp:173
ros::Publisher joints_pub_
Definition: marty_core.hpp:150
ros::ServiceServer fall_dis_srv_
Definition: marty_core.hpp:161
bool fall_disable_
Definition: marty_core.hpp:121
ros::Publisher falling_pub_
Definition: marty_core.hpp:147
ros::Subscriber gpio_sub_
Definition: marty_core.hpp:157
std::deque< double > yaw_
Definition: marty_core.hpp:138
std::deque< double > roll_
Definition: marty_core.hpp:136
bool simulated_
Definition: marty_core.hpp:124
float battery_min_
Definition: marty_core.hpp:134
bool hasFallen()
Definition: marty_core.hpp:88
geometry_msgs::TransformStamped r_f_tf_
Definition: marty_core.hpp:176
ros::Subscriber batt_sub_
Definition: marty_core.hpp:156
ros::NodeHandle nh_
Definition: marty_core.hpp:59
std_msgs::Bool falling_
Definition: marty_core.hpp:131
geometry_msgs::TransformStamped odom_tf_
Definition: marty_core.hpp:174
ros::Publisher enable_pub_
Definition: marty_core.hpp:146
float battery_val_
Definition: marty_core.hpp:132
bool odom_setup_
Definition: marty_core.hpp:118
int servoChannel
Definition: marty_core.hpp:54
double camera_ori_
Definition: marty_core.hpp:127
void soundCB(const std_msgs::String::ConstPtr &msg)
Definition: marty_core.hpp:104
ros::ServiceClient get_gpio_config_
Definition: marty_core.hpp:163
geometry_msgs::TransformStamped l_f_tf_
Definition: marty_core.hpp:175
ros::Subscriber joint_sub_
Definition: marty_core.hpp:153
marty_msgs::GPIOs gpios_val_
Definition: marty_core.hpp:135
ros::Publisher servo_array_pub_
Definition: marty_core.hpp:149
bool fallDisabled()
Definition: marty_core.hpp:89
marty_msgs::ServoMsg servo_msg_
Definition: marty_core.hpp:142
float cmdMult
Definition: marty_core.hpp:53
ros::Timer tf_timer_
Definition: marty_core.hpp:172
ros::ServiceClient set_gpio_config_
Definition: marty_core.hpp:162
ros::Publisher servo_pub_
Definition: marty_core.hpp:148
ros::Publisher sound_pub_
Definition: marty_core.hpp:151
bool calibrated_
Definition: marty_core.hpp:117
ros::Publisher keyframe_pub_
Definition: marty_core.hpp:152
double y_
Definition: marty_core.hpp:166
ros::Subscriber joints_sub_
Definition: marty_core.hpp:154
tf2_ros::Buffer tf_buff_
Definition: marty_core.hpp:170
sensor_msgs::JointState joints_
Definition: marty_core.hpp:143
double fall_thr_
Definition: marty_core.hpp:122
tf2_ros::TransformBroadcaster tf_br_
Definition: marty_core.hpp:168
ros::Subscriber accel_sub_
Definition: marty_core.hpp:155
tf2::Quaternion quat_ori_
Definition: marty_core.hpp:167
float battery_max_
Definition: marty_core.hpp:133
marty_msgs::Accelerometer accel_
Definition: marty_core.hpp:130
std::deque< double > pitch_
Definition: marty_core.hpp:137
bool odom_accel_
Definition: marty_core.hpp:123
std_msgs::Bool enable_robot_
Definition: marty_core.hpp:141


ros_marty
Author(s): Alejandro Bordallo
autogenerated on Fri Jul 21 2017 14:51:45