Home        Store        Learn        Blog

Question on Raw Signal Sending with Time Intervals by Pymavlinks

Hello, I have been trying to control the BlueROV2 through Pymavlinks, but I found something not right when I tried to control ‘raw’.

At the end of my program, I tried to put raw signal continuously, but the thrusters will gradually stopped even there are still raw input signal.

Another case is that if I put signals to channel 1(pitch), the motion that the BlueROV2 does not have, the raw signal after that will stop even faster.

However, this kind of ‘gradually stop’ problem will not happen when I tried to control ‘throttle’ or ‘forward’.

I guess it might be because of some safety issue, but I am not sure.

Could you please help with that?

I really appreciate that!

The following is my code:

from pymavlink import mavutil
import time
# Create the connection
master = mavutil.mavlink_connection('udpin:192.168.2.1:14550')
# Wait a heartbeat before sending commands
master.wait_heartbeat()

######################################################## function to send RC values
def set_rc_channel_pwm(id, pwm=1500):
    if id < 1:
        print("Channel does not exist.")
        return
    if id < 9:
        print("Signal Sent.")
        rc_channel_values = [65535 for _ in range(8)]
        rc_channel_values[id - 1] = pwm
        master.mav.rc_channels_override_send(
            master.target_system,                # target_system
            master.target_component,             # target_component
            *rc_channel_values)                  # RC channel list, in microseconds.

############################################################## Arm
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
    0,
    1, 0, 0, 0, 0, 0, 0)

############################################################set mode :
# Choose a mode
mode = 'ACRO'

# Check if mode is available
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)

# Get mode ID: return value should be 1 for acro mode
mode_id = master.mode_mapping()[mode]
master.mav.set_mode_send(
    master.target_system,
    mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
    mode_id)

set_rc_channel_pwm(1, 1600)
time.sleep(0.8)
set_rc_channel_pwm(1, 1500)
time.sleep(0.8)

timeout = time.time() + 5
while True:
    if time.time() > timeout:
        break
    set_rc_channel_pwm(4, 1600)
    time.sleep(0.2)
# set_rc_channel_pwm(4, 1600)
# time.sleep(3.8)
quit()

As a failsafe system you need to send heartbeats periodically, for further information: Pymavlink · GitBook

Hello Patrick,

Thanks for your quick reply, I have been following that heartbeats rules by sending the signal with frequency higher than 1HZ.

In the code I present above, there is not signal interval larger than 1s, but the raw control just gradually
stops, even the signal is still sending within the loop, where the time pause interval is 0.2s.

The same problem will not happen if I control ‘throttle’ channel or ‘forward’ channel.

This loop does not send heartbeats.

Thanks for your reply.

I am not sure if I have a correct understanding of ‘heartbeats’. It looks like the signal sent to the BlueROV2 is not the heartbeats.

I have been searching for a while for how to send heartbeats to the BlueRov2 but did not find it.

Could you please give an example of how to send the heartbeats?

Thank you so much!

You should be able to do it with:

master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS,
    mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)

I tried this. It did send the heartbeat to the BlueROV2, but I think there still a problem when I was trying to control the ‘raw’ motion (channel 4)

I tried the following code:

time_start = time.time()
while True:
    t = time.time() - time_start
    if t > 15:
        break
    # set_rc_channel_pwm(4, 1600)
    master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS,
                              mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
    if 8 < t < 9:
        print(3)
        set_rc_channel_pwm(4, 1600)

    time.sleep(0.5)
    
quit()

So ‘t’ is the current time after running the program.

In the condition if 8 < t < 9: if I set t < 7s second, the thruster will run and gradually stopped before t == 7.

For example, if here is t < 7, the thruster will run fast at first and gradually stopped; and if I set 6 < t < 7, the thruster will just run a little bit and stop; and if I set t > 7, the thruster will not run.

This problem only happens when the input signal is ‘raw’ channel ‘4’.

Could you please help me with this, thank you so much!