-
Notifications
You must be signed in to change notification settings - Fork 0
Matlab & ROS
This tutorial will show how to integrate a matlab generated code for a motor pid controller with ros , we will use a simple two link robot to visualize the results in rviz, the robot package can be downloaded at github.
As mentioned before the code is generated from a simulink model which is included below
This model takes a setpoint as input and with each simulation step sends the position of the motor back. The main generated function we will be using is called step.
After generating code from matlab take some time to read the code and get familiar with the objects and functions used in this code. We have created a new c++ file and included an instance of the simulink model class called driver. In the main function we are using the return value of the step function to edit a variable called msg of type sensor_msgs::JointState. Then we are publishing the values on a ros topic that is visualized in rviz. The code:
int main(int argc, char **argv)
{
ros::init(argc, argv, "jsp");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<sensor_msgs::JointState>("joint_states", 1000);
ros::Rate loop_rate(10000);
sensor_msgs::JointState msg;
msg.name.resize(1);
msg.name[0] = "joint1";
msg.position.resize(1);
/**************************************************************/
finaldriverModelClass driver;
driver.initialize();
//driver.finaldriver_U.INPUT = 100; // a command
for (size_t i = 0; i < 10; i++)
{
std::cin >> driver.finaldriver_U.INPUT;
while (true)
{
driver.step();
printf("%f \n", driver.finaldriver_Y.MotorOutput);
msg.position[0] = driver.finaldriver_Y.MotorOutput;
msg.header.stamp = ros::Time::now();
`pub.publish(msg);`
`ros::spinOnce();`
`loop_rate.sleep();`
`if(sqrt((driver.finaldriver_Y.MotorOutput - driver.finaldriver_U.INPUT)*(driver.finaldriver_Y.MotorOutput - driver.finaldriver_U.INPUT)) < 0.001)`
`{`
`break;`
`}`
}
/**************************************************************/
}
return 0;
}
Launch the my_robot_description package. Launch the jsp node
Notice that the node takes terminal input from the user, with each value the two links robot will move to the setpoint that is used as user input in the jsp node.
And that will conclude integrating matlab generated code with ros.