-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Improvements crazyflie #6107
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
Merged
knmcguire
merged 43 commits into
cyberbotics:develop
from
knmcguire:improvements-crazyflie
May 5, 2023
Merged
Improvements crazyflie #6107
Changes from 12 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
304d62a
added height control to c based controller
knmcguire 94f5db4
new proto with fast helix
knmcguire 054ee50
added a new controller with wall following
knmcguire 1082b38
added new environment with an apartement
knmcguire 08a81cd
fix the autonomy toggle
knmcguire 8924df5
tracking shot for apartement
knmcguire 04be146
changed extern proto url
knmcguire 180ab02
Merge branch 'develop' into improvements-crazyflie
omichel 915ac52
add description
knmcguire 8666172
change wbt name from typo
knmcguire de11049
Merge branch 'develop' into improvements-crazyflie
omichel be06426
Update projects/robots/bitcraze/crazyflie/protos/Crazyflie.proto
knmcguire 7d5cd93
remove fast helix of crazyflie proto
knmcguire d902958
move fast helix to default texture file
knmcguire 274f96a
formated python with pep8
knmcguire 46b4d15
clang format
knmcguire 92de77d
Remove unnessesary imports
knmcguire 2ec5315
Remove switch cases to ifelse
knmcguire 39703a5
return format of c controllers
knmcguire c00cc18
revert c files to former clang
knmcguire 727534b
manual clang pid_controller.c
knmcguire 7adcbd0
manual clang pid_controller.c
knmcguire e05d62f
manual clang pid_controller.c
knmcguire 31eca66
manual clang crazyflie, pid_controller.h
knmcguire d5c7c1f
manual clang crazyflie.c
knmcguire c14b265
manual clang crazyflie.c pid_controller.h
knmcguire f462a3d
manual clang pid_controller.h
knmcguire 83c26eb
Merge branch 'develop' into improvements-crazyflie
knmcguire 697c3d9
update apartement file
knmcguire 20625f0
Fixed indentation
omichel 4afdaf8
Merge branch 'develop' into improvements-crazyflie
omichel bd1fd3c
Merge branch 'develop' into improvements-crazyflie
knmcguire dd26625
update wall following class
knmcguire 3d59860
intergrated omichel comments
knmcguire 76d5081
fixed small name change in controller
knmcguire 5e031ce
address rest of omichel comments
knmcguire 54ab7bc
clang and pep8
knmcguire 370a9a2
remove switch case again
knmcguire ac30df6
Merge branch 'develop' into improvements-crazyflie
knmcguire c672c8f
Update projects/robots/bitcraze/crazyflie/controllers/crazyflie/pid_c…
omichel ca23b9a
Update projects/robots/bitcraze/crazyflie/controllers/crazyflie_py_wa…
omichel 966f650
Update projects/robots/bitcraze/crazyflie/controllers/crazyflie/pid_c…
omichel f19ae1e
Update projects/robots/bitcraze/crazyflie/controllers/crazyflie/pid_c…
omichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
...s/bitcraze/crazyflie/controllers/crazyflie_py_wallfollowing/crazyflie_py_wallfollowing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| # ........... ____ _ __ | ||
| # | ,-^-, | / __ )(_) /_______________ _____ ___ | ||
| # | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ | ||
| # | / ,..´ | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ | ||
| # +....... /_____/_/\__/\___/_/ \__,_/ /___/\___/ | ||
|
|
||
| # MIT License | ||
|
|
||
| # Copyright (c) 2022 Bitcraze | ||
|
|
||
| # @file crazyflie_controllers_py.py | ||
| # Controls the crazyflie motors in webots in Python | ||
|
|
||
| """crazyflie_py_wallfollowing.py controller.""" | ||
|
|
||
|
|
||
| from controller import Robot | ||
| from controller import Motor | ||
| from controller import InertialUnit | ||
| from controller import GPS | ||
| from controller import Gyro | ||
| from controller import Keyboard | ||
| from controller import Camera | ||
| from controller import DistanceSensor | ||
|
|
||
| from math import cos, sin | ||
|
|
||
| from pid_controller import pid_velocity_fixed_height_controller | ||
| from wall_following import WallFollowing | ||
|
|
||
| FLYING_ATTITUDE = 1 | ||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| robot = Robot() | ||
| timestep = int(robot.getBasicTimeStep()) | ||
|
|
||
| ## Initialize motors | ||
| m1_motor = robot.getDevice("m1_motor"); | ||
| m1_motor.setPosition(float('inf')) | ||
| m1_motor.setVelocity(-1) | ||
| m2_motor = robot.getDevice("m2_motor"); | ||
| m2_motor.setPosition(float('inf')) | ||
| m2_motor.setVelocity(1) | ||
| m3_motor = robot.getDevice("m3_motor"); | ||
| m3_motor.setPosition(float('inf')) | ||
| m3_motor.setVelocity(-1) | ||
| m4_motor = robot.getDevice("m4_motor"); | ||
| m4_motor.setPosition(float('inf')) | ||
| m4_motor.setVelocity(1) | ||
|
|
||
| ## Initialize Sensors | ||
| imu = robot.getDevice("inertial unit") | ||
knmcguire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| imu.enable(timestep) | ||
| gps = robot.getDevice("gps") | ||
| gps.enable(timestep) | ||
| gyro = robot.getDevice("gyro") | ||
| gyro.enable(timestep) | ||
| camera = robot.getDevice("camera") | ||
| camera.enable(timestep) | ||
| range_front = robot.getDevice("range_front") | ||
| range_front.enable(timestep) | ||
| range_left = robot.getDevice("range_left") | ||
| range_left.enable(timestep) | ||
| range_back = robot.getDevice("range_back") | ||
| range_back.enable(timestep) | ||
| range_right = robot.getDevice("range_right") | ||
| range_right.enable(timestep) | ||
|
|
||
| ## Get keyboard | ||
| keyboard = Keyboard() | ||
| keyboard.enable(timestep) | ||
|
|
||
| ## Initialize variables | ||
|
|
||
| past_x_global = 0 | ||
| past_y_global = 0 | ||
| past_time = 0 | ||
| first_time = True | ||
|
|
||
| # Crazyflie velocity PID controller | ||
| PID_CF = pid_velocity_fixed_height_controller() | ||
knmcguire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| PID_update_last_time = robot.getTime() | ||
| sensor_read_last_time = robot.getTime() | ||
|
|
||
| height_desired = FLYING_ATTITUDE | ||
|
|
||
| wall_following = WallFollowing(angle_value_buffer= 0.01, ref_distance_from_wall = 0.5, max_forward_speed = 0.3, init_state = WallFollowing.StateWF.FORWARD) | ||
|
|
||
| autonomous_mode = False | ||
|
|
||
|
|
||
| print("\n"); | ||
|
|
||
| print("====== Controls =======\n\n"); | ||
|
|
||
| print(" The Crazyflie can be controlled from your keyboard!\n"); | ||
| print(" All controllable movement is in body coordinates\n"); | ||
| print("- Use the up, back, right and left button to move in the horizontal plane\n"); | ||
| print("- Use Q and E to rotate around yaw "); | ||
| print("- Use W and S to go up and down\n ") | ||
| print("- Press A to start autonomous mode\n") | ||
| print("- Press D to disable autonomous mode\n") | ||
|
|
||
| # Main loop: | ||
| while robot.step(timestep) != -1: | ||
|
|
||
| dt = robot.getTime() - past_time | ||
| actual_state = {} | ||
|
|
||
| if first_time: | ||
| past_x_global = gps.getValues()[0] | ||
| past_y_global = gps.getValues()[1] | ||
| past_time = robot.getTime() | ||
| first_time = False | ||
|
|
||
| ## Get sensor data | ||
| roll = imu.getRollPitchYaw()[0] | ||
| pitch = imu.getRollPitchYaw()[1] | ||
| yaw = imu.getRollPitchYaw()[2] | ||
| yaw_rate = gyro.getValues()[2] | ||
| x_global = gps.getValues()[0] | ||
| v_x_global = (x_global - past_x_global)/dt | ||
| y_global = gps.getValues()[1] | ||
| v_y_global = (y_global - past_y_global)/dt | ||
| altitude = gps.getValues()[2] | ||
|
|
||
| ## Get body fixed velocities | ||
| cosyaw = cos(yaw) | ||
| sinyaw = sin(yaw) | ||
| v_x = v_x_global * cosyaw + v_y_global * sinyaw | ||
| v_y = - v_x_global * sinyaw + v_y_global * cosyaw | ||
knmcguire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Initialize values | ||
| desired_state = [0, 0, 0, 0] | ||
| forward_desired = 0 | ||
| sideways_desired = 0 | ||
| yaw_desired = 0 | ||
| height_diff_desired = 0 | ||
|
|
||
| key = keyboard.getKey() | ||
| while key>0: | ||
| if key == Keyboard.UP: | ||
| forward_desired += 0.5 | ||
| elif key == Keyboard.DOWN: | ||
| forward_desired -= 0.5 | ||
| elif key == Keyboard.RIGHT: | ||
| sideways_desired -= 0.5 | ||
| elif key == Keyboard.LEFT: | ||
| sideways_desired += 0.5 | ||
| elif key == ord('Q'): | ||
| yaw_desired = + 1 | ||
| elif key == ord('E'): | ||
| yaw_desired = - 1 | ||
| elif key == ord('W'): | ||
| height_diff_desired = 0.1 | ||
| elif key == ord('S'): | ||
| height_diff_desired = - 0.1 | ||
| elif key == ord('A'): | ||
| if autonomous_mode is False: | ||
| autonomous_mode = True | ||
| print("Autonomous mode: ON") | ||
| elif key == ord('D'): | ||
| if autonomous_mode is True: | ||
| autonomous_mode = False | ||
| print("Autonomous mode: OFF") | ||
| key = keyboard.getKey() | ||
|
|
||
| height_desired += height_diff_desired * dt | ||
|
|
||
| ## Example how to get sensor data | ||
| ## range_front_value = range_front.getValue(); | ||
| ## cameraData = camera.getImage() | ||
| range_front_value = range_front.getValue()/1000 | ||
| range_right_value = range_right.getValue()/1000 | ||
|
|
||
| direction = 1 | ||
| cmd_vel_x, cmd_vel_y, cmd_ang_w, state_wf = wall_following.wall_follower(range_front_value, range_right_value, yaw, direction, robot.getTime()) | ||
|
|
||
| if autonomous_mode: | ||
| sideways_desired = cmd_vel_y | ||
| forward_desired = cmd_vel_x | ||
| yaw_desired = cmd_ang_w | ||
|
|
||
| ## PID velocity controller with fixed height | ||
| motor_power = PID_CF.pid(dt, forward_desired, sideways_desired, | ||
| yaw_desired, height_desired, | ||
| roll, pitch, yaw_rate, | ||
| altitude, v_x, v_y) | ||
|
|
||
| m1_motor.setVelocity(-motor_power[0]) | ||
| m2_motor.setVelocity(motor_power[1]) | ||
| m3_motor.setVelocity(-motor_power[2]) | ||
| m4_motor.setVelocity(motor_power[3]) | ||
|
|
||
| past_time = robot.getTime() | ||
| past_x_global = x_global | ||
| past_y_global = y_global | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.