first_order_plant_sim.cpp
Go to the documentation of this file.
00001 // This file simulates a simple dynamic system, publishes its state,
00002 // and subscribes to a 'control_effort' topic. The control effort is used
00003 // to stabilize the plant.
00004 
00005 #include "lyap_control/first_order_plant.h"
00006 
00007 
00008 int main(int argc, char **argv)
00009 {
00010   ROS_INFO("Starting simulation of a first-order, single-input plant.");
00011   ros::init(argc, argv, "plant");
00012   
00013   ros::NodeHandle plant_node;
00014 
00015   // Declare a new message variable
00016   lyap_control::plant_msg  msg;
00017 
00018   // Initial conditions -- these were defined in the header file
00019   msg.x[0] = x_IC[0];
00020   msg.t = t_IC;
00021   msg.setpoint[0] = setpoint[0];
00022 
00023   // Publish a plant.msg
00024   ros::Publisher chatter_pub = plant_node.advertise<lyap_control::plant_msg>("state", 1);
00025 
00026   // Subscribe to "control_effort" topic to get a controller_msg.msg
00027   ros::Subscriber sub = plant_node.subscribe("control_effort", 1, chatterCallback );
00028   
00029   double x_dot [num_states] = {0};
00030 
00031   ros::Rate loop_rate(1/delta_t); // Control rate in Hz
00032 
00033   ROS_INFO("Waiting 3s for rqt_plot to load.");
00034   ros::Duration d(3.0);
00035   d.sleep();
00036 
00037   while (ros::ok())
00038   {
00039     ROS_INFO("x1: %f   setpt: %f", msg.x[0], msg.setpoint[0]);
00040 
00041     chatter_pub.publish(msg);
00042 
00043 
00044     // Update the plant.
00045     x_dot[0] = 0.1*msg.x[0]+u[0];
00046     msg.x[0] = msg.x[0]+x_dot[0]*delta_t;
00047     msg.t = msg.t+delta_t;
00048 
00049     ros::spinOnce();
00050 
00051     loop_rate.sleep();
00052   }
00053 
00054   return 0;
00055 }
00056 
00057 // Callback when something is published on 'control_effort'
00058 void chatterCallback(const lyap_control::controller_msg& u_msg)
00059 {
00060   //ROS_INFO("I heard: [%f]", u_msg.u[0]);
00061 
00062   // Define the stabilizing control effort
00063   u[0] = u_msg.u[0];
00064 }


lyap_control
Author(s): Andy Zelenak
autogenerated on Wed Mar 2 2016 12:14:43