ethz-asl / rotors_simulator

RotorS is a UAV gazebo simulator
1.22k stars 756 forks source link

Controlling actuator velocities using simulink blocks #487

Open HusseinHazem opened 6 years ago

HusseinHazem commented 6 years ago

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 commented 6 years ago

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

HusseinHazem commented 6 years ago

![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 commented 6 years ago

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 _mavmsgs/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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

gsilano commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Yes, of course. You find it in attach.

GitHub_issue_RotorS_MALTAB2017a.zip

HusseinHazem commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

gsilano commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

gsilano commented 6 years ago

Yes, it should work.

HusseinHazem commented 6 years ago

The "from workspace " blocks give me that error errors

gsilano commented 6 years ago

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

HusseinHazem commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

gsilano commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

gsilano commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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 commented 4 years ago

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 commented 4 years ago

Here it is the link to the Wiki page: https://github.com/ethz-asl/rotors_simulator/wiki/Interfacing-RotorS-through-Matlab

danialwaleed commented 2 years ago

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