BotNavSim  v0.4.3
Mobile Robot Simulation
ArDrone Class Reference

AR Drone control system implementation. Developed with data from http://parrotcontact.emencia.net/website/user-guides/download-user-guides.php?pdf=ar-drone-2/AR-Drone-2_User-guide_Android_UK.pdf See final report for diagram of implementation. More...

Inheritance diagram for ArDrone:
Collaboration diagram for ArDrone:

Public Attributes

float maxLateralSpeed
 The maximum lateral speed allowed by the control system. More...
 
Pid lateralSpeedController
 The lateral speed controller. More...
 
Pid tiltController
 The tilt controller. More...
 
float verticalSpeed
 The maximum speed that the vertical target location can change. More...
 
float maxThrottle
 The max throttle of each propeller force. More...
 
Pid throttleController
 The throttle controller. More...
 
float maxTilt
 The maximum roll and pitch permitted by the control system. (measured in degrees) More...
 
bool rotateYaw
 If true then AR Drone will rotate to face the command direction vector. More...
 
float maxYawVelocity
 The max yaw rotational velocity. (measured in degrees per second) More...
 
float yawKp
 Gain parameter in controlling the yaw torque. More...
 
Pid rollController
 The roll controller. More...
 
Pid pitchController
 The pitch controller. More...
 
Transform FL
 Reference to the front-left propeller position. More...
 
Transform FR
 Reference to the front-right propeller position. More...
 
Transform BL
 Reference to the back-left propeller position. More...
 
Transform BR
 Reference to the back-right propeller position. More...
 

Private Member Functions

void Awake ()
 Awake this instance. More...
 
void Start ()
 Start this instance. More...
 
void Reset ()
 Reset the PID controllers. More...
 
void Update ()
 
void FixedUpdate ()
 

Private Attributes

float _actualPitch
 
float _actualRoll
 
float _targetSpeed
 
float _targetTilt
 
float _targetRoll
 
float _targetPitch
 
float _targetHeight
 
Robot _robot
 
Vector3 _command
 
Vector3 _target2d
 
Vector3 _holdRotation
 
Vector3 _holdPosition
 
Vector3 _controlTorque
 
Vector3 _flControlThrust
 
Vector3 _frControlThrust
 
Vector3 _blControlThrust
 
Vector3 _brControlThrust
 

Detailed Description

AR Drone control system implementation. Developed with data from http://parrotcontact.emencia.net/website/user-guides/download-user-guides.php?pdf=ar-drone-2/AR-Drone-2_User-guide_Android_UK.pdf See final report for diagram of implementation.

Definition at line 10 of file ArDrone.cs.

Member Function Documentation

void ArDrone.Awake ( )
private

Awake this instance.

Definition at line 122 of file ArDrone.cs.

122  {
123  _robot = GetComponent<Robot>();
124  }
Robot _robot
Definition: ArDrone.cs:105
void ArDrone.FixedUpdate ( )
private

Definition at line 223 of file ArDrone.cs.

223  {
224  if(!_robot.moveEnabled) return;
225 
226  float displacement2d = _target2d.magnitude;
227 
228  _targetSpeed = lateralSpeedController.output(0f, - displacement2d);
229  _targetSpeed = Mathf.Clamp(_targetSpeed, 0f, maxLateralSpeed);
230  _targetTilt = tiltController.output(_targetSpeed, rigidbody.velocity.magnitude);
231  _targetTilt = Mathf.Clamp(_targetTilt, 0f, maxTilt);
232 
233  Vector3 forward2d = new Vector3(transform.forward.x, 0f, transform.forward.z).normalized;
234  Vector3 right2d = new Vector3(transform.right.x, 0f, transform.right.z).normalized;
235 
236  // PITCH
237  float forwardAxisAngle = Vector3.Angle(forward2d, _target2d);
238  float forwardCosine = Mathf.Cos(forwardAxisAngle * Mathf.Deg2Rad);
239  _actualPitch = Vector3.Angle(transform.forward, Vector3.up) - 90f; // avoids the 0-360 boundary
240  _targetPitch = forwardCosine*_targetTilt;
241 
242  // ROLL
243  float rightAxisAngle = Vector3.Angle(right2d, _target2d);
244  float rightCosine = Mathf.Cos(rightAxisAngle * Mathf.Deg2Rad);
245  _actualRoll = Vector3.Angle(transform.right, Vector3.up) - 90f; // avoids the 0-360 boundary
246  _targetRoll = rightCosine*_targetTilt;
247 
248 
249  float pitchOutput = pitchController.output(_targetPitch, _actualPitch);
250  float rollOutput = rollController.output(_targetRoll, _actualRoll);
251 
252  // THROTTLE
253  float throttle = throttleController.output(_targetHeight, transform.position.y);
254 
255  // adapted from here
256  // https://ghowen.me/build-your-own-quadcopter-autopilot/
257  float flForce = Mathf.Clamp(throttle + rollOutput - pitchOutput, 0f, maxThrottle);
258  float blForce = Mathf.Clamp(throttle + rollOutput + pitchOutput, 0f, maxThrottle);
259  float frForce = Mathf.Clamp(throttle - rollOutput - pitchOutput, 0f, maxThrottle);
260  float brForce = Mathf.Clamp(throttle - rollOutput + pitchOutput, 0f, maxThrottle);
261 
262 
263  _flControlThrust = FL.up * flForce;
264  _blControlThrust = BL.up * blForce;
265  _frControlThrust = FR.up * frForce;
266  _brControlThrust = BR.up * brForce;
267 
268  rigidbody.AddForceAtPosition(_flControlThrust, FL.position);
269  rigidbody.AddForceAtPosition(_frControlThrust, FR.position);
270  rigidbody.AddForceAtPosition(_blControlThrust, BL.position);
271  rigidbody.AddForceAtPosition(_brControlThrust, BR.position);
272  rigidbody.AddTorque(_controlTorque, ForceMode.Acceleration);
273 
274  }
float _targetPitch
Definition: ArDrone.cs:102
Vector3 _blControlThrust
Definition: ArDrone.cs:116
Vector3 _brControlThrust
Definition: ArDrone.cs:117
float _targetRoll
Definition: ArDrone.cs:101
Pid rollController
The roll controller.
Definition: ArDrone.cs:70
Vector3 _target2d
Definition: ArDrone.cs:108
Vector3 _controlTorque
Definition: ArDrone.cs:113
float maxLateralSpeed
The maximum lateral speed allowed by the control system.
Definition: ArDrone.cs:18
Transform BL
Reference to the back-left propeller position.
Definition: ArDrone.cs:89
float output(float target, float actual)
Pid control from actual to target. Uses Time.fixedDeltaTime for delta time values ...
Definition: Pid.cs:21
Robot _robot
Definition: ArDrone.cs:105
float _targetSpeed
Definition: ArDrone.cs:99
Vector3 _frControlThrust
Definition: ArDrone.cs:115
Pid pitchController
The pitch controller.
Definition: ArDrone.cs:74
bool moveEnabled
A flag indicating whether the robot should try moving.
Definition: Robot.cs:29
float _targetHeight
Definition: ArDrone.cs:103
float maxThrottle
The max throttle of each propeller force.
Definition: ArDrone.cs:38
float maxTilt
The maximum roll and pitch permitted by the control system. (measured in degrees) ...
Definition: ArDrone.cs:49
Transform FR
Reference to the front-right propeller position.
Definition: ArDrone.cs:84
Transform BR
Reference to the back-right propeller position.
Definition: ArDrone.cs:94
float _targetTilt
Definition: ArDrone.cs:100
Pid tiltController
The tilt controller.
Definition: ArDrone.cs:28
Vector3 _flControlThrust
Definition: ArDrone.cs:114
Transform FL
Reference to the front-left propeller position.
Definition: ArDrone.cs:79
Pid lateralSpeedController
The lateral speed controller.
Definition: ArDrone.cs:23
Pid throttleController
The throttle controller.
Definition: ArDrone.cs:43
float _actualPitch
Definition: ArDrone.cs:96
float _actualRoll
Definition: ArDrone.cs:97

Here is the call graph for this function:

void ArDrone.Reset ( )
private

Reset the PID controllers.

Definition at line 137 of file ArDrone.cs.

137  {
143  }
Pid rollController
The roll controller.
Definition: ArDrone.cs:70
Pid pitchController
The pitch controller.
Definition: ArDrone.cs:74
void Reset()
Set error, previous_error, integral and derivative to 0.
Definition: Pid.cs:32
Pid tiltController
The tilt controller.
Definition: ArDrone.cs:28
Pid lateralSpeedController
The lateral speed controller.
Definition: ArDrone.cs:23
Pid throttleController
The throttle controller.
Definition: ArDrone.cs:43

Here is the call graph for this function:

Here is the caller graph for this function:

void ArDrone.Start ( )
private

Start this instance.

Definition at line 129 of file ArDrone.cs.

129  {
130  _holdPosition = transform.position;
131  _targetHeight = transform.position.y;
132  }
float _targetHeight
Definition: ArDrone.cs:103
Vector3 _holdPosition
Definition: ArDrone.cs:110
void ArDrone.Update ( )
private

Definition at line 146 of file ArDrone.cs.

146  {
147 
148  if (!_robot.moveEnabled) {
149  Reset();
150  _holdPosition = transform.position;
151  return;
152  }
153 
155 
156  Draw.Instance.Bearing(transform.position, _command, Color.green);
157  Debug.DrawRay(transform.position, _command, Color.green);
158 
159  _targetHeight += _command.y * Time.deltaTime * verticalSpeed;
160 
161  Vector3 forward2d = new Vector3(transform.forward.x, 0f, transform.forward.z).normalized;
162  Vector3 right2d = new Vector3(transform.right.x, 0f, transform.right.z).normalized;
163  Vector3 yawTarget; // set to vector3.forward when there is no command
164 
165 
166 
167  // if no command
168  if (_command.magnitude < 0.1f) { // no command, replace command vector with hold values
169 
170 
171  _target2d = _holdPosition - transform.position;
172  _target2d.y = 0f;
173  yawTarget = Vector3.forward;
174 
175  } else { // deal with command vector
176 
177  // X/Z plane target direction
178  _target2d = new Vector3(_command.x, 0f, _command.z);
179  yawTarget = _target2d;
180  // update hold previous command
182  // keep hold position
183  _holdPosition = transform.position;
184  }
185 
186 
187 
188 
189  Draw.Instance.Bearing(transform.position, _target2d, Color.red);
190  Debug.DrawRay(transform.position, _target2d, Color.red);
191 
192  if (rotateYaw) {
193  // control the local yaw velocity so that ArDrone faces command direction
194  float yawAxisAngle = Vector3.Angle(right2d, yawTarget);
195  float yawCosine = Mathf.Cos(yawAxisAngle * Mathf.Deg2Rad); // 1:RotateRight, -1:RotateLeft
196  float localYawVelocity = transform.InverseTransformDirection(rigidbody.angularVelocity).y;
197  float yawForce = (yawCosine*maxYawVelocity - localYawVelocity) * yawKp;
198  _controlTorque = transform.rotation * new Vector3(0f, yawForce, 0f);
199  }
200 
201 
202 
203 
204 
205 
206  Draw.Instance.Bearing(transform.position, throttleController.error * Vector3.up, Color.red);
207  Debug.DrawRay(transform.position, throttleController.error * Vector3.up, Color.red);
208 
209 
210  Draw.Instance.Bearing(FL.position, _flControlThrust, Color.magenta);
211  Draw.Instance.Bearing(BR.position, _brControlThrust, Color.magenta);
212  Draw.Instance.Bearing(FR.position, _frControlThrust, Color.magenta);
213  Draw.Instance.Bearing(BL.position, _blControlThrust, Color.magenta);
214 
215  Debug.DrawRay(FL.position, _flControlThrust, Color.magenta);
216  Debug.DrawRay(BR.position, _brControlThrust, Color.magenta);
217  Debug.DrawRay(FR.position, _frControlThrust, Color.magenta);
218  Debug.DrawRay(BL.position, _blControlThrust, Color.magenta);
219 
220  }
float error
Definition: Pid.cs:11
void Bearing(Vector3 origin, Vector3 direction, Color color)
Draw a line in a direction starting at a specified location, in a specified color.
Definition: Draw.cs:90
Vector3 navigationCommand
The cached navigation bearing from INavigation.
Definition: Robot.cs:89
Vector3 _blControlThrust
Definition: ArDrone.cs:116
Vector3 _brControlThrust
Definition: ArDrone.cs:117
float verticalSpeed
The maximum speed that the vertical target location can change.
Definition: ArDrone.cs:33
Vector3 _target2d
Definition: ArDrone.cs:108
Vector3 _controlTorque
Definition: ArDrone.cs:113
Transform BL
Reference to the back-left propeller position.
Definition: ArDrone.cs:89
Robot _robot
Definition: ArDrone.cs:105
Draws stuff, useful for displaying debug data in the simulation. Note that GL implementations require...
Definition: Draw.cs:10
Vector3 _frControlThrust
Definition: ArDrone.cs:115
bool moveEnabled
A flag indicating whether the robot should try moving.
Definition: Robot.cs:29
float _targetHeight
Definition: ArDrone.cs:103
Transform FR
Reference to the front-right propeller position.
Definition: ArDrone.cs:84
Transform BR
Reference to the back-right propeller position.
Definition: ArDrone.cs:94
Vector3 _holdRotation
Definition: ArDrone.cs:109
float yawKp
Gain parameter in controlling the yaw torque.
Definition: ArDrone.cs:65
Vector3 _flControlThrust
Definition: ArDrone.cs:114
float maxYawVelocity
The max yaw rotational velocity. (measured in degrees per second)
Definition: ArDrone.cs:60
Transform FL
Reference to the front-left propeller position.
Definition: ArDrone.cs:79
Vector3 _holdPosition
Definition: ArDrone.cs:110
Pid throttleController
The throttle controller.
Definition: ArDrone.cs:43
void Reset()
Reset the PID controllers.
Definition: ArDrone.cs:137
static Draw Instance
Reference to the MonoBehaviour instance.
Definition: Draw.cs:39
bool rotateYaw
If true then AR Drone will rotate to face the command direction vector.
Definition: ArDrone.cs:54
Vector3 _command
Definition: ArDrone.cs:107

Here is the call graph for this function:

Member Data Documentation

float ArDrone._actualPitch
private

Definition at line 96 of file ArDrone.cs.

float ArDrone._actualRoll
private

Definition at line 97 of file ArDrone.cs.

Vector3 ArDrone._blControlThrust
private

Definition at line 116 of file ArDrone.cs.

Vector3 ArDrone._brControlThrust
private

Definition at line 117 of file ArDrone.cs.

Vector3 ArDrone._command
private

Definition at line 107 of file ArDrone.cs.

Vector3 ArDrone._controlTorque
private

Definition at line 113 of file ArDrone.cs.

Vector3 ArDrone._flControlThrust
private

Definition at line 114 of file ArDrone.cs.

Vector3 ArDrone._frControlThrust
private

Definition at line 115 of file ArDrone.cs.

Vector3 ArDrone._holdPosition
private

Definition at line 110 of file ArDrone.cs.

Vector3 ArDrone._holdRotation
private

Definition at line 109 of file ArDrone.cs.

Robot ArDrone._robot
private

Definition at line 105 of file ArDrone.cs.

Vector3 ArDrone._target2d
private

Definition at line 108 of file ArDrone.cs.

float ArDrone._targetHeight
private

Definition at line 103 of file ArDrone.cs.

float ArDrone._targetPitch
private

Definition at line 102 of file ArDrone.cs.

float ArDrone._targetRoll
private

Definition at line 101 of file ArDrone.cs.

float ArDrone._targetSpeed
private

Definition at line 99 of file ArDrone.cs.

float ArDrone._targetTilt
private

Definition at line 100 of file ArDrone.cs.

Transform ArDrone.BL

Reference to the back-left propeller position.

Definition at line 89 of file ArDrone.cs.

Transform ArDrone.BR

Reference to the back-right propeller position.

Definition at line 94 of file ArDrone.cs.

Transform ArDrone.FL

Reference to the front-left propeller position.

Definition at line 79 of file ArDrone.cs.

Transform ArDrone.FR

Reference to the front-right propeller position.

Definition at line 84 of file ArDrone.cs.

Pid ArDrone.lateralSpeedController

The lateral speed controller.

Definition at line 23 of file ArDrone.cs.

float ArDrone.maxLateralSpeed

The maximum lateral speed allowed by the control system.

Definition at line 18 of file ArDrone.cs.

float ArDrone.maxThrottle

The max throttle of each propeller force.

Definition at line 38 of file ArDrone.cs.

float ArDrone.maxTilt

The maximum roll and pitch permitted by the control system. (measured in degrees)

Definition at line 49 of file ArDrone.cs.

float ArDrone.maxYawVelocity

The max yaw rotational velocity. (measured in degrees per second)

Definition at line 60 of file ArDrone.cs.

Pid ArDrone.pitchController

The pitch controller.

Definition at line 74 of file ArDrone.cs.

Pid ArDrone.rollController

The roll controller.

Definition at line 70 of file ArDrone.cs.

bool ArDrone.rotateYaw

If true then AR Drone will rotate to face the command direction vector.

Definition at line 54 of file ArDrone.cs.

Pid ArDrone.throttleController

The throttle controller.

Definition at line 43 of file ArDrone.cs.

Pid ArDrone.tiltController

The tilt controller.

Definition at line 28 of file ArDrone.cs.

float ArDrone.verticalSpeed

The maximum speed that the vertical target location can change.

Definition at line 33 of file ArDrone.cs.

float ArDrone.yawKp

Gain parameter in controlling the yaw torque.

Definition at line 65 of file ArDrone.cs.


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