first_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_INFO("Starting simulation of a first-order, single-input plant.");
11  ros::init(argc, argv, "plant");
12 
13  ros::NodeHandle plant_node;
14 
15  // Declare a new message variable
16  lyap_control::plant_msg msg;
17 
18  // Initial conditions -- these were defined in the header file
19  msg.x[0] = x_IC[0];
20  msg.t = t_IC;
21  msg.setpoint[0] = setpoint[0];
22 
23  // Publish a plant.msg
24  ros::Publisher chatter_pub = plant_node.advertise<lyap_control::plant_msg>("state", 1);
25 
26  // Subscribe to "control_effort" topic to get a controller_msg.msg
27  ros::Subscriber sub = plant_node.subscribe("control_effort", 1, chatterCallback );
28 
29  double x_dot [num_states] = {0};
30 
31  ros::Rate loop_rate(1/delta_t); // Control rate in Hz
32 
33  ROS_INFO("Waiting 3s for rqt_plot to load.");
34  ros::Duration d(3.0);
35  d.sleep();
36 
37  while (ros::ok())
38  {
39  ROS_INFO("x1: %f setpt: %f", msg.x[0], msg.setpoint[0]);
40 
41  chatter_pub.publish(msg);
42 
43 
44  // Update the plant.
45  x_dot[0] = 0.1*msg.x[0]+u[0];
46  msg.x[0] = msg.x[0]+x_dot[0]*delta_t;
47  msg.t = msg.t+delta_t;
48 
49  ros::spinOnce();
50 
51  loop_rate.sleep();
52  }
53 
54  return 0;
55 }
56 
57 // Callback when something is published on 'control_effort'
58 void chatterCallback(const lyap_control::controller_msg& u_msg)
59 {
60  //ROS_INFO("I heard: [%f]", u_msg.u[0]);
61 
62  // Define the stabilizing control effort
63  u[0] = u_msg.u[0];
64 }
d
void publish(const boost::shared_ptr< M > &message) const
int main(int argc, char **argv)
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)
static const double x_IC[num_states]
double delta_t
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
void chatterCallback(const lyap_control::controller_msg &u_msg)
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