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

How to use MAV_CMD_DO_SET_ACTUATOR in MATLAB? #3028

Closed
Everglow0214 opened this issue Jan 31, 2024 · 5 comments
Closed

How to use MAV_CMD_DO_SET_ACTUATOR in MATLAB? #3028

Everglow0214 opened this issue Jan 31, 2024 · 5 comments

Comments

@Everglow0214
Copy link

Settings

Pixhawk 4
PX4 1.14.0
MATLAB R2023b

Question

I want to send rotor commands to Pixhawk from MATLAB through telemetry radio. After some search, I think MAV_CMD_DO_SET_ACTUATOR is the appropriate interface. I can arm Pixhawk from MATLAB using MAV_CMD_COMPONENT_ARM_DISARM. However, I cannot make MAV_CMD_DO_SET_ACTUATOR work. My code is as follows.

tele_com_port = "COM16";
device = serial(tele_com_port, "BaudRate", 57600);

dialect = mavlinkdialect("common.xml");
mavlink = mavlinkio(dialect);

% Arm, MAV_CMD_COMPONENT_ARM_DISARM 
cmd_arm = createcmd(dialect, "long", 400);
cmd_arm.Payload.target_system = uint8(1);
cmd_arm.Payload.param1 = single(1);
buffer_cmd_arm = serializemsg(mavlink, cmd_arm);

% Set rotor input, MAV_CMD_DO_SET_ACTUATOR
cmd_set_actuator = createcmd(dialect, "long", 187);
cmd_set_actuator.Payload.target_system = uint8(1);
cmd_set_actuator.Payload.param1 = single(0.1);
cmd_set_actuator.Payload.param2 = single(0,1);
cmd_set_actuator.Payload.param3 = single(0,1);
cmd_set_actuator.Payload.param4 = single(0.1);
buffer_cmd_set_actuator = serializemsg(mavlink, cmd_set_actuator);

fclose(device);
fopen(device);

% Arm.
fwrite(device, buffer_cmd_arm, "uint8");
fprintf("Armed\n");
% Set rotor inputs.
for i = 0: 1000
    thr = (0.8 - 0.2) / 1000 * i + 0.2; 
    cmd_set_actuator.Payload.param1 = single(thr);
    cmd_set_actuator.Payload.param2 = single(thr);
    cmd_set_actuator.Payload.param3 = single(thr);
    cmd_set_actuator.Payload.param4 = single(thr);
    buffer_cmd_set_actuator = serializemsg(mavlink, cmd_set_actuator);
    % fwrite(device, buffer_cmd_set_actuator, "uint8");
    fwrite(device, buffer_cmd_set_actuator);
    pause(0.02);
end
fprintf("Done\n");
fclose(device);

I checked the document at https://github.com/PX4/PX4-user_guide/blob/main/en/payloads/README.md#generic-actuator-control. However, there is no Peripheral via Actuator Set in my QGC. The current settings about the rotors are as follows.
a
b

Thanks in advance.

@hamishwillee
Copy link
Collaborator

Unfortunately this feature is not present in 1.14 - see PX4/PX4-Autopilot#21966

It is still in the docs because it should not have been removed and I have been trying to get it added back.

@bkueng
Copy link
Member

bkueng commented Feb 1, 2024

@Everglow0214 Offboard Actuator Set is correct, it got renamed to Peripheral via Actuator Set after 1.14.
You have to set COM_PREARM_MODE to Always, if you want to use it while disarmed.
And it is working on 1.14 as well.

@Everglow0214
Copy link
Author

@Everglow0214 Offboard Actuator Set is correct, it got renamed to Peripheral via Actuator Set after 1.14. You have to set COM_PREARM_MODE to Always, if you want to use it while disarmed. And it is working on 1.14 as well.

Thanks for your reply.

I can arm Pixhawk now. In the code above, I set the rotor inputs to increase linearly. But the rotors' speed seem does not change. And Pixhawk disarms even when the program above is still running.

I guess I have to try a lower version of PX4.

@hamishwillee
Copy link
Collaborator

hamishwillee commented Feb 1, 2024

Thanks @bkueng - the docs look correct then thanks.
I didn't know you could/should use do set actuator to control motors for external input

@Everglow0214 Beat is saying that this should work on main and v1.14 so trying a lower version probably isn't necesssary - something else is going on.

The vehicle is probably disarming because it isn't getting the ARM command - and in normal use it will turn off so the rotors don't spin on the ground too long. You might need to disable this - see https://docs.px4.io/main/en/advanced_config/parameter_reference.html#COM_DISARM_PRFLT

If that doesn't help, please repost the query on the discussion boards - this is no longer a "docs issue".

@DronecodeBot
Copy link

This issue has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/how-to-use-mav-cmd-do-set-actuator-in-matlab/36651/1

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

4 participants