Skip to content

ArduPlane: Factor out guided airspeed cmd logic from GCS_MAVLinkPlane to ModeGuided #29862

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

Conversation

Ryanf55
Copy link
Collaborator

@Ryanf55 Ryanf55 commented Apr 22, 2025

@Ryanf55 Ryanf55 requested a review from IamPete1 April 22, 2025 15:01
@Ryanf55 Ryanf55 force-pushed the guided-factor-airspeed-command-to-mode-guided branch from f17fca7 to a092f85 Compare April 22, 2025 15:04
@Ryanf55 Ryanf55 changed the title ArduPlane: Factor out guided airspeed change logic ArduPlane: Factor out guided airspeed change logic from GCS_MAVLinkPlane to ModeGuided Apr 22, 2025
@Ryanf55 Ryanf55 changed the title ArduPlane: Factor out guided airspeed change logic from GCS_MAVLinkPlane to ModeGuided ArduPlane: Factor out guided airspeed cmd logic from GCS_MAVLinkPlane to ModeGuided Apr 22, 2025
@Ryanf55 Ryanf55 requested a review from peterbarker April 23, 2025 00:08
Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing things this way.

// same airspeed any further, if we are already doing it.
float new_target_airspeed_cm = airspeed * 100;
if ( is_equal(new_target_airspeed_cm,plane.guided_state.target_airspeed_cm)) {
return MAV_RESULT_ACCEPTED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exits early without setting acceleration and timestamp. I think you might have mentioned this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, do you want a functional change in this refactor too? I'd prefer to solve that in a separate PR with a test that verifies it's working.

@Ryanf55 Ryanf55 force-pushed the guided-factor-airspeed-command-to-mode-guided branch from d573339 to 4f0f013 Compare April 23, 2025 02:36
Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What testing has this had?

// the user wanted /maximum acceleration, pick a large value as close enough
plane.guided_state.target_airspeed_accel = 1000.0f;
if (plane.mode_guided.handle_change_airspeed(packet.param2, packet.param3)) {
return MAV_RESULT_ACCEPTED;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Apr 23, 2025

What testing has this had?

Try this python script. I played around with changing speed, sending the same command multiple times, and putting a really slow acceleration (0.25). You can also try airspeeds of 9 and 31 which are rejected. Run this against default SITL, with an added UDP output on 14551.

#!/usr/bin/env python3
from pymavlink import mavutil

# Connect to the vehicle
master = mavutil.mavlink_connection('udp:127.0.0.1:14551')  # Change if needed

# Wait for heartbeat
master.wait_heartbeat()
print(f"Connected to system {master.target_system}, component {master.target_component}")

# Define constants
MAV_CMD_GUIDED_CHANGE_SPEED = 43000
SPEED_TYPE_AIRSPEED = 0
TARGET_SPEED = 24.0  # m/s
INSTANT_ACCEL = 0.0  # 0 = change speed instantly

# Send command
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    MAV_CMD_GUIDED_CHANGE_SPEED,
    0,                      # confirmation
    SPEED_TYPE_AIRSPEED,    # param1: speed type
    TARGET_SPEED,           # param2: target speed (m/s)
    INSTANT_ACCEL,          # param3: acceleration (0 = instant)
    0, 0, 0, 0              # param4-7: empty
)

print(f"MAV_CMD_GUIDED_CHANGE_SPEED sent: airspeed {TARGET_SPEED} m/s")


import time
start_time = time.time()
timeout = 5  # seconds

while time.time() - start_time < timeout:
    msg = master.recv_match(type='COMMAND_ACK', blocking=True, timeout=1)
    if msg and msg.command == MAV_CMD_GUIDED_CHANGE_SPEED:
        result = mavutil.mavlink.enums['MAV_RESULT'][msg.result].name
        print(f"Received ACK: {result}")
        break
else:
    print("Timed out waiting for COMMAND_ACK")
    

@Ryanf55 Ryanf55 force-pushed the guided-factor-airspeed-command-to-mode-guided branch from 99d9102 to deffaf6 Compare April 23, 2025 03:06
@peterbarker
Copy link
Contributor

What testing has this had?

Try this python script. I played around with changing speed, sending the same command multiple times, and putting a really slow acceleration (0.25). You can also try airspeeds of 9 and 31 which are rejected. Run this against default SITL, with an added UDP output on 14551.

#!/usr/bin/env python3
from pymavlink import mavutil

# Connect to the vehicle
master = mavutil.mavlink_connection('udp:127.0.0.1:14551')  # Change if needed

# Wait for heartbeat
master.wait_heartbeat()
print(f"Connected to system {master.target_system}, component {master.target_component}")

# Define constants
MAV_CMD_GUIDED_CHANGE_SPEED = 43000
SPEED_TYPE_AIRSPEED = 0
TARGET_SPEED = 24.0  # m/s
INSTANT_ACCEL = 0.0  # 0 = change speed instantly

# Send command
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    MAV_CMD_GUIDED_CHANGE_SPEED,
    0,                      # confirmation
    SPEED_TYPE_AIRSPEED,    # param1: speed type
    TARGET_SPEED,           # param2: target speed (m/s)
    INSTANT_ACCEL,          # param3: acceleration (0 = instant)
    0, 0, 0, 0              # param4-7: empty
)

print(f"MAV_CMD_GUIDED_CHANGE_SPEED sent: airspeed {TARGET_SPEED} m/s")


import time
start_time = time.time()
timeout = 5  # seconds

while time.time() - start_time < timeout:
    msg = master.recv_match(type='COMMAND_ACK', blocking=True, timeout=1)
    if msg and msg.command == MAV_CMD_GUIDED_CHANGE_SPEED:
        result = mavutil.mavlink.enums['MAV_RESULT'][msg.result].name
        print(f"Received ACK: {result}")
        break
else:
    print("Timed out waiting for COMMAND_ACK")
    

An autotest would be more useful. And yes, chatgpt can generate those with a little more prompting :-)

* Precursor to ArduPilot#29829

Signed-off-by: Ryan Friedman <[email protected]>
Co-authored-by: Peter Barker <[email protected]>
@Ryanf55 Ryanf55 force-pushed the guided-factor-airspeed-command-to-mode-guided branch from 3df4d96 to c82247d Compare April 23, 2025 03:25
Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like code has been moved.

Just need to know it's been tested in its current form.

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Apr 23, 2025

Looks like code has been moved.

Just need to know it's been tested in its current form.

Retested, LGTM on c82247d

ryan@FRIEDMANR-W11:~/Dev/ardu_ws/src/ardupilot$ ./guided_airspeed.py 
Connected to system 1, component 0
MAV_CMD_GUIDED_CHANGE_SPEED sent: airspeed 18.0 m/s
Received ACK: MAV_RESULT_ACCEPTED
ryan@FRIEDMANR-W11:~/Dev/ardu_ws/src/ardupilot$ ./guided_airspeed.py 
Connected to system 1, component 0
MAV_CMD_GUIDED_CHANGE_SPEED sent: airspeed 35.0 m/s
Received ACK: MAV_RESULT_FAILED
ryan@FRIEDMANR-W11:~/Dev/ardu_ws/src/ardupilot$ git log -n 1
commit c82247dcbece28b4f5ba4d42744c51f61d0f704c (HEAD -> guided-factor-airspeed-command-to-mode-guided, origin/guided-factor-airspeed-command-to-mode-guided)
Author: Ryan Friedman <[email protected]>
Date:   Tue Apr 22 09:00:06 2025 -0600

    ArduPlane: Factor out guided airspeed change logic
    
    * Precursor to https://github.com/ArduPilot/ardupilot/pull/29829
    
    Signed-off-by: Ryan Friedman <[email protected]>
    Co-authored-by: Peter Barker <[email protected]>
    ```

Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@IamPete1 IamPete1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@peterbarker peterbarker merged commit 57c0c24 into ArduPilot:master Apr 29, 2025
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants