SMORES Robot Platform Simulation
Modlab at Penn, ASL at Cornell
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
ModuleController.hh
1 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // Author: Edward Yunkai Cui
3 // Description: Thie is a customized model plugin class, in which we
4 // implemented the low level controllers of each individual
5 // model. Those controllers including joint position
6 // control, joint speed control, model plane motion control,
7 // model plane position and orientation control
8 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 #ifndef _GAZEBO_MODEL_CONTROLLER_HH_
11 #define _GAZEBO_MODEL_CONTROLLER_HH_
12 
13 #include <iostream>
14 #include <cmath>
15 #include <boost/bind.hpp>
16 
17 #include <gazebo/gazebo.hh>
18 #include <gazebo/physics/physics.hh>
19 #include <gazebo/common/common.hh>
20 // Classes for communication between different plugins
21 #include "collision_message.pb.h"
22 #include "command_message.pb.h"
23 // Library for colored log text
24 #include "ColorLog.hh"
25 // The head file which contains the joint_plus structure
26 #include "SmoresJoint.hh"
27 
28 #define PI 3.1415926
29 #define EXECUTIONERROR 0.08
30 
31 using std::vector;
32 using std::string;
34 typedef const boost::shared_ptr<const gazebo::msgs::GzString> GzStringPtr;
36 typedef const boost::shared_ptr<const gazebo::msgs::Pose> PosePtr;
38 typedef const boost::shared_ptr<const command_message::msgs::CommandMessage>
39  CommandMessagePtr;
40 
41 namespace gazebo
42 {
44 
47 class ModuleController : public ModelPlugin
48 {
49  public:
54 
55  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56  //+ Useful functions to get model status and other tool functions +
57  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59 
66  double RevolutionSpeedCal(physics::JointPtr joint, const int axis_index);
68 
72  math::Pose GetModelCentralCoor(void);
74 
80  math::Angle AngleCalculation2Points(math::Vector2d start_point,
81  math::Vector2d end_point);
83 
92  double ComplementaryFilter(double filtering_value,
93  double complement_filter_par, double *value_history);
95 
100  double ComplementaryFilter(double filtering_value, double *value_history);
102 
106  SmoresJoint & GetJointPlus(int node_ID);
108 
112  int GetJointAxis(int node_ID);
114 
119  math::Angle GetJointAngle(physics::JointPtr current_joint, int rot_axis);
120 
121  private:
123 
127  void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf);
129 
132  void WelcomInfoProcessor(GzStringPtr &msg);
134 
137  void SystemInitialization(physics::ModelPtr parent_model);
139  void OnSystemRunning(const common::UpdateInfo & /*_info*/);
141 
142  void CollisionPubAndSubInitialization(void);
144 
145  void CollisionReceivingCallback(GzStringPtr &msg);
147 
150  void CommandDecoding(CommandMessagePtr &msg);
151 
152 
153  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
154  //+ Low level model control functions +
155  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157 
163  void SetJointAngleForce(physics::JointPtr current_joint, int rot_axis,
164  double position);
166 
171  void JointPIDController(double angle_desired_radian,
172  double desire_speed, SmoresJoint *current_joint);
174 
179  void JointPIDController(double angle_desired_radian, SmoresJoint *current_joint);
181  void JointAngleUpdateInJointPlus(void);
183 
189  void SetJointSpeed(physics::JointPtr CurrentJoint, int RotAxis,
190  double SpeedDesired);
191 
192  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
193  //+ Command Execution Tracking Functions +
194  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
196 
197  void JointAngleTracking(void);
199 
200  void PositionTracking(void);
201 
202  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
203  //+ Functions that control the model's planar motion +
204  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
206 
211  void AnglePIDController(math::Angle desired_angle, math::Angle current_angle,
212  math::Vector2d current_speed);
214 
218  void Move2Point(math::Vector2d DesiredPoint, math::Angle DesiredOrientation);
219  public:
221  physics::ModelPtr model;
223  transport::PublisherPtr collisionInfoToServer;
224  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
225  //+ Model Pramaters +
226  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
228 
233  math::Vector3 jointAngleKPID;
235 
240  math::Vector3 modelAngleKPID;
242  double maxiRotationRate; // Maximium rotation rate of each joint
248  double wheelRadius;
249  private:
250  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251  //+ Pointers of Model and joints and a emhanced struct +
252  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
254  physics::JointPtr jointWR;
256  physics::JointPtr jointWL;
258  physics::JointPtr jointWF;
260  physics::JointPtr jointCB;
262  SmoresJoint jointWRP;
264  SmoresJoint jointWLP;
266  SmoresJoint jointWFP;
268  SmoresJoint jointCBP;
269 
270  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
271  //+ Model plugin events +
272  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
274  event::ConnectionPtr updateConnection;
275 
276  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
277  //+ Communication Publishers and Subscribers +
278  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
280  transport::SubscriberPtr welcomeInfoSub;
282  transport::SubscriberPtr commandSub;
284  transport::PublisherPtr commandPub;
286  transport::SubscriberPtr linkCollisonSub[4];
287 
288  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
289  //+ Useful Buffers +
290  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
292  vector<string> nameOfConnectedModels;
293 
294  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
295  //+ Model Execution Variables +
296  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
298 
304  int executionState;
306  double jointAngleShouldBe[4];
308  double jointSpeeds[4];
310  math::Pose targetPosition;
312  double lftWheelSpeed;
314  double rgtWheelSpeed;
316  bool startExecution;
317  // TODO: command id should be implemented in the future
318 }; // class ModuleController
319 } // namespace gazebo
320 #endif
gazebo name space
Definition: system_gui.cc:41
~ModuleController()
Destructor.
Definition: ModuleController.cc:31
double maxiRotationRate
Maximium rotation rate of each joint, unit: rad/s.
Definition: ModuleController.hh:242
double RevolutionSpeedCal(physics::JointPtr joint, const int axis_index)
Calculates the angular velocity of a revolute joint.
Definition: ModuleController.cc:39
math::Angle GetJointAngle(physics::JointPtr current_joint, int rot_axis)
Returns the angle of the specified joint.
Definition: ModuleController.cc:99
The modelplugin that actually controls the model and provide low level apis.
Definition: ModuleController.hh:47
math::Vector3 jointAngleKPID
Default Joint Angle PID controller parameter.
Definition: ModuleController.hh:233
SmoresJoint & GetJointPlus(int node_ID)
Returns the SmoresJoint structure according to the node ID.
Definition: ModuleController.cc:75
double accelerationRate
Acceleration rate of the planar motion.
Definition: ModuleController.hh:244
math::Vector3 modelAngleKPID
Default Joint Angle PID controller parameter.
Definition: ModuleController.hh:240
transport::PublisherPtr collisionInfoToServer
Publisher of the collision information.
Definition: ModuleController.hh:223
math::Angle AngleCalculation2Points(math::Vector2d start_point, math::Vector2d end_point)
Calculates the anlge of the line connected two points in the world frame.
Definition: ModuleController.cc:52
double planarMotionStopThreshold
Planar motion control threshold.
Definition: ModuleController.hh:246
A struct that has richer features than gazebo joint object.
Definition: SmoresJoint.hh:10
ModuleController()
Constructor.
Definition: ModuleController.cc:10
double ComplementaryFilter(double filtering_value, double complement_filter_par, double *value_history)
Apply a complementary filter a raw sensor value.
Definition: ModuleController.cc:61
double wheelRadius
Radius of the wheels.
Definition: ModuleController.hh:248
physics::ModelPtr model
Current Model Pointer.
Definition: ModuleController.hh:221
math::Pose GetModelCentralCoor(void)
Gets the coordinates and direction of the current model.
Definition: ModuleController.cc:46
int GetJointAxis(int node_ID)
Returns the index of joint axis according to the node ID.
Definition: ModuleController.cc:88