second_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/second_order_plant.h"
00006 
00007 
00008 int main(int argc, char **argv)
00009 {
00010   ros::init(argc, argv, "plant");
00011   
00012   ros::NodeHandle plant_node;
00013 
00014   // Declare a new message variable
00015   lyap_control::plant_msg  msg;
00016 
00017   // Initial conditions -- these were defined in the header file
00018   for (int i=0; i<num_states; i++)
00019   {
00020     msg.x[i] = x_IC[i];
00021     msg.setpoint[i] = setpoint[i];
00022   }
00023   msg.t = t_IC;
00024 
00025   // Publish a plant.msg
00026   ros::Publisher chatter_pub = plant_node.advertise<lyap_control::plant_msg>("state", 1);
00027 
00028   // Subscribe to "control_effort" topic to get a controller_msg.msg
00029   ros::Subscriber sub = plant_node.subscribe("control_effort", 1, chatterCallback );
00030   
00031   double x_dot [num_states] = {0.0, 0.0};
00032 
00033   ros::Rate loop_rate(1/delta_t); // Control rate in Hz
00034 
00035   ROS_INFO("Waiting 3s for rqt_plot to load.");
00036   ros::Duration d(3.0);
00037   d.sleep();
00038 
00039   while (ros::ok())
00040   {
00041     ROS_INFO("x1: %f  x2: %f", msg.x[0], msg.x[1]);
00042 
00043     chatter_pub.publish(msg);
00044 
00045     // Update the plant.
00046     x_dot[0] = 0.1*msg.x[0]+u[0];
00047     x_dot[1] = 2.0*msg.x[1]+u[1];
00048 
00049     msg.x[0] = msg.x[0]+x_dot[0]*delta_t;
00050     msg.x[1] = msg.x[1]+x_dot[1]*delta_t;
00051     
00052     msg.t = msg.t+delta_t;
00053 
00054     ros::spinOnce();
00055 
00056     loop_rate.sleep();
00057   }
00058 
00059   return 0;
00060 }
00061 
00062 // Callback when something is published on 'control_effort'
00063 void chatterCallback(const lyap_control::controller_msg& u_msg)
00064 {
00065   //ROS_INFO("I heard: %f %f", u_msg.u[0], u_msg.u[1]);
00066 
00067   // Define the stabilizing control effort
00068   for (int i=0; i< num_inputs; i++)
00069     u[i] = u_msg.u[i];
00070 }


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