second_order_plant_sim.cpp
Go to the documentation of this file.
1 // This file simulates a simple dynamic system, publishes its state,
2 // and subscribes to a 'control_effort' topic. The control effort is used
3 // to stabilize the plant.
4 
6 
7 
8 int main(int argc, char **argv)
9 {
10  ros::init(argc, argv, "plant");
11 
12  ros::NodeHandle plant_node;
13 
14  // Declare a new message variable
15  lyap_control::plant_msg msg;
16 
17  // Initial conditions -- these were defined in the header file
18  for (int i=0; i<num_states; i++)
19  {
20  msg.x[i] = x_IC[i];
21  msg.setpoint[i] = setpoint[i];
22  }
23  msg.t = t_IC;
24 
25  // Publish a plant.msg
26  ros::Publisher chatter_pub = plant_node.advertise<lyap_control::plant_msg>("state", 1);
27 
28  // Subscribe to "control_effort" topic to get a controller_msg.msg
29  ros::Subscriber sub = plant_node.subscribe("control_effort", 1, chatterCallback );
30 
31  double x_dot [num_states] = {0.0, 0.0};
32 
33  ros::Rate loop_rate(1/delta_t); // Control rate in Hz
34 
35  ROS_INFO("Waiting 3s for rqt_plot to load.");
36  ros::Duration d(3.0);
37  d.sleep();
38 
39  while (ros::ok())
40  {
41  ROS_INFO("x1: %f x2: %f", msg.x[0], msg.x[1]);
42 
43  chatter_pub.publish(msg);
44 
45  // Update the plant.
46  x_dot[0] = 0.1*msg.x[0]+u[0];
47  x_dot[1] = 2.0*msg.x[1]+u[1];
48 
49  msg.x[0] = msg.x[0]+x_dot[0]*delta_t;
50  msg.x[1] = msg.x[1]+x_dot[1]*delta_t;
51 
52  msg.t = msg.t+delta_t;
53 
54  ros::spinOnce();
55 
56  loop_rate.sleep();
57  }
58 
59  return 0;
60 }
61 
62 // Callback when something is published on 'control_effort'
63 void chatterCallback(const lyap_control::controller_msg& u_msg)
64 {
65  //ROS_INFO("I heard: %f %f", u_msg.u[0], u_msg.u[1]);
66 
67  // Define the stabilizing control effort
68  for (int i=0; i< num_inputs; i++)
69  u[i] = u_msg.u[i];
70 }
d
static const int num_inputs
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
lyap_control::controller_msg u_msg
ublas_vector u(num_inputs)
void chatterCallback(const lyap_control::controller_msg &u_msg)
static const double x_IC[num_states]
double delta_t
int main(int argc, char **argv)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
ublas_vector setpoint(num_states)
static const double t_IC
ROSCPP_DECL void spinOnce()
static const int num_states


lyap_control
Author(s): Andy Zelenak
autogenerated on Mon Jun 10 2019 13:50:32