My BLDC motor which is connected to a Pixhawk X7+ runs using QGroundControl and ran once using the following code but no longer runs. It not only arms but runs properly when using QGC. The Pixhawk has ArduSub flight stack and the code is in Python.
The code:
import time
from pymavlink import mavutil
# Create the connection
master = mavutil.mavlink_connection('/dev/ttyACM1')
# Wait a heartbeat before sending commands
master.wait_heartbeat()
# https://mavlink.io/en/messages/common.html#MAV_CMD_COMPONENT_ARM_DISARM
# Arm
# master.arducopter_arm() or:
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)
# wait until arming confirmed (can manually check with master.motors_armed())
print("Waiting for the vehicle to arm")
master.motors_armed_wait()
print('Armed!')
# Initialize the motor with a specific PWM value
motor_number = 1 # Motor number
initial_pwm = 1100 # Initial PWM value to initialize the motor
print(f"Initializing motor {motor_number} with PWM: {initial_pwm}")
msg = master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_SERVO, 0, motor_number, initial_pwm, 0, 0, 0, 0, 0)
# Allow the motor to initialize for a moment
print("Motor initializing... (2 seconds)")
time.sleep(2) # Adjust this duration as needed
# Run the motor at the desired PWM value (adjust as needed)
motor_speed = 1500 # Adjust this value to control the motor speed
print(f"Running motor {motor_number} at PWM: {motor_speed}")
msg = master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_SERVO, 0, motor_number, motor_speed, 0, 0, 0, 0, 0)
time.sleep(10)
# Disarm
# master.arducopter_disarm() or:
master.mav.command_long_send(
master.target_system,
master.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
0,
0, 0, 0, 0, 0, 0, 0)
# wait until disarming confirmed
master.motors_disarmed_wait()
print("Disarmed")