droneProject
Loading...
Searching...
No Matches
droneRobot.DroneRobot Class Reference
Inheritance diagram for droneRobot.DroneRobot:
Collaboration diagram for droneRobot.DroneRobot:

Public Member Functions

 __init__ (self)
 
 update_motor_power (self, current_location)
 
 axis_angle_to_euler (self, axis_angle)
 
 get_observations (self)
 
 get_default_observation (self)
 
 get_reward (self, action=None)
 
 is_done (self)
 
 solved (self)
 
 get_info (self)
 
 launchTensorBoard (self, log_path)
 
 startTensorBoard (self, log_path)
 
 render (self, mode='human')
 
 step (self, action)
 
 reset (self)
 Guess what, another override function.
 
 apply_action (self, action)
 

Public Attributes

bool debugMode = False
 
int randomSeed = 42
 
bool saveOK = False
 
int location_bound = 5
 
int angle_bound = 90
 
int max_velocity = 150
 
int avg_target_score = -20
 
int previous_velocity = 0
 
list target_location = [0,0,2]
 
dict past_errors = {"x": 0, "y": 0, "z": 0}
 
dict integral_errors = {"x": 0, "y": 0, "z": 0}
 
int past_yaw_error = 0
 
int last_ep_score = 0
 
bool didOnce = False
 
int actionCount = 0
 
 timestep = self.getBasicTimeStep()
 
int dt = 1/200
 
int cur_step_count = 0
 
int steps_per_episode = 3000
 
int episode_score = 0
 
list episode_score_list = []
 
int yaw_gain = 0
 
dict pid_gains
 
dict motor_power
 
 observation_space
 
 action_space
 
 robot = self.getSelf()
 
bool stepFlag = True
 
 m1_motor = self.getDevice('m1_motor')
 Needs to be defined in step to work properly.
 
 m2_motor = self.getDevice('m2_motor')
 
 m3_motor = self.getDevice('m3_motor')
 
 m4_motor = self.getDevice('m4_motor')
 
 imu = self.getDevice('inertial_unit')
 
 gps = self.getDevice('gps')
 
 gyro = self.getDevice('gyro')
 

Detailed Description

Definition at line 28 of file droneRobot.py.

Constructor & Destructor Documentation

◆ __init__()

droneRobot.DroneRobot.__init__ ( self)
@brief Initializes the DroneRobot environment and sets default parameters.

This constructor initializes the simulation environment, sets up the observation and action spaces,
and defines key parameters such as PID gains, motor power, and episode configurations.
Additionally, it configures the random seed for reproducibility and sets up the simulation timestep.

@details
- Initializes debug mode, random seed, and task status.
- Configures observation and action spaces using Gym's `Box`.
- Sets up PID gains with random initial values and motor power to zero.
- Defines simulation parameters such as timestep and maximum steps per episode.
- Maintains error tracking for the PID controller.

@note The method assumes that the superclass `RobotSupervisorEnv` is correctly implemented.

Definition at line 30 of file droneRobot.py.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ apply_action()

droneRobot.DroneRobot.apply_action ( self,
action )
     @brief Applies the specified action to the drone by updating PID controller gains.
    
     This method sets the PID gains based on the provided action. If debug mode is active,
     the action is ignored, and default PID gains are used instead.
    
     @param action A list or array containing PID gain values for X, Y, and Z axes in the order:
                   [X_P, X_I, X_D, Y_P, Y_I, Y_D, Z_P, Z_I, Z_D].
    
     @details
     - In debug mode, fixed PID gains are applied, and the action parameter is ignored.
     - Outside debug mode, the PID gains are updated directly from the action input.
     - Resets the last episode score to zero after applying the action.
    
     @note Ensure that the action parameter contains exactly 9 elements representing the PID gains.

Definition at line 671 of file droneRobot.py.

◆ axis_angle_to_euler()

droneRobot.DroneRobot.axis_angle_to_euler ( self,
axis_angle )
     @brief Converts an axis-angle representation to Euler angles in degrees.
    
     This method takes a 4D vector representing an axis-angle (x, y, z, angle)
     and converts it to the corresponding Euler angles (roll, pitch, yaw) in degrees.
    
     @param axis_angle A list or tuple containing the axis components (x, y, z) and the rotation angle.
     @return A tuple (roll, pitch, yaw) representing the Euler angles in degrees.
    
     @details
     - Normalizes the axis vector if its magnitude is non-zero.
     - Converts the axis-angle to a quaternion and then to Euler angles.
     - Handles edge cases to ensure angles are properly constrained.
    
     @note The input angle is expected in radians.

Definition at line 234 of file droneRobot.py.

Here is the caller graph for this function:

◆ get_default_observation()

droneRobot.DroneRobot.get_default_observation ( self)
     @brief Provides a default observation vector.
    
     This method returns a default observation consisting of zero values, which matches the shape
     of the observation space.
    
     @return A list of zeros with a length equal to the number of dimensions in the observation space.
    
     @details
     - The default observation is used as a placeholder or initialization value when no meaningful
       observation data is available.

Definition at line 323 of file droneRobot.py.

◆ get_info()

droneRobot.DroneRobot.get_info ( self)
     @brief Provides additional information about the environment.
    
     This method returns a dictionary containing environment-specific details.
    
     @return A dictionary with key-value pairs representing additional environment information.
    
     @details
     - Currently, this method returns a placeholder dictionary `{"Dummy": "dummy"}`.
     - Can be extended to include meaningful diagnostic or metadata information.

Definition at line 488 of file droneRobot.py.

◆ get_observations()

droneRobot.DroneRobot.get_observations ( self)
     @brief Retrieves the current state of the drone as observations.
    
     This method gathers the drone's current position, orientation, and velocity, and combines them
     into a single observation vector.
    
     @return A numpy array containing the concatenated observations: 
             [position (x, y, z), rotation (roll, pitch, yaw), velocity (linear and angular)].
    
     @details
     - The position is obtained from the `translation` field of the robot.
     - The rotation is converted from axis-angle representation to Euler angles using `axis_angle_to_euler`.
     - The velocity includes both linear and angular components.
    
     @note Assumes that the Webots simulation provides the required fields and velocity information.
     @see axis_angle_to_euler

Definition at line 290 of file droneRobot.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_reward()

droneRobot.DroneRobot.get_reward ( self,
action = None )
     @brief Computes the reward based on the drone's current state and actions.
    
     This method evaluates the drone's performance in the environment by calculating a composite reward
     that combines various factors such as distance to the target, stability, efficiency, and task completion.
    
     @param action (Optional) The action taken by the drone, though not directly used in the current implementation.
     @return A float value representing the computed reward for the current timestep.
    
     @details
     - **Distance to Target:** A quadratic penalty based on the squared distance to the target location.
     - **Stability Reward:** Penalizes abrupt changes in velocity.
     - **Efficiency Reward:** Adds a small time penalty to encourage faster task completion.
     - **Stay Penalty:** Penalizes the drone for staying near the origin after a certain number of steps.
     - **Direction Reward:** Rewards the drone for moving in the direction of the target.
     - **Goal Bonus:** Provides a significant reward for reaching the target location.
     - **Time Penalty:** Encourages efficiency by applying a small penalty for each timestep.
    
     @note This function assumes observations include position and velocity as described in `get_observations`.

Definition at line 342 of file droneRobot.py.

Here is the call graph for this function:

◆ is_done()

droneRobot.DroneRobot.is_done ( self)
     @brief Checks whether the current episode is complete.
    
     This method evaluates conditions to determine if the episode should terminate, such as exceeding
     step limits, going out of bounds, or exceeding angular constraints.
    
     @return `True` if the episode is complete, `False` otherwise.
    
     @details
     - **Step Count Trigger:** The episode ends if the current step count exceeds the maximum allowed steps.
     - **Location Bound Trigger:** The episode terminates if the drone's position deviates beyond a predefined
       bound from the target location on any axis.
     - **Angle Bound Trigger:** The episode terminates if any rotational angle (roll, pitch, yaw) exceeds
       the specified angular limit.
    
     @note Observations are fetched from `get_observations`, and positional and angular bounds are defined
           within the function.

Definition at line 411 of file droneRobot.py.

◆ launchTensorBoard()

droneRobot.DroneRobot.launchTensorBoard ( self,
log_path )
     @brief Launches TensorBoard for monitoring training progress.
    
     This method runs a system command to start TensorBoard using the specified log directory.
    
     @param log_path The path to the directory containing TensorBoard log files.
    
     @details
     - Executes the command `tensorboard --logdir=<log_path>` to launch TensorBoard.
     - Requires TensorBoard to be installed and accessible from the system's command line.
    
     @note Ensure the specified `log_path` is valid and contains log files for visualization.

Definition at line 505 of file droneRobot.py.

Here is the caller graph for this function:

◆ render()

droneRobot.DroneRobot.render ( self,
mode = 'human' )
     @brief Renders the environment.
    
     This method is a placeholder for rendering the environment in various modes.
    
     @param mode The rendering mode (default is `"human"`).
    
     @details
     - Currently, this method is not implemented.
     - Can be extended to provide visualizations or other forms of rendering.

Definition at line 543 of file droneRobot.py.

◆ reset()

droneRobot.DroneRobot.reset ( self)

Guess what, another override function.

Only change is to add and reset a flag

     @brief Resets the environment to its initial state.
    
     This method restores the simulation to its default configuration by resetting motor power,
     step counters, error terms, and the Webots simulation state.
    
     @return A default observation, represented as a zero-filled numpy array matching the observation space shape.
    
     @details
     - Resets motor power values to zero and clears PID error terms.
     - Resets the Webots simulation state and physics using `simulationReset` and `simulationResetPhysics`.
     - Ensures compatibility with Webots versions >R2020b but can be overridden for earlier versions.
     - Calls `super().step` to integrate the reset state into the simulation timestep.
    
     @note This method is backward-compatible, allowing older supervisor implementations to be migrated.

Definition at line 634 of file droneRobot.py.

Here is the call graph for this function:

◆ solved()

droneRobot.DroneRobot.solved ( self)
     @brief Determines if the task is considered "solved."
    
     The task is deemed solved if the mean score of the last 100 episodes exceeds
     a predefined threshold (`avg_target_score`), indicating consistent performance.
    
     @return `True` if the task is solved, `False` otherwise.
    
     @details
     - **Threshold Check:** The task is solved if the mean score of the last 100 episodes is greater than `avg_target_score`.
     - If the condition is met, the `saveOK` flag is set to `True`.
    
     @note Requires the `episode_score_list` to contain at least 100 episodes.

Definition at line 463 of file droneRobot.py.

◆ startTensorBoard()

droneRobot.DroneRobot.startTensorBoard ( self,
log_path )
     @brief Starts TensorBoard in a separate thread.
    
     This method creates and starts a new thread to run TensorBoard, ensuring that it does not block the main process.
    
     @param log_path The path to the directory containing TensorBoard log files.
    
     @details
     - Internally calls `launchTensorBoard` in a separate thread.
     - Useful for starting TensorBoard asynchronously during training or simulation.
    
     @note Ensure the `log_path` is valid and TensorBoard is installed.

Definition at line 523 of file droneRobot.py.

Here is the call graph for this function:

◆ step()

droneRobot.DroneRobot.step ( self,
action )
@brief Executes a single simulation step, applying an action and updating the environment.

This method steps the controller, applies the given action to the robot, and returns the resulting
state of the environment, including observations, reward, termination status, and additional info.

@param action The action to be applied to the robot, defined by the use case (e.g., integer for discrete actions).
@return A tuple `(observations, reward, done, info)`:
      - `observations`: Current state of the environment.
      - `reward`: Reward received for the action taken.
      - `done`: Boolean indicating whether the episode is complete.
      - `info`: Additional diagnostic information.

@details
- Increments the step count and initializes motor devices if not already done.
- Applies the action using `apply_action` and updates motor power with `update_motor_power`.
- Fetches observations, computes the reward, and checks if the episode is done.
- Handles integration with the Webots supervisor timestep.

@note Assumes valid initialization of motors and sensors (e.g., GPS, IMU, gyro).

Definition at line 560 of file droneRobot.py.

Here is the caller graph for this function:

◆ update_motor_power()

droneRobot.DroneRobot.update_motor_power ( self,
current_location )
     @brief Updates the motor power for the drone based on the PID controller and the current location.
    
     This method calculates the error between the drone's current location and the target location for
     each axis (x, y, z) and adjusts the motor power accordingly using PID control. It also applies yaw
     control to align the drone towards the target direction. For testing purposes, yaw gain can be set to 
     zero.
    
     @param current_location A list containing the drone's current position (x, y, z) and yaw angle.
    
     @details
     - Computes proportional (P), integral (I), and derivative (D) terms for PID control on each axis.
     - Normalizes yaw angle errors to the range [-π, π].
     - Updates motor power values for maintaining stability and reaching the target location.
     - Clips motor power values to ensure they remain within the allowed range.
    
     @note Assumes the `current_location` input includes yaw as the third element.
     @see self.pid_gains, self.motor_power

Definition at line 133 of file droneRobot.py.

Member Data Documentation

◆ action_space

droneRobot.DroneRobot.action_space
Initial value:
= Box(low=np.array([0,0,0,0,0,0,0,0,0]),
high=np.array([100,100,100,100,100,100,100,100,100]),
dtype=np.float16)

Definition at line 125 of file droneRobot.py.

◆ actionCount

int droneRobot.DroneRobot.actionCount = 0

Definition at line 68 of file droneRobot.py.

◆ angle_bound

droneRobot.DroneRobot.angle_bound = 90

Definition at line 52 of file droneRobot.py.

◆ avg_target_score

int droneRobot.DroneRobot.avg_target_score = -20

Definition at line 55 of file droneRobot.py.

◆ cur_step_count

int droneRobot.DroneRobot.cur_step_count = 0

Definition at line 73 of file droneRobot.py.

◆ debugMode

bool droneRobot.DroneRobot.debugMode = False

Definition at line 47 of file droneRobot.py.

◆ didOnce

bool droneRobot.DroneRobot.didOnce = False

Definition at line 67 of file droneRobot.py.

◆ dt

int droneRobot.DroneRobot.dt = 1/200

Definition at line 72 of file droneRobot.py.

◆ episode_score

int droneRobot.DroneRobot.episode_score = 0

Definition at line 75 of file droneRobot.py.

◆ episode_score_list

droneRobot.DroneRobot.episode_score_list = []

Definition at line 76 of file droneRobot.py.

◆ gps

droneRobot.DroneRobot.gps = self.getDevice('gps')

Definition at line 600 of file droneRobot.py.

◆ gyro

droneRobot.DroneRobot.gyro = self.getDevice('gyro')

Definition at line 603 of file droneRobot.py.

◆ imu

droneRobot.DroneRobot.imu = self.getDevice('inertial_unit')

Definition at line 597 of file droneRobot.py.

◆ integral_errors

dict droneRobot.DroneRobot.integral_errors = {"x": 0, "y": 0, "z": 0}

Definition at line 62 of file droneRobot.py.

◆ last_ep_score

int droneRobot.DroneRobot.last_ep_score = 0

Definition at line 65 of file droneRobot.py.

◆ location_bound

droneRobot.DroneRobot.location_bound = 5

Definition at line 51 of file droneRobot.py.

◆ m1_motor

droneRobot.DroneRobot.m1_motor = self.getDevice('m1_motor')

Needs to be defined in step to work properly.

Definition at line 585 of file droneRobot.py.

◆ m2_motor

droneRobot.DroneRobot.m2_motor = self.getDevice('m2_motor')

Definition at line 588 of file droneRobot.py.

◆ m3_motor

droneRobot.DroneRobot.m3_motor = self.getDevice('m3_motor')

Definition at line 591 of file droneRobot.py.

◆ m4_motor

droneRobot.DroneRobot.m4_motor = self.getDevice('m4_motor')

Definition at line 594 of file droneRobot.py.

◆ max_velocity

int droneRobot.DroneRobot.max_velocity = 150

Definition at line 53 of file droneRobot.py.

◆ motor_power

dict droneRobot.DroneRobot.motor_power
Initial value:
= {
'm1': 0,
'm2': 0,
'm3': 0,
'm4': 0
}

Definition at line 93 of file droneRobot.py.

◆ observation_space

droneRobot.DroneRobot.observation_space
Initial value:
= Box(low=np.array([-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf,-np.inf]),
high=np.array([np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf,np.inf]),
dtype=np.float64)

Definition at line 120 of file droneRobot.py.

◆ past_errors

dict droneRobot.DroneRobot.past_errors = {"x": 0, "y": 0, "z": 0}

Definition at line 61 of file droneRobot.py.

◆ past_yaw_error

int droneRobot.DroneRobot.past_yaw_error = 0

Definition at line 63 of file droneRobot.py.

◆ pid_gains

dict droneRobot.DroneRobot.pid_gains
Initial value:
= {
'X_P': random.uniform(0,100),
'X_I': random.uniform(0,100),
'X_D': random.uniform(0,100),
'Y_P': random.uniform(0,100),
'Y_I': random.uniform(0,100),
'Y_D': random.uniform(0,100),
'Z_P': random.uniform(0,100),
'Z_I': random.uniform(0,100),
'Z_D': random.uniform(0,100),
}

Definition at line 80 of file droneRobot.py.

◆ previous_velocity

int droneRobot.DroneRobot.previous_velocity = 0

Definition at line 56 of file droneRobot.py.

◆ randomSeed

droneRobot.DroneRobot.randomSeed = 42

Definition at line 48 of file droneRobot.py.

◆ robot

droneRobot.DroneRobot.robot = self.getSelf()

Definition at line 130 of file droneRobot.py.

◆ saveOK

bool droneRobot.DroneRobot.saveOK = False

Definition at line 50 of file droneRobot.py.

◆ stepFlag

bool droneRobot.DroneRobot.stepFlag = True

Definition at line 437 of file droneRobot.py.

◆ steps_per_episode

int droneRobot.DroneRobot.steps_per_episode = 3000

Definition at line 74 of file droneRobot.py.

◆ target_location

list droneRobot.DroneRobot.target_location = [0,0,2]

Definition at line 58 of file droneRobot.py.

◆ timestep

droneRobot.DroneRobot.timestep = self.getBasicTimeStep()

Definition at line 70 of file droneRobot.py.

◆ yaw_gain

int droneRobot.DroneRobot.yaw_gain = 0

Definition at line 78 of file droneRobot.py.


The documentation for this class was generated from the following file: