rotors_simulator icon indicating copy to clipboard operation
rotors_simulator copied to clipboard

Controlling actuator velocities using simulink blocks

Open HusseinHazem opened this issue 6 years ago • 43 comments

I launched the mav_hovering_example.launch file and commented out lines 32 till line 36 (the part where the lee_position_controller node launches) to be able to control the speeds of the motors using the message publisher in rqt. Is it possible to control each motor speed from the simulink ros blocks? and if so how can i do it? My attempt leads to gazebo freezing and even then i don't how to access each motor speed separately.

HusseinHazem avatar May 02 '18 12:05 HusseinHazem

I suggest you to read something about the MathWorks Robotics System Toolbox

gsilano avatar May 02 '18 12:05 gsilano

![screenshot from 2018-05-02 15 58 33](https://user-images.githubusercontent.com/36478570/39527571-0b638eb2-4e22-11e8-88fd-4ec5c4b672b5.png that was my attempt. but i also followed https://it.mathworks.com/help/robotics/examples/work-with-ros-messages-in-simulink.html but i couldn't get it to work.

HusseinHazem avatar May 02 '18 14:05 HusseinHazem

I suggest you to read these tutorials: Connect to a ROS-enabled Robot from Simulink, Install robotics system toolbox support packages and Create custom messagges from ROS package.

You have to take in mind that mav_msgs/Actuators is not a standard ROS message. It is not present in the default MATLAB installation (with the Robotics System Toolbox) and you have to import it by using the tutorials I linked you.

gsilano avatar May 02 '18 14:05 gsilano

thanks but i already went through all of these and installed ROS custom message interface. and as you can see from the screenshot the message type mav_msgs/Actuators in available for use. My problem is how to publish the message to the motor speeds.

HusseinHazem avatar May 02 '18 14:05 HusseinHazem

You right!! It is not allowed to publish a message via Simulink, at least I did not succeed. You should use MATLAB, in particular the commands as reported below:

[pub, msg] = rospublisher('/command/motor_speed','mav_msgs/Actuators'); msg.AngularVelocities = [Omega_1_SIMULINK.signals.values(end), Omega_2_SIMULINK.signals.values(end), Omega_3_SIMULINK.signals.values(end), Omega_4_SIMULINK.signals.values(end)]; send(pub,msg);

where Omega_1, Omega_2, and so on, are the propellers angular velocities you would publish.

I hope I have made it clear.

gsilano avatar May 02 '18 16:05 gsilano

screenshot from 2018-05-03 14 41 04

Thanks a lot , i changed it a bit to the above and it worked. My goal is to use my controller from simulink to control the motor velocities, any idea how i could do that?

HusseinHazem avatar May 03 '18 12:05 HusseinHazem

Great!! You have to combine SIMULINK with MATLAB. The MATLAB script handles the communication, receiving data from ROS and passing such data to Simulink. The control scheme in Simulink computes the control actions and saves such actions in a toWorkspace block. Later MATLAB, thanks to the command I showed (and you corrected a little bit), sends the motor command to GAZEBO.

To do that you have to synchronize the enviroment by using the GAZEBO services pause and unpase physics.

gsilano avatar May 03 '18 12:05 gsilano

Do you know how i can remove this error? the u here is a vector of length 4 coming from a mux and for some reason the function can't recognize the ros function. sendinput2 matlaberror2

HusseinHazem avatar May 05 '18 19:05 HusseinHazem

You cannot use this kind of Matlab function in Simulink. I mean, you cannot publish a ROS message via Simulink by using it. In attached two files: a MATLAB script that handles the communication between MATLAB and GAZEBO, and a Simulink that allows you to implement your control law. I mean, the Simulink scheme is able to use the Gazebo data (MATLAB makes them available) and to compute the propellers angular velocities. Such velocities every simulation step (Ts seconds) are sent to MATLAB (a simple toWorkspace block) that has the aim to publish them. Furthermore, the MATLAB script makes the two environments synchronized: when MATLAB is running, Gazebo is in pause and vice-versa.

I don't know if I got it across. I hope so.

The 2018a version of MATLAB you need to run these files

Issue_GitHub.zip

gsilano avatar May 05 '18 21:05 gsilano

thanks i will try it out and get back to you, but will 2017a version not work?

HusseinHazem avatar May 06 '18 14:05 HusseinHazem

I made the Simulink scheme by using 2018a version of MATLAB. I don't know if you can open it with a previous version of the software. You should try.

gsilano avatar May 06 '18 14:05 gsilano

it is possible but you have to change the file from a simulink 2018a as shown in the pic 2018-05-06 Can you export to version 2017a and send it again?

HusseinHazem avatar May 06 '18 14:05 HusseinHazem

Yes, of course. You find it in attach.

GitHub_issue_RotorS_MALTAB2017a.zip

gsilano avatar May 06 '18 14:05 gsilano

Thanks a lot, but i am having a bit of trouble understanding how i am supposed to run it using my control method from simulink. Am i supposed to send my omegas generated from my control to the omega blocks instead of the constants and run the simulink model first then the matlab script?

HusseinHazem avatar May 06 '18 15:05 HusseinHazem

As I told you before, the MATLAB script has the aim of handling the communication. It is enough to run the MATLAB script to execute the entire simulation. Thus, you have to think in your mind an event triggered system. Such system brings data from the environment, computes the next action to do (the propeller angular velocities) and acts it in order to reach the goal. Every step, each Ts seconds (1e-3 in my example) the cycle is run. How many time? The number of the simulation steps.

In other words, the code works like an automata. In each state, some actions are done and later the results used by the next node, and so on.

gsilano avatar May 06 '18 15:05 gsilano

so that being said, i won't be able to use my MPC (model predictive control) i did on simulink where i publish the speed of the motors depending on the feedback i get from gazebo (i.e position of the hummingbird)

HusseinHazem avatar May 07 '18 10:05 HusseinHazem

i hadn't noticed that you run the simulink simulation in the matlab script, so it should work?

HusseinHazem avatar May 07 '18 10:05 HusseinHazem

Yes, you are able to do that..but you have to redesign the controller. I mean, the idea behind. I already used the Robotics System Toolbox in my research activity. In particular, I employed such toolbox to develop an extension of the RotorS platform named CrazyS.

In this video is showed as the Robotics System Toolbox (on a Windows machine) is able to send commands (the propellers angular velocities) to and receive data from the Gazebo 3D virtual environment (on a Ubuntu 14.04 machine).

gsilano avatar May 07 '18 10:05 gsilano

So if this is my controller with the system, i should just change the feedback to be of x y z roll pitch and yaw to be from the workspace of matlab which come from gazebo?

simulinksim

HusseinHazem avatar May 07 '18 10:05 HusseinHazem

First of all, when you are using the Robotics System Toolbox with Gazebo you have to take in mind that your model is in Gazebo, and not in MATLAB. Later, the feedback loop data come from Gazebo. Thus, you have to read the odometry messages from the environment, to compute from the propellers angular velocities for a single step, and finally to send them by using the properly MATLAB function.

gsilano avatar May 07 '18 10:05 gsilano

Yes i know, i will totally neglect the system in matlab sim should that work?

HusseinHazem avatar May 07 '18 10:05 HusseinHazem

Yes, it should work.

gsilano avatar May 07 '18 11:05 gsilano

The "from workspace " blocks give me that error errors

HusseinHazem avatar May 07 '18 11:05 HusseinHazem

You have to save the variables in the Workspace as 2D Array. See my Simulink scheme.

gsilano avatar May 07 '18 11:05 gsilano

It worked, flies for like 3 seconds then it disappears into the ground hahahahahah but i think it is an error from my control. gazebo

HusseinHazem avatar May 07 '18 11:05 HusseinHazem

Maybe, could be. I am glad to listen that all is ok now. Thus, I think you can close the issue now.

Say something more in the title. Such discussion could help others that are interested to use the toolbox but don't know how to do.

gsilano avatar May 07 '18 12:05 gsilano

i don't think it is the control though, are there any parameters that i can change which would effect the simulation somehow?

HusseinHazem avatar May 07 '18 12:05 HusseinHazem

The simulation steps, maybe. It has to be equal to the simulation step that you used in the simulation scheme. Search on google how to modify a world in GAZEBO, the real time factor.

gsilano avatar May 07 '18 12:05 gsilano

okay i will, but i think something is wrong with the controllers output because i tried your simulink scheme and it worked, or at least didn't flip over. Thanks a lot for your help, and i'll try to get it to work and get back to you.

HusseinHazem avatar May 07 '18 12:05 HusseinHazem

Hey @HusseinHazem and @gsilano I think it would be awesome if you could add a Wiki page for this.

I started one here: https://github.com/ethz-asl/rotors_simulator/wiki/Interfacing-RotorS-through-Matlab

Feel free to edit also the sample titles.

ffurrer avatar May 07 '18 12:05 ffurrer