Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlling actuator velocities using simulink blocks #487

Open
HusseinHazem opened this issue May 2, 2018 · 43 comments
Open

Controlling actuator velocities using simulink blocks #487

HusseinHazem opened this issue May 2, 2018 · 43 comments

Comments

@HusseinHazem
Copy link

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.

@gsilano
Copy link

gsilano commented May 2, 2018

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

@HusseinHazem
Copy link
Author

![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.

@gsilano
Copy link

gsilano commented May 2, 2018

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.

@HusseinHazem
Copy link
Author

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.

@gsilano
Copy link

gsilano commented May 2, 2018

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.

@HusseinHazem
Copy link
Author

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?

@gsilano
Copy link

gsilano commented May 3, 2018

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.

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 5, 2018

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

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 6, 2018

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.

@HusseinHazem
Copy link
Author

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?

@gsilano
Copy link

gsilano commented May 6, 2018

Yes, of course. You find it in attach.

GitHub_issue_RotorS_MALTAB2017a.zip

@HusseinHazem
Copy link
Author

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?

@gsilano
Copy link

gsilano commented May 6, 2018

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.

@HusseinHazem
Copy link
Author

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
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 7, 2018

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).

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 7, 2018

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.

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 7, 2018

Yes, it should work.

@HusseinHazem
Copy link
Author

The "from workspace " blocks give me that error
errors

@gsilano
Copy link

gsilano commented May 7, 2018

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

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 7, 2018

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.

@HusseinHazem
Copy link
Author

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

@gsilano
Copy link

gsilano commented May 7, 2018

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.

@HusseinHazem
Copy link
Author

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.

@ffurrer
Copy link
Contributor

ffurrer commented May 7, 2018

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.

@gsilano
Copy link

gsilano commented May 7, 2018

@ffurrer, why not. It is the minimum for the help you gave me some months ago: the issues I had with the build when I opened the pull request #465.

Could I insert such guide in CrazyS? If you remember, it is an extension of the RotorS capabilities in which I modeled, developed and integrated the Crazyflie 2.0 nano-quadcopter.

@ffurrer
Copy link
Contributor

ffurrer commented May 8, 2018

This is up to you of course, but I guess it would make a bit more sense if it would be in this repo. As this is unspecific to CrazyS.

@gsilano
Copy link

gsilano commented May 8, 2018

Yes, of course. I will write it for RotorS sharing such contents also in CrazyS. In this way, the same procedure may be used not only by RotorS developers but also the CrazyS ones.
I remember well that CrazyS is nothing else than a branch, an extension of RotorS.

@HusseinHazem
Copy link
Author

Giuseppe, is there a way to run the simulink for one step? because when i put my controller scheme it generates the input till the quadrotor settles so for example it can reach z=1 then fall and in the next simulation step it will do the same and so on.

@gsilano
Copy link

gsilano commented May 9, 2018

I don't know how your controller works, but you have to take into account that now the simulation is in discrete time and not continuous.

@HusseinHazem
Copy link
Author

what kinda worked for me was to do the whole thing on matlab, i tried out a simple PID for Z. it goes to the desired Z position and keeps hovering for about 40 seconds perfectly then it drifts slowly and eventually falls. Can you give me your email so i can send you?

@gsilano
Copy link

gsilano commented May 10, 2018

My e-mail address is giuseppe.silano[at]unisannio.it :-)

@gsilano
Copy link

gsilano commented Nov 19, 2018

@ffurrer, I just updated the wiki section of RotorS related issue above. Here the link

https://github.com/ethz-asl/rotors_simulator/wiki/Interfacing-RotorS-through-Matlab

In the coming days, I will release a new version of CrazyS with all the files I mentioned in the wiki. I hope it will be useful for all RotorS users.

@mahdinobar
Copy link

@gsilano Hello, I was wondering if you would inform me of the documentation or reference for the hovering example controller model at https://github.com/ethz-asl/rotors_simulator/wiki/Interfacing-RotorS-through-Matlab#examples-controller.

@gsilano
Copy link

gsilano commented Mar 27, 2019

Hi @mahdinobar! I don't know if I understand correctly, but all the information regarding the use of the MathWorks Robotics System Toolbox with RotorS are reported in the Wiki section of CrazyS and RotorS. Are you looking for other stuff?

@islamdib
Copy link

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

Hello,
please can u send me your model ?

@gsilano
Copy link

gsilano commented Apr 11, 2020

@danialwaleed
Copy link

Hi Everyone,

I have been working on a similar problem which HusseinHazem was working on and that is how to publish motor speed commands using Simulink. I believe I have figured out a method to do so. I am attaching a simulink file as well and hope it helps.

The solution requires the use of "Use of Matlab function to assign nested arrays" documentation in Matlab, I have done this for the mav_msgs/Actuators message.

In the function make sure that the message length is defined using the command

msg.AngularVelocities_SL_Info.CurrentLength=uint32(4);

After that assign values to the motor using the command
msg.AngularVelocities=u;

Where u is the input to the function. I am using Matlab 2021b so if you need an older version do let me know. I have been trying to figure this problem out for quite a few months and I hope this helps other people.

Simulink_motor_pub.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants