Lesson Applications of Robotics I - Artificial Intelligence - ثالث ثانوي

Lesson 2 Applications of Robotics I Link to digital lesson www.ien.edu.sa Revolutionizing the World with Robotics Robotics is a rapidly growing field that is revolutionizing the way people work, live, and interact with their environment. It has a wide range of applications, from industrial manufacturing to space exploration, medical procedures to home cleaning, and entertainment to military missions. A key advantage of robots is their ability to perform repetitive tasks with a high degree of accuracy and precision. Robots can work tirelessly and without error, making them ideal for tasks that are too dangerous or difficult for humans to perform. For example, in the manufacturing industry, robots are used to perform tasks such as welding, painting, and assembling products. In the medical field, robots are used to perform surgeries with greater precision, and in space exploration, robots are used to explore and study distant planets. Robotics Robotics is the study of robots, which are machines that can perform a variety of tasks autonomously, semi-autonomously, or under human control. Robotics and Simulators Two significant challenges in robotics include the cost and time required to build and test physical robots; this is where simulators come in. Simulators are widely used in robotics research, education, and industry, as they provide a cost-effective and safe way to test and experiment with robots. Simulators allow developers to create virtual environments that mimic real-world scenarios, allowing them to test their robots' abilities and performance in a variety of situations. They can simulate different weather conditions, terrains, and obstacles that robots may encounter in the real world. Simulators can also simulate the interactions between multiple robots and between robots and humans, allowing developers to study and refine the ways in which robots interact with their environment. Simulator Software that allows developers to test and refine their designs and algorithms in a virtual world before building physical robots. ورات التعليم Ministry of Education 312 2024-1446 Figure 6.5: Simulation of industrial arms

Lesson 2 Applications of Robotics I

Revolutionizing the World with Robotics

Robotics

Robotics and Simulators

Another advantage of simulators is that they allow developers to easily modify and test different robot designs and algorithms without the need for expensive hardware. This allows for faster iteration and experimentation, leading to faster development cycles and more efficient designs. In general, robotics is a rapidly growing field with a wide range of applications and simulators which play a crucial role in robotics development by allowing developers to test and refine their robot designs and algorithms in a safe, cost-effective, and efficient way. As technology continues to advance, the applications of robotics and the use of simulators are only expected to grow, paving the way for a more automated and interconnected world. Webots Webots is a powerful software tool that can be used to simulate robots and their environments and an excellent platform for introduction to the world of robotics and artificial intelligence. With Webots, students can design, simulate, and test their own robotic systems and algorithms without the need for expensive hardware. Using Webots in Al is particularly useful because it allows students to experiment with machine learning algorithms and test their performance in a simulated environment. By creating virtual robots and environments, students can explore the capabilities and limitations of Al, and learn how to program intelligent systems that can make decisions based on real-time data. You can download Webots from this link: Webots™ robot simulation https://github.com/cyberbotics/webots/releases/download/R2023a/webots-R2023a_setup.exe C:\Program Files\Webots\projects\robots\dj\/mavicworlds\/mavic 2 pro.wet (mavic) - Webots R2023a File Edit View Simulation Build Overlays Tools Help DJI Mavic 2 PRO demo IMPORTABLE EXTERNPROTO Worldinfo Viewpoint TexturedBackground TexturedBackgroundLight Floor "floor Road "road" Windmill "windmill Windmill "windmill(1)" Windmill "windmill(2) Windmill "windmill(3) SmallManor "small manor SquareManhole "manhole" CardboardBox "cardboard box" Tesla Model3Simple "vehicle" Pine "pine tree Pine "pine tree(1) Pine "pine tree(3) Pine "pine tree(4) Pine "pine tree(5) Pine "pine tree(6) Pine "pine tree(2) Forest Mavic Pro "Mavic 2 PRO وزارة التعليم Ministry of Education 2024-1446 во 0:00:04:096 0.00 000000000000 000000000 00000000000 000000000 10000 000 Figure 6.6: A Webots drone project ontrollers\mavic pro\mavicpro.c BBOQO mavic2pro.c 2 Copyright 1996-2022 Cyberbotic 34 4 Licensed under the Apache Lice 5 you may not use this file exce 6 You may obtain a copy of the L 7 http://www.apache.org/Lice 94 10 21 Unless required by applicable distributed under the License 12 THOUT WARRANTIES OR CONDITIO 13 See the License for the specif 14 Limitations under the License. 15 1/ 16 17/* 18 Description: Simplistic drone 19 20 Stabilize the robot using th - Use PID technique to stabili 21-Use a cubic function applied 22 Stabilize the camera. 23 Control the robot using the 24 "/ 25 26 #include <math.h 27 #include <stdio.h> 28 #include <stdlib.h> 29 30 include webats/robot.h> 31 32 #include 33 #include ebots/camera.hs ebots/compass.ho 34 #include <webots/gps.h> 35 Winclude webots/gyro.h> 36 include webots/inertial unit.h 37 include webots/keyboard.ho 38 #include webots/led.h> 39 #include 40 rebots/motor.ho 41 #define SIGN(x) ((x) > 0) ((x) 42 #define CLAMP(value, low, high1 ( 313

Lesson 2 Applications of Robotics I

Another advantage of simulators is that they allow developers to easily modify

Webots

Area Surveillance During this lesson and the next, you will use Webots to run a simulation of a drone patrolling over a house and you will upgrade it to detect human silhouettes to act as surveillance. The simulation consists of a drone taking off from rest on the ground and commencing a patrol around the house. In the next lesson, you will also add computer vision capabilities to the drone using its camera with the OpenCV library. This will make it possible to analyze images taken by the camera. The drone is controlled through a Python script; it is responsible for controlling all of the drone's devices, including the motors of the propellers, camera, GPS (Global Positioning System), etc. It also contains the code to synchronize all the motors to move the drone to various waypoints and stabilize it in the air. Starting with Webots Waypoint A specific geographical location in 3D space that a drone is programmed to fly to and pass through. They are used to create predefined flight paths for the drone to follow and can be set using GPS coordinates or other location-based systems. In this lesson, you will be introduced to Webots in order to become familiar with its environment. A Webots simulation consists of two components: • The definition of one or more robots and their environments in a Webots world file. • One or more controller programs for the mentioned robots. A Webots world is a 3D description of a robot's attributes. Every object is defined, including its location, orientation, geometry, appearance (such as color or brightness), physical characteristics, type, and more. Objects can contain other objects in the hierarchical systems that make up worlds. A robot might, for instance, have two wheels, a distance sensor, a joint that houses a camera, etc. A world file specifies the name of the controller that is necessary for each robot; it does not contain the controller code for the robots. Worlds are saved in ".wbt" files. Each Webots project has a "worlds" subdirectory where the ".wbt" files are stored. A Webots controller is a computer program that controls a robot specified in a world file. Any of the programming languages that Webots supports for controller development, such as C++ and Java, can be used, however for this project, you will use Python (.py). Webots launches each of the given controllers as a separate process when a simulation begins, and it associates the controller processes with the simulated robots. Although several robots can share the same controller code, each robot will run its own process. Each controller's source and binary files are stored together in a controller directory. Each Webots project contains a controller directory under the "controllers" subdirectory. The Webots Environment When you open the program you will notice several fields and windows. The key components of the Webots interface include: Menu bar: Located at the top of the interface, the menu bar provides access to various commands and options for working with the simulation, such as creating or importing a robot model, configuring the simulation environment, and running simulations. Toolbar: The toolbar is a collection of buttons located under the menu bar that provides quick access to frequently used functions, such as adding objects to the scene, starting and stopping the simulation, and changing the camera view. وزارة التعليم Ministry of Education 314 2024-1446

Lesson 2 Applications of Robotics I

Area Surveillance

Waypoint

Starting with Webots

The Webots Environment

Scene tree: The scene tree is a hierarchical representation of the objects in the simulated environment. It allows users to easily navigate and manipulate the scene, such as by adding or deleting objects, changing object properties, or grouping objects for easier management. Field editor: The field editor is a graphical interface for editing the properties of objects in the simulated environment. Users can use it to adjust object parameters such as position, orientation, size, material, and physical properties. 3D window: The 3D window is the main view of the simulated environment, showing the objects and their interactions in a 3D space. Users can navigate the 3D window using various camera controls, such as pan, zoom, and rotate. Text editor: The text editor is a tool for editing source code or other text-based files used in the simulation. It provides syntax highlighting and other helpful features for writing and debugging code, such as auto-completion and error highlighting. Console: The console is a window that displays text-based output from the simulation, including error messages and debugging information. It is useful for troubleshooting problems that may arise during the simulation. Menu bar Toolbar Text editor C:\Program Files\Webots\proje tilvehicles\worlds\city.wbt (vehicles) Webots R2023 File Edit View Simulation Build Overlays Tools Help City 0:00:04:830 0.00▸ IMPORTABLE EXTERNPROTO StraightRoadSegment "road(5) Curved RoadSegment "road(6)" StraightRoadSegment "road(?) Roadintersection "road intersection(1) StraightRoadSegment "road(8) O CurvedRoadSegment "road(9) StraightRoadSegment "road(10/ CurvedRoadSegment "road(11) StraightRoadSegment "road(12) Curved RoadSegment "road(13)" StraightRoadSegment "road(14) Curved RoadSegment "road(15) BwX5 "vehicle" Generic TrafficLight "generic traffic light" CrossRoads TrafficLight "cross road traffic light" DEF STONES Solid BuildingUnderConstruction "building under construction CommercialBuilding "commercial building Selection: BmwX5 (Robot) Node Mass Position Velocity Scene tree excluding descendants mass 2000 kg density CoM relative 202.769 kg/m^3 1.2975 0 1125.07 0 Field editor 0.1 846.89 Inertia matric 4473.54 0 846.89 D 4346.1 rollers autonomous vehicle autonomous vehicle.c D autonomous vehicle.c 49 se//enabe various features" 51 bool enable collision avoidance = false; 52 bool enable display = false; 53 bool has gps-false; 54 bool has caners false; 55 56/conera 57 bDeviceTag camera; 58 Lnt camera_width-1; 59 int camera_height =-11 60 double camera_fov-1.0; 61 62 // SICK Loser 63 bDeviceTag sick; 64 int sick width = -1; 65 double sick range-1.0; 66 double sick fov - -1.0; 67 68 // speedometer 69bDeviceTag display; 70 int display_width=0; 71 int display_height = 0; 72 lbImageRef speedometer image - NULL; 73 74// GPS 75 WbDeviceTag gps 76 double gps_coords[3] (e.e, e.e, 0.0); 77 double gps speed - 8.0; 76 79 // misc variables 30 double speed 0.01 81 double steering angle = 0.0; 82 int manual steering; 83 bool autodrive true 3D window 84 85 veid print help() { 86 printf("You can drive this can!\n"); 87 printf("Select the 30 window and then use th Console - All INFO: autonomous vehicle: Starting controller: "C:\Program Files\Webots \projects\vehicles \controllers\autonomous vehicle\autonomous vehicle.exe" INFO: generic traffic light: Starting controller: "C:\Program Files\Nebots\projects\objects\traffic\controllers\generic traffic light generic traffic light.exe" 10 10 r INFO: crossroads traffic lights: Starting controller: "C:\Program Files\Webots \projects\objects\traffic\controllers\crossroads traffic lights\crossroads traffic lights.exe" setting speed to 50 km/h You can drive this carl Select the 3D window and then use the cursor keys to: [LEFT]/[RIGHT] - steer [UP]/[DOWN]-accelerate/slow down وزارة التعليم Ministry of Education 2024-1446 Console Figure 6.7: The Webots window 315

Lesson 2 Applications of Robotics I

Scene tree: The scene tree is a hierarchical representation of the objects in the simulated environment.

Structure Bookmarks Project First, you have to install the necessary libraries you will use in your project. To install the OpenCV library via PyCharm: To install OpenCV: > In the PyCharm window, click on Packages. > Type opencv in the search bar. 2 > Select opencv-python 3 and click Install. 4 > A message will inform you that the installation is done. 5 main.py File Edit View Navigate Code Refactor Run Tools VCS Window Help pythonProject pythonProject-main.py main main.py Python Packages a opencv Installed (0 found) PyPI (78 found) opencv-python opencv-python-headless opencv-contrib-python opencv-contrib-python-headless 2 Add Package 3 opencv-pyth... downloads 241M 5 Packages installed successfully Installed packages: 'openo-python' 4 latest Install opencve opencv-pg opencvFPS opencvpro opencv-cam opencv-lav opencv-log opencv-mat opencvzone opencv-cam2 AAA m Version Control 1 Python Packages TODO OpenCV on Wheels Pre-built CPU-only OpenCV packages for Python. Check the manual build section if you wish to compile the bindings from source to enable additional modules such as CUDA. Installation and Usage 1. If you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. Python Console Problems Terminal Shared indexes for Python package "openpyxl-3.0.10" are downloaded (414,86 kB in 786 ms) (10 minutes ago) Services وزارة التعليم Ministry of Education 316 2024-1446 1:1 CRLF UTF-8 4 spaces Python 3.7 (pythonProject) Figure 6.8: Installing OpenCV Likewise, you can install the Pillow library, by searching for "pillow". Notifications

Lesson 2 Applications of Robotics I

First, you have to install the necessary libraries you will use in your project. To install the OpenCV library via PyCharm:

عنوان جديد 2

Let's take a look at the project! First you will have to find and load the Webots world file: To open a Webots world: > Click File > Open World... from the Menu bar. > Find the drone_world.wbt file in the worlds directory and click Open. ③ 3 2 C:\Program Files\Webots\projects\vehicles\worlds\city.wbt (vehicles) - Webots R2023a File Edit View Simulation Build Overlays Tools Help New Open World... 1 Shift+0 0:00:00:000 0.00x © < Open Recent World Open Sample World... Save World Ctrl+Shift+S Save World As... Reload World Ctrl+Shift+R Reset Simulation Ctrl-Shift-T New Text File Ctrl-N Open Text File... Ctrl-O Save Text File Ctrl+S Save Text File As... Save All Text Files Revert Text File Ctrl-R Print Preview... Print... Open World File Take Screenshot. This PC Documents A12_03_Drone > worlds Search worlds Make Movie... Share... Organize▾ New folder Exit Name 2 Quick access Curved RoadS StraightRoadS drone_world.wbt Date modified Type Size 30/3/2023 11:35 π.μ. WBT File 5 KB Creative Cloud Files Curved RoadS This PC StraightRoadS Curved RoadS Network BmwX5 "vehi Generic Traffic وزارة التعليم Ministry of Education 2024-1446 File name: drone world.wbt Figure 6.9: Opening a Webots world X 3 Wo ("wbt "WBT) Open Cancel 317

Lesson 2 Applications of Robotics I

Let's take a look at the project! First you will have to find and load the Webots world file:

Next open the Python script file that will be used to control the drone: To open a controller script: > Click File > Open Text File... from the Menu bar. 1 > Find the drone_controller.py file in the controllers\ drone_controller directory 2 and click Open. ③ C:\Program Files\Webots\projects\vehicles\worlds\city.wbt (vehicles) - Webots R2023a File Edit View Simulation Build Overlays Tools Help New Open World... Ctrl+Shift+0 0 0:00:00:000 0.00x KI Open Recent World Open Sample World... Save World Ctrl+Shift+S Save World As... Reload World Ctrl+Shift+R Reset Simulation Ctrl+Shift+T V New Text File Open Text File... Save Text File Save Text File As... Save All Text File Revert Text File Ctrl-N 1-0 Ctrl-S Open File... This PC > Documents A12 U3_Drone controllers > drone_controller Search drone controller Print Preview... Print... Organize▾ New folder 2 Take Screenshot > Quick access Make Movie... PC Creative Cloud Files Share... Exit This PC Curved Roads > Network drone_controller. py haarcascade_full body.xml StraightRoads Curved RoadS StraightRoads File name: drone controller.py Figure 6.10: Opening a Webots controller script 3 All Open Cancel Object Position and Rotation X, Y, and Z are three-dimensional coordinates used to represent the position of an object in space. X represents the horizontal axis, Y represents the vertical axis, and Z represents the depth axis. They are similar to the real world coordinates of latitude, longitude and altitude, used to describe a location on Earth. Pitch, roll, and yaw are rotational orientations that can be used to describe the movement of an object relative to a reference frame, as shown in figure 6.11. Pitch is the rotation of an object around its X-axis, which tilts the object up or down relative to a horizontal plane. Roll is the rotation of an object around its Y-axis, which tilts the object sideways or from side to side. Yaw is the rotation of an object around its Z-axis, which turns the object left or right relative to a reference frame. •● which turns the o Together, these six values (X, Y, Z, pitch, roll, and yaw) can be used to describe the position and orientation of an object in three-dimensional space. They are commonly used in robotics, navigation systems, and other pill applications that require precise positioning and control. Magtry of Education 2024-1446

Lesson 2 Applications of Robotics I

Next open the Python script file that will be used to control the drone:

Object Position and Rotation

Drone Devices The drone is equipped with several sensors, allowing it to collect input from its environment. getDevice() and enable() are functions provided by the simulator to interface with various sensors and actuators of a simulated robot. The getDevice() function is used to get readings from a device, such as a sensor or an actuator, from the Webots robot model. It takes a string argument that specifies the name of the device to be accessed. The enable() function is used to activate a device so that it can start providing data or performing an action. Pitch Axis Roll Axis Camera GPS Yaw Axis Figure 6.11: Rotational axes IMU The IMU (Inertial Measurement Unit) can measure the drone's linear acceleration and angular velocity; it measures forces such as gravity, in addition to the rotational forces acting on the drone. It can provide information about the drone's attitude (pitch, roll, and yaw), which is critical for stabilization and control. The GPS (Global Positioning System) is a satellite-based navigation system that provides precise location information to the drone. GPS enables the drone to know its current position, altitude, and velocity relative to the earth. This information is important for drone navigation and control. Sensors are devices that detect physical quantities or environmental conditions and measure it, and convert them into an electrical signal for monitoring or control. Actuators are devices that convert electrical signals into mechanical motion to perform a specific action or task. Gyroscope In contrast to linear speed, which measures the distance traveled in unit time, angular speed is a measure of the change in the central angle of a rotating object with respect to time. It is usually measured in radians per second (rad/s) or degrees per second (°/s). Figure 6.12: Drone with ⚫sensors and camera وزارة التعليم Ministry of Education 2024-1446 319

Lesson 2 Applications of Robotics I

Drone Devices

The gyroscope is a sensor that measures angular velocity, or the rate of rotation around a specific axis. The gyroscope is especially useful in detecting and correcting small changes in the drone's orientation, which is important for maintaining stability and control during flight. The drone's camera will be used to capture images during flight. It can be mounted on the drone and by adjusting the camera pitch angle with the setPosition() function, the drone can capture images from different perspectives and angles. In this project, the position is set to 0.7, which is about 45 degrees looking downwards. The drone's four propeller devices are actuators that control the rotational speed and direction of the quadcopter. Quadcopters are drones that are equipped with four rotors, with two rotors rotating clockwise and the other two rotating counterclockwise. The rotation of these rotors. generates lift and allows the drone to take off and maneuver in the air. The motors are retrieved and set into position in the same way as the other devices and the setVelocity() function is also used to set an initial velocity to the propellers. Moving to a Target Propellers Figure 6.13: Four propeller drone device To move from one location to the other, the drone uses the move_to_target() function, which contains the control logic. It takes a list of coordinates as argument, in the form of pairs (x, y), to be used as waypoints. At first, it checks if the target position has been initialized, and if not, sets it to the first waypoint. Then, it checks if the drone has reached the target position with a precision of target_precision, and if so, the function proceeds to the next target waypoint. The angle between the current position of the drone and its target position has to be computed, in order to know how sharp it has to turn in the next step. This value is also normalized to the range of [-π, π]. Next, it computes the yaw and pitch disturbances required to turn the drone towards the target waypoint and adjust the drone's pitch angle, respectively. Motor Calculations Lastly, the velocity that has to be set to the motors must be calculated. This is done by initially reading the sensor values for the the roll, pitch and yaw from the IMU, and getting values of the x, y and z positions from the GPS while getting values of the roll and pitch accelerations from the gyroscope. The various constants defined early in the code are used to make calculations and adjustments in conjunction with the sensor, inputs and finally the correct thrust is set. INFORMATION By controlling the speed and direction of these four propellers, the quadcopter can move in any direction and maintain stable flight. For example, by increasing the speed of the two rotors on one side and decreasing the speed of the other two rotors, the drone can tilt and move in a specific direction. وزارة التعليم Ministry of Education 320 2024-1446

Lesson 2 Applications of Robotics I

The gyroscope is a sensor that measures angular velocity, or the rate of rotation around

Moving to a Target

Motor Calculations

By controlling the speed and direction of these four propellers, the quadcopter can move in

from controller import Robot import numpy as np # used for mathematic operations import os # used for folder creation import cv2 # used for image manipulation and human detection from PIL import Image # used for image object creation from datetime import datetime # used for date and time # auxiliary function used for calculations def clamp(value, value_min, value_max): return min(max(value, value_min), value_max) class Mavic (Robot): # constants of the drone used for flight # thrust for the drone to lift K_VERTICAL_THRUST = 68.5 # vertical offset the drone uses as targets for stabilization K_VERTICAL_OFFSET = 0.6 K_VERTICAL_P = 3.0 K_ROLL_P 50.0 = K_PITCH_P = 30.0 MAX_YAW_DISTURBANCE = 0.4 MAX_PITCH DISTURBANCE = -1 # P constant of the vertical PID # P constant of the roll PID # P constant of the pitch PID # precision between the target position and the drone position in meters target_precision = 0.5 def __init__(self): The controller library contains the Robot class, whose methods will be used to control the drone. Imports of libraries needed for calculations and processing Constants found empirically used in calculations for flight and stabilization # initializes the drone and sets the time interval between updates of the simulation Robot.__init__(self) self.time_step = int(self.getBasicTimeStep()) # gets and enables devices self.camera = self.getDevice("camera") self.camera.enable(self.time_step) self.imu = self.getDevice("inertial unit") self.imu.enable(self.time_step) self.gps = self.getDevice("gps") self.gps.enable(self.time_step) self.gyro = self.getDevice("gyro") self.gyro.enable(self.time_step) self.camera_pitch_motor = self.getDevice("camera pitch") self.camera_pitch_motor.setPosition(0.7) self.front_left_motor = self.getDevice("front left propeller") self.front_right_motor = self.getDevice("front right propeller") self.rear left_motor = self.getDevice("rear left propeller") self.rear_right_motor = self.getDevice("rear right propeller") motors = [self.front_left_motor, self.front_right_motor, self.rear_left_motor, self.rear_right_motor] for motor in motors: #mass initialization of the four motors motor.setPosition(float('inf')) motor.setVelocity (1) وزارة التعليم Ministry of Education 2024-1446 321

Lesson 2 Applications of Robotics I

The controller library contains the Robot class, whose methods will be used to control the drone.

self.current_pose = 6* [0] #X, Y, Z, yaw, pitch, roll self.target_position = [0, 0, 0] Initialization of the drone's position (x, y, z) and rotation (roll, pitch, yaw) self.target_index = 0 self.target_altitude = 0 def move_to_target(self, waypoints): #Moves the drone to the given coordinates # Parameters: # waypoints (list): list of X,Y coordinates # Returns: # # yaw_disturbance (float): yaw disturbance (negative value to go on the right) pitch_disturbance (float): pitch disturbance (negative value to go forward) if self.target_position [0:2] == [0, 0] #initialization self.target_position [0:2] = waypoints[0] # if the drone is at the position with a precision of target_precision if all([abs(x1 - x2) < self.target_precision for (x1, x2) in zip(self.target_position, self.current_pose [0:2])]): self.target_index += 1 if self.target_index> len(waypoints) - 1: Θ self.target_index = 0 self.target_position [0:2] = waypoints [self.target_index] # computes the angle between the current position of the drone and its target position # and normalizes the resulting angle to be within the range of [-pi, pi] self.target_position[2] = np.arctan2( self.target_position[1] - self.current_pose[1], self.target_position[0] - self.current_pose[0]) angle_left = self.target_position [2] - self.current_pose[5] angle_left = (angle_left + 2 * np.pi) % (2 * np.pi) if (angle_left > np.pi): angle_left = 2 * np.pi # turns the drone to the left or to the right according to the value # and the sign of angle_left and adjusts pitch_disturbance yaw_disturbance = self.MAX_YAW_DISTURBANCE * angle_left / (2 * np.pi) pitch_disturbance = clamp ( np. log10(abs(angle_left)), self.MAX_PITCH_DISTURBANCE, 0.1) return yaw_disturbance, pitch_disturbance def run(self): # time intevals used for adjustments in order to reach the target altitude t1 = self.getTime() roll_disturbance = 0 pitch disturbance = 0 yaw_disturbance = 0 وزارة التعليم Ministry of Education 322 2024-1446

Lesson 2 Applications of Robotics I

Initialization of the drone's position (x, y, z) and rotation (roll, pitch, yaw)

# specifies the patrol coordinates waypoints = [[-30, 20], [-60, 30], [-75, 0], [-40, -10]] # target altitude of the drone in meters self.target_altitude = 8 while self.step(self.time_step) != -1: #reads sensors The waypoints of the route the drone will be flying roll, pitch, yaw = self.imu.getRoll PitchYaw() x_pos, y_pos, altitude = self.gps.getValues() roll_acceleration, pitch_acceleration, _ = self.gyro.getValues() self.current_pose = [x_pos, y_pos, altitude, roll, pitch, yaw] if altitude self.target_altitude - 1: # as soon as it reaches the target altitude, # computes the disturbances to go to the given waypoints if self.getTime() - t1 > 0.1: yaw_disturbance, pitch_disturbance waypoints) t1 = self.getTime() # calculates the desired input values for roll, pitch, yaw, = self.move_to_target( # and altitude using various constants and disturbance values roll_input = self.K_ROLL_P * clamp(roll, −1, 1) + roll_acceleration + roll_disturbance pitch_input = self.K_PITCH_P * clamp(pitch, -1, 1) pitch acceleration + pitch_disturbance yaw_disturbance yaw_input = + - clamped difference_altitude = clamp(self.target_altitude altitude self.K_VERTICAL_OFFSET, -1, 1) vertical_input = self.K_VERTICAL_P * pow(clamped_difference_altitude, 3.0) # calculates the motors' input values based on the #desired roll, pitch, yaw, and altitude values front_left_motor_input = self.K_VERTICAL_THRUST + vertical_input - yaw_input + pitch_input pitch_input - roll_input front_right_motor_input = self.K_VERTICAL_THRUST + vertical_input + yaw_input + pitch_input + roll_input rear_left_motor_input = self.K_VERTICAL_THRUST + vertical_input +yaw_input pitch_input - roll input self.K_VERTICAL_THRUST + vertical_input yaw_input pitch_input + roll_input rear_right_motor_input = - - # sets the velocity of each motor based on the motors' input values calculated above self.front_left_motor.setVelocity(front_left_motor_input) self.front_right_motor.setVelocity (-front_right_motor_input) self.rear_left_motor.setVelocity(-rear_left_motor_input) self.rear_right_motor.setVelocity (rear_right_motor_input) robot = Mavic() robot.run() وزارة التعليم Ministry of Education 2024-1446 323

Lesson 2 Applications of Robotics I

The waypoints of the route the drone will be flying

Now it's time to insert the script into the drone and run the simulation: To insert a controller script and run the simulation: > Click Mavic2Pro "Mavic 2 Pro" in the Scene tree 1 and click controller "mavic2pro_patrol". ② > Click Select... in the Field editor. 3 Select drone_controller 4 and click OK. 5 > Click Run the simulation in real-time from the Toolbar. 6 When making changes to your scripts, do not forget to save them by pressing Ctrl + S. File Edit View Simulation Build Overlays Tools Help DJI Mavic 2 PRO demo © 0:00:00:000 0.00x K IMPORTABLE EXTERNPROTO WorldInfo Viewpoint Textured Background Textured BackgroundLight Floor "floor" Pedestrian "pedestrian(2)" Pedestrian "pedestrian(1)" Road "road" Pedestrian "pedestrian" Windmill "windmill Windmill "windmill(1)" Windmill "windmill(2)" SmallManor "small manor" SquareManhole "manhole" CardboardBox "cardboard box" Tesla Model3Simple "vehicle" Forest Mavic2Pro "Mavic 2 PRO" 1 translation 0.00531-1.9e-07 0.0857 rotation 0.0349 7.3e-07 0.999-3.14 name "Mavic 2 PRO" controller "mavic2pro_patrol" 2 Controller choice 6 Please select a controller from the list (it will start at the next time step) <none> <extern> <generic> braitenberg drone_controller 4 mavic2pro mavic2pro_patrol sumo_supervisor 5 SK OK Cancel controllerArgs window "<generic>" customData" supervisor FALSE synchronization TRUE battery bodySlot > cameraSlot Selection: controller (String) mavic2pro_patrol 3 Select... Edit وزارة التعليم Ministry of Education 324 2024-1446 4 Figure 6.14: Inserting the controller script and running the simulation

Lesson 2 Applications of Robotics I

Now it's time to insert the script into the drone and run the simulation:

When you start the simulation, the drone's motors will power up and it will take off. Then it will follow the predetermined route around the house, passing through the waypoints. وزارة التعليم Ministry of Education 2024-1446 Human objects have been pre-positioned in the Webots environment, to serve as your detection objective. Figure 6.15: The drone taking off 325

Lesson 2 Applications of Robotics I

When you start the simulation, the drone's motors will power up and it will take off. Then it will follow the predeterminded route around the house, passing through the waypoints.

Exercises 1 Analyze the move_to_target() function and explain how the drone calculates its next position in the waypoints list. How can the drone's trajectory be optimized to minimize flight time between the waypoints? 2 Evaluate the limitations of the current drone control algorithm when faced with external factors such as wind, obstacles, or GPS inaccuracies. Propose and discuss improvements to the control algorithm that would make the drone more resilient to these challenges. وزارة التعليم Ministry of Education 326 2024-1446

Lesson 2 Applications of Robotics I

Analyze the move_to_target() function and explain how the drone calculates its next position in the waypointslist. How can the drone'strajectory be optimized to minimize flight time between t

Evaluate the limitations ofthe current drone control algorithm when faced with external factors such as wind, obstacles, or GPS inaccuracies.

3 Explore the ethical implications of autonomous aerial drones in real-world applications, such as surveillance, package delivery, and search-and-rescue operations. Write down potential privacy concerns, safety issues, and the potential for misuse of this technology. 4 Add a feature that logs the drone's position, altitude, and orientation at regular intervals during its flight. Write down any patterns that you may find in the log data. 5 Experiment with different values for the PID controller constants (K_VERTICAL_P, K_ROLL_P, and K_PITCH_P). Observe how these changes affect the drone's stability and responsiveness. Discuss the trade-offs between stability and responsiveness. وزارة التعليم Ministry of Education 2024-1446 327

Lesson 2 Applications of Robotics I

Explore the ethical implications of autonomous aerial dronesin real-world applications, such as surveillance, package delivery, and search-and-rescue operations.

Add a feature thatlogsthe drone's position, altitude, and orientation atregularintervals during its flight. Write down any patterns that you may find in the log data.

Experiment with different values for the PID controller constants (K_VERTICAL_P, K_ROLL_P, and K_PITCH_P).