Tetrapod Project
single_motor_controller.h
Go to the documentation of this file.
1 #ifndef single_motor_controller_h
2 #define single_motor_controller_h
3 
4 #include "ros/ros.h"
5 #include "std_msgs/Bool.h"
6 #include "std_msgs/Float32.h"
7 #include "sensor_msgs/JointState.h"
8 #include "std_msgs/Float64MultiArray.h"
9 
10 #include <Eigen/Core>
11 
12 #include <kinematics/kinematics.h>
13 
15 {
16  const double UNINITIALIZED_STATE = 1000.0;
17 
18  const double IDLE_COMMAND = 1000.0;
19 
20  /*** CONSTRUCTOR/DESTRUCTOR) ***/
21 
22  public: SingleMotorController(double _publish_frequency, bool _use_position_control);
23 
24  public: SingleMotorController(double _publish_frequency);
25 
26  public: virtual ~SingleMotorController(){};
27 
28  /*** ROS FUNCTIONS ***/
29 
30  public: void initROS();
31 
32  private: void motorStateCallback(const sensor_msgs::JointStatePtr &_msg);
33 
34  private: void readyToProceedCallback(const std_msgs::Bool &_msg);
35 
36  private: void setGoalCallback(const std_msgs::Float32 &_msg);
37 
38  private: void motorConfirmationCallback(const std_msgs::Bool &_msg);
39 
40  private: void keepLoggingCallback(const std_msgs::Bool &_msg);
41 
42  /*** CONTROL FUNCTIONS ***/
43 
45  (
46  const double &_percentage,
47  const double &_period,
48  const double &_max_travel,
49  double &_x,
50  double &_x_d,
51  double &_x_dd
52  );
53 
54  public: void updateTrajectory();
55 
56  public: void sendJointPositionCommand();
57 
58  public: void sendJointTorqueCommand();
59 
60  public: void increaseIterator();
61 
62  public: void moveToZero();
63 
64  public: void setPositionDirectly(double _joint_pos);
65 
66  public: void setVelocityDirectly(double _joint_vel);
67 
68  public: void setTorqueDirectly(double _torque);
69 
70 
71 
72  /*** HELPER FUNCTIONS ***/
73 
74  public: void initializeMotor
75  (
76  double _k_p_pos,
77  double _k_d_pos,
78  double _k_p_vel,
79  double _k_i_vel,
80  double _k_p_torque,
81  double _k_i_torque
82  );
83 
84  public: void initializeMotor();
85 
86  public: bool readyToProceed();
87 
88  public: bool initialStateReceived();
89 
90  public: void checkForNewMessages();
91 
92  public: void printAll();
93 
94  public: void writeToLog();
95 
96  public: bool keepLogging(){return keep_logging;}
97 
98  public: double getPosition(){return angle_pos;}
99 
100  /*** STATE VARIABLES ***/
101 
102  private: double angle_offset = 0.0;
103 
104  private: double angle_pos = UNINITIALIZED_STATE;
105 
106  private: double angle_vel = 0;
107 
108  private: double angle_pos_ref = 0;
109 
110  private: double angle_vel_ref = 0;
111 
112  private: double angle_acc_ref = 0;
113 
114  private: double torque = 0;
115 
116  private: double torque_ref = 0;
117 
118  private: bool ready_to_proceed = false;
119 
120  private: bool motor_initialized = false;
121 
122  private: double current_iteration = 0.0;
123 
124  private: bool keep_logging = true;
125 
126  private: bool gains_set = false;
127 
128  /*** PARAMETERS ***/
129 
130  private: double publish_frequency = 100.0;
131 
132  private: double period = 2.0;
133 
134  private: double max_iterations;
135 
136  private: double max_travel = 0.0; //M_PI*2.0/3.0;
137 
138  private: double inertia = 0.1;
139 
140  private: double k_p_controller = 50.0;
141 
142  private: double k_i_controller = 10.0;
143 
144  private: double k_d_controller = 5.0;
145 
146  private: double k_p_pos = 1.0;
147 
148  private: double k_i_pos = 1.0;
149 
150  private: double k_d_pos = 5.0;
151 
152  private: double k_p_vel = 50.0;
153 
154  private: double k_i_vel = 50.0;
155 
156  private: double k_d_vel = 0.0;
157 
158  private: double k_p_torque = 100.0;
159 
160  private: double k_i_torque = 100.0;
161 
162  private: double k_d_torque = 0.0;
163 
164  /*** ROS VARIABLES ***/
165 
166  private: std::unique_ptr<ros::NodeHandle> node_handle;
167 
168  private: ros::Subscriber motor_state_subscriber;
169 
170  private: ros::Subscriber ready_to_proceed_subscriber;
171 
172  private: ros::Subscriber set_goal_subscriber;
173 
174  private: ros::Subscriber motor_confirmation_subscriber;
175 
176  private: ros::Subscriber keep_logging_subscriber;
177 
178  private: ros::Publisher joint_command_publisher;
179 
180  private: ros::Publisher motor_gain_publisher;
181 
182  private: ros::Publisher log_motor_state_publisher;
183 
184  private: ros::Publisher log_motor_reference_publisher;
185 };
186 
187 #endif
ros::Subscriber ready_to_proceed_subscriber
ros::Subscriber motor_confirmation_subscriber
void readyToProceedCallback(const std_msgs::Bool &_msg)
void calculateSingleAxisTrajectory(const double &_percentage, const double &_period, const double &_max_travel, double &_x, double &_x_d, double &_x_dd)
ros::Subscriber keep_logging_subscriber
std::unique_ptr< ros::NodeHandle > node_handle
void setPositionDirectly(double _joint_pos)
void setTorqueDirectly(double _torque)
ros::Publisher joint_command_publisher
ros::Subscriber motor_state_subscriber
void setVelocityDirectly(double _joint_vel)
ros::Publisher log_motor_state_publisher
void keepLoggingCallback(const std_msgs::Bool &_msg)
void motorConfirmationCallback(const std_msgs::Bool &_msg)
SingleMotorController(double _publish_frequency, bool _use_position_control)
void motorStateCallback(const sensor_msgs::JointStatePtr &_msg)
void setGoalCallback(const std_msgs::Float32 &_msg)
ros::Publisher log_motor_reference_publisher