-
Notifications
You must be signed in to change notification settings - Fork 0
/
scm_example
57 lines (44 loc) · 1.7 KB
/
scm_example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import dronekit
import time
import math
def sliding_control_mode():
# Connect to the drone
vehicle = dronekit.connect("/dev/serial0", baud=57600, wait_ready=True)
# Arm the drone
vehicle.armed = True
# Takeoff to a target altitude
target_altitude = 10 # meters
vehicle.simple_takeoff(target_altitude)
# Wait until the drone has reached the target altitude
while vehicle.location.global_relative_frame.alt < target_altitude:
time.sleep(0.1)
# Define the sliding surface and control gains
sliding_surface = [0, 0, 1, 0, 0, 0]
kp = [1, 1, 1, 1, 1, 1]
kd = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
# Start the control loop
while True:
# Get the current state of the drone
position = vehicle.location.global_relative_frame
velocity = vehicle.velocity
attitude = vehicle.attitude
# Calculate the error between the current state and the desired state
error = [position.x - target_position.x,
position.y - target_position.y,
position.z - target_altitude,
attitude.roll,
attitude.pitch,
attitude.yaw]
# Calculate the control inputs using the sliding control algorithm
u = -kp * error - kd * velocity
u = [math.clip(val, -1, 1) for val in u]
# Send the control inputs to the drone
vehicle.send_mavlink(u)
# Sleep for a short time to control the loop frequency
time.sleep(0.01)
# Land the drone
vehicle.mode = dronekit.VehicleMode("LAND")
# Close the connection to the drone
vehicle.close()
# Call the function to implement the sliding control mode
sliding_control_mode()