Apply php-cs-fixer

* Apply php-cs-fixer
* Use short array syntax
Esse commit está contido em:
Baptiste Adrien
2016-04-06 09:23:59 +02:00
commit 8595d2342b
12 arquivos alterados com 350 adições e 343 exclusões
+12 -11
Ver Arquivo
@@ -1,37 +1,38 @@
<?php
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$loader->add('Joli\ArDrone', __DIR__ . '/../src/');
$loader = require_once __DIR__.'/../vendor/autoload.php';
$loader->add('Joli\ArDrone', __DIR__.'/../src/');
$client = new \Joli\ArDrone\Client();
$client->takeoff();
$client
->after(3, function() use ($client) {
->after(3, function () use ($client) {
$client->up(0.6);
})
->after(4, function() use ($client) {
->after(4, function () use ($client) {
$client->stop();
})
->after(1, function() use ($client) {
->after(1, function () use ($client) {
$client->left(0.3);
})
->after(1, function() use ($client) {
->after(1, function () use ($client) {
$client->stop();
})
->after(1, function() use ($client) {
->after(1, function () use ($client) {
$client->down(0.5);
})
->after(2, function() use ($client) {
->after(2, function () use ($client) {
$client->stop();
})
->after(1, function() use ($client) {
->after(1, function () use ($client) {
$client->right(0.3);
})
->after(1, function() use ($client) {
->after(1, function () use ($client) {
$client->stop();
})
->after(3, function() use ($client) {
->after(3, function () use ($client) {
$client->land();
});
+3 -2
Ver Arquivo
@@ -1,6 +1,7 @@
<?php
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$loader->add('Joli\ArDrone', __DIR__ . '/../src/');
$loader = require_once __DIR__.'/../vendor/autoload.php';
$loader->add('Joli\ArDrone', __DIR__.'/../src/');
$client = new \Joli\ArDrone\Client();
+22 -16
Ver Arquivo
@@ -1,7 +1,9 @@
<?php
namespace Joli\ArDrone\Buffer;
class Buffer {
class Buffer
{
/**
* @var string
*/
@@ -20,14 +22,15 @@ class Buffer {
public function getUint32LE()
{
$value = unpack('V/', substr($this->data, $this->offset, ($this->offset + 4)));
$value = unpack('V/', substr($this->data, $this->offset, ($this->offset + 4)));
$this->moveOffset(4);
return dechex($value[1]);
}
public function getUint16LE()
{
$value = unpack('v/', substr($this->data, $this->offset, ($this->offset + 2)));
$value = unpack('v/', substr($this->data, $this->offset, ($this->offset + 2)));
$this->moveOffset(2);
return dechex($value[1]);
@@ -35,7 +38,7 @@ class Buffer {
public function getFloat32()
{
$value = unpack('f/', substr($this->data, $this->offset, ($this->offset + 4)));
$value = unpack('f/', substr($this->data, $this->offset, ($this->offset + 4)));
$this->moveOffset(4);
return dechex($value[1]);
@@ -43,7 +46,7 @@ class Buffer {
public function getUint8()
{
$value = unpack('C/', substr($this->data, $this->offset, ($this->offset + 1)));
$value = unpack('C/', substr($this->data, $this->offset, ($this->offset + 1)));
$this->moveOffset(1);
return dechex($value[1]);
@@ -51,7 +54,7 @@ class Buffer {
public function getInt32()
{
$value = unpack('I/', substr($this->data, $this->offset, ($this->offset + 4)));
$value = unpack('I/', substr($this->data, $this->offset, ($this->offset + 4)));
$this->moveOffset(4);
return dechex($value[1]);
@@ -62,16 +65,18 @@ class Buffer {
return $this->mask($masks, $this->getUint32LE());
}
public function getVector31() {
return array(
public function getVector31()
{
return [
'x' => $this->getFloat32(),
'y' => $this->getFloat32(),
'z' => $this->getFloat32(),
);
];
}
public function getMatrix33() {
return array(
public function getMatrix33()
{
return [
'm11' => $this->getFloat32(),
'm12' => $this->getFloat32(),
'm13' => $this->getFloat32(),
@@ -81,7 +86,7 @@ class Buffer {
'm31' => $this->getFloat32(),
'm32' => $this->getFloat32(),
'm33' => $this->getFloat32(),
);
];
}
public function getBytes($nbBytes)
@@ -100,13 +105,13 @@ class Buffer {
//todo: move this function ?
private function mask($masks, $value)
{
$flags = array();
$flags = [];
foreach($masks as $name => $mask) {
foreach ($masks as $name => $mask) {
$flags[$name] = (hexdec($value) & ($mask)) ? 1 : 0;
}
return $flags;
return $flags;
}
/**
@@ -117,7 +122,8 @@ class Buffer {
return $this->data;
}
public function getLength() {
public function getLength()
{
return strlen($this->data);
}
}
+22 -20
Ver Arquivo
@@ -1,15 +1,17 @@
<?php
namespace Joli\ArDrone;
use Evenement\EventEmitter;
use Joli\ArDrone\Control\UdpControl;
use Joli\ArDrone\Navdata\Frame;
use Joli\ArDrone\Navdata\UdpNavdata;
use React\EventLoop\Factory AS LoopFactory;
use React\Datagram\Factory AS UdpFactory;
use React\EventLoop\Factory as LoopFactory;
use React\Datagram\Factory as UdpFactory;
use Joli\ArDrone\Config\Config;
class Client extends EventEmitter {
class Client extends EventEmitter
{
/**
* @var \Joli\ArDrone\Control\UdpControl
*/
@@ -26,7 +28,7 @@ class Client extends EventEmitter {
private $timerOffset;
/**
* @var boolean
* @var bool
*/
public $disableEmergency;
@@ -52,12 +54,12 @@ class Client extends EventEmitter {
public function __construct()
{
$this->loop = LoopFactory::create();
$this->loop = LoopFactory::create();
$this->udpFactory = new UdpFactory($this->loop);
$this->timerOffset = 0;
$this->lastState = 'CTRL_LANDED';
$this->lastBattery = 100;
$this->udpFactory = new UdpFactory($this->loop);
$this->timerOffset = 0;
$this->lastState = 'CTRL_LANDED';
$this->lastBattery = 100;
$this->lastAltitude = 0;
$this->disableEmergency = false;
@@ -70,7 +72,7 @@ class Client extends EventEmitter {
$this->udpNavdata = new UdpNavdata($this->loop);
$self = $this;
$this->udpNavdata->on('navdata', function(Frame $navdata) use (&$self) {
$this->udpNavdata->on('navdata', function (Frame $navdata) use (&$self) {
if (count($navdata->getDroneState()) > 0) {
$stateData = $navdata->getDroneState();
if ($stateData['emergencyLanding'] && $self->disableEmergency) {
@@ -102,11 +104,11 @@ class Client extends EventEmitter {
$stateData = $navdata->getDroneState();
if ($stateData['lowBattery'] === 1) {
$self->emit('lowBattery', array($battery));
$self->emit('lowBattery', [$battery]);
}
if ($battery !== $self->lastBattery) {
$self->emit('batteryChange', array($battery));
$self->emit('batteryChange', [$battery]);
$self->lastBattery = $battery;
}
@@ -114,19 +116,19 @@ class Client extends EventEmitter {
$altitude = $demoData['altitudeMeters'];
if ($altitude !== $self->lastAltitude) {
$self->emit('altitudeChange', array($altitude));
$self->emit('altitudeChange', [$altitude]);
$self->lastAltitude = $altitude;
}
}
$self->emit('navdata', array($navdata));
$self->emit('navdata', [$navdata]);
});
}
public function emitState($e, $state, $currentState)
{
if ($currentState === $state && $this->lastState !== $state) {
$this->emit($e, array());
$this->emit($e, []);
}
}
@@ -142,7 +144,7 @@ class Client extends EventEmitter {
$udpControl = $this->udpControl;
$repl->on('action', function($action) use (&$udpControl) {
$repl->on('action', function ($action) use (&$udpControl) {
$udpControl->emit($action);
});
}
@@ -178,23 +180,23 @@ class Client extends EventEmitter {
public function __call($name, $arguments)
{
if(in_array($name, Config::$commands)) {
if (in_array($name, Config::$commands)) {
if ($name === 'takeoff' || $name === 'land') {
// process callback function
$callback = (count($arguments) === 1) ? $arguments[0] : function() {};
$callback = (count($arguments) === 1) ? $arguments[0] : function () {};
$eventName = ($name === 'takeoff') ? 'hovering' : 'landed';
$this->once($eventName, $callback);
$this->udpControl->emit($name);
} else if ($name === 'stop' || $name === 'ftrim' || $name === 'flip') {
} elseif ($name === 'stop' || $name === 'ftrim' || $name === 'flip') {
$this->udpControl->emit($name);
// Control commands
} else {
if (count($arguments) > 1) {
new \Exception('There are too many arguments');
}
$this->udpControl->emit($name, array($arguments[0]));
$this->udpControl->emit($name, [$arguments[0]]);
}
} else {
new \Exception('Invalid function');
+6 -5
Ver Arquivo
@@ -1,13 +1,14 @@
<?php
namespace Joli\ArDrone\Config;
class Config {
class Config
{
const DRONE_IP = '192.168.1.1';
const NAVDATA_PORT = 5554;
const CONTROL_PORT = 5556;
static $commands = array(
public static $commands = [
'takeoff',
'land',
'clockwise',
@@ -21,6 +22,6 @@ class Config {
'stop',
'exit',
'ftrim',
'flip'
);
'flip',
];
}
+13 -13
Ver Arquivo
@@ -1,8 +1,9 @@
<?php
namespace Joli\ArDrone\Control;
class AtCommand {
class AtCommand
{
/**
* @var string
*/
@@ -18,31 +19,30 @@ class AtCommand {
*/
private $sequence;
const TYPE_REF = 'REF';
const TYPE_PCMD = 'PCMD';
const TYPE_CALIB = 'CALIB';
const TYPE_REF = 'REF';
const TYPE_PCMD = 'PCMD';
const TYPE_CALIB = 'CALIB';
const TYPE_CONFIG = 'CONFIG';
const TYPE_FTRIM = 'FTRIM';
const TYPE_ANIM = 'ANIM';
const TYPE_FTRIM = 'FTRIM';
const TYPE_ANIM = 'ANIM';
public function __construct($sequence, $type, $args)
{
$this->args = $args;
$this->type = $type;
$this->args = $args;
$this->type = $type;
$this->sequence = $sequence;
}
function __toString()
public function __toString()
{
$command = 'AT*' . $this->type . '=' . $this->sequence;
$command = 'AT*'.$this->type.'='.$this->sequence;
if (count($this->args) > 0) {
$command .= ','. implode(',', $this->args);
$command .= ','.implode(',', $this->args);
}
$command .= "\r";
return $command;
}
}
+28 -28
Ver Arquivo
@@ -1,9 +1,9 @@
<?php
namespace Joli\ArDrone\Control;
use Joli\ArDrone\Control\AtCommand;
class AtCommandCreator {
class AtCommandCreator
{
/**
* @var int
*/
@@ -12,16 +12,16 @@ class AtCommandCreator {
/**
* @var array
*/
private $pcmdAlias = array(
'left' => array('index' => 1, 'invert' => true),
'right' => array('index' => 1, 'invert' => false),
'front' => array('index' => 2, 'invert' => true),
'back' => array('index' => 2, 'invert' => false),
'up' => array('index' => 3, 'invert' => false),
'down' => array('index' => 3, 'invert' => true),
'clockwise' => array('index' => 4, 'invert' => false),
'counterClockwise' => array('index' => 4, 'invert' => true)
);
private $pcmdAlias = [
'left' => ['index' => 1, 'invert' => true],
'right' => ['index' => 1, 'invert' => false],
'front' => ['index' => 2, 'invert' => true],
'back' => ['index' => 2, 'invert' => false],
'up' => ['index' => 3, 'invert' => false],
'down' => ['index' => 3, 'invert' => true],
'clockwise' => ['index' => 4, 'invert' => false],
'counterClockwise' => ['index' => 4, 'invert' => true],
];
public function __construct()
{
@@ -30,11 +30,11 @@ class AtCommandCreator {
public function createConfigCommand($name, $value)
{
$args = array();
$config = '"' . $name . '","' . $value . '"';
$args = [];
$config = '"'.$name.'","'.$value.'"';
array_push($args, $config);
$this->sequence++;
++$this->sequence;
return new AtCommand($this->sequence, AtCommand::TYPE_CONFIG, $args);
}
@@ -42,7 +42,7 @@ class AtCommandCreator {
public function createRefCommand($options)
{
$config = 0;
$args = array();
$args = [];
if ($options['fly'] === true) {
$config = $config | (1 << 9);
@@ -54,16 +54,16 @@ class AtCommandCreator {
array_push($args, $config);
$this->sequence++;
++$this->sequence;
return new AtCommand($this->sequence, AtCommand::TYPE_REF, $args);
}
public function createPcmdCommand($options)
{
$args = array(0, 0, 0, 0, 0);
$args = [0, 0, 0, 0, 0];
foreach($options as $key => $value) {
foreach ($options as $key => $value) {
$alias = $this->pcmdAlias[$key];
if ($alias['invert']) {
@@ -73,23 +73,23 @@ class AtCommandCreator {
$args[$alias['index']] = $this->floatToIEEE($value);
}
if ($args[1] != 0 || $args[2] != 0 ) {
if ($args[1] != 0 || $args[2] != 0) {
$args[0] = 1;
}
$this->sequence++;
++$this->sequence;
return new AtCommand($this->sequence, AtCommand::TYPE_PCMD, $args);
}
public function createFtrimCommand()
{
return new AtCommand($this->sequence, AtCommand::TYPE_FTRIM, array());
return new AtCommand($this->sequence, AtCommand::TYPE_FTRIM, []);
}
public function createAnimCommand()
{
$args = array(17, 1);
$args = [17, 1];
return new AtCommand($this->sequence, AtCommand::TYPE_ANIM, $args);
}
@@ -99,17 +99,17 @@ class AtCommandCreator {
$floatInt = (float) $floatInt;
$binInt = pack('f', $floatInt);
$hexInt = "";
for($i = 0; $i < strlen($binInt); $i++) {
$hexInt = '';
for ($i = 0; $i < strlen($binInt); ++$i) {
$c = ord($binInt{$i});
$hexInt = sprintf("%02X", $c).$hexInt;
$hexInt = sprintf('%02X', $c).$hexInt;
}
if ($floatInt < 0) {
$binIntString = decbin(hexdec($hexInt));
$twoComplement = '';
for($i=0; $i < strlen($binIntString); $i++) {
for ($i = 0; $i < strlen($binIntString); ++$i) {
if ($binIntString[$i] == '0') {
$twoComplement .= '1';
} else {
+36 -37
Ver Arquivo
@@ -4,8 +4,8 @@ namespace Joli\ArDrone\Control;
use Evenement\EventEmitter;
use Joli\ArDrone\Config\Config;
use React\Datagram\Factory AS UdpFactory;
use React\Datagram\Socket AS UdpSocket;
use React\Datagram\Factory as UdpFactory;
use React\Datagram\Socket as UdpSocket;
class UdpControl extends EventEmitter
{
@@ -26,14 +26,14 @@ class UdpControl extends EventEmitter
public function __construct($loop)
{
$this->loop = $loop;
$this->port = Config::CONTROL_PORT;
$this->ip = Config::DRONE_IP;
$this->loop = $loop;
$this->port = Config::CONTROL_PORT;
$this->ip = Config::DRONE_IP;
$this->commandCreator = new AtCommandCreator();
$this->speed = 0.3;
$this->ref = array('fly' => false, 'emergency' => false);
$this->pcmd = array();
$this->anim = array();
$this->speed = 0.3;
$this->ref = ['fly' => false, 'emergency' => false];
$this->pcmd = [];
$this->anim = [];
$this->start();
}
@@ -41,21 +41,21 @@ class UdpControl extends EventEmitter
private function start()
{
$udpFactory = new UdpFactory($this->loop);
$loop = $this->loop;
$loop = $this->loop;
$udpControl = $this;
$udpFactory->createClient($this->ip.':'.$this->port)->then(function (UdpSocket $client) use (&$loop, $udpControl) {
$commandCreator = $udpControl->commandCreator;
$ref = $udpControl->ref;
$pcmd = $udpControl->pcmd;
$anim = $udpControl->anim;
$ref = $udpControl->ref;
$pcmd = $udpControl->pcmd;
$anim = $udpControl->anim;
// Start dialog
$client->send('1');
$client->send('1');
for ($j = 0; $j < 5; $j++) {
$cmds = array();
for ($j = 0; $j < 5; ++$j) {
$cmds = [];
array_push($cmds,
$commandCreator->createConfigCommand('general:navdata_demo',
@@ -71,14 +71,14 @@ class UdpControl extends EventEmitter
// According to tests, a satisfying control of the AR.Drone 2.0 is reached
// by sending the AT-commands every 30 ms for smooth drone movements.
$loop->addPeriodicTimer(0.03,
function() use ($client, $commandCreator, &$ref, &$pcmd, &$anim) {
$cmds = array();
function () use ($client, $commandCreator, &$ref, &$pcmd, &$anim) {
$cmds = [];
array_push($cmds, $commandCreator->createRefCommand($ref));
array_push($cmds, $commandCreator->createPcmdCommand($pcmd));
if (count($anim) > 0) {
for ($i = 0; $i <= 10; $i++) {
for ($i = 0; $i <= 10; ++$i) {
foreach ($anim as $name => $duration) {
array_push($cmds,
$commandCreator->createConfigCommand($name,
@@ -86,88 +86,87 @@ class UdpControl extends EventEmitter
}
}
$anim = array();
$anim = [];
}
$cmds = implode('', $cmds);
$client->send($cmds);
});
$udpControl->on('land',
function() use (&$ref, &$pcmd) {
$pcmd = array();
function () use (&$ref, &$pcmd) {
$pcmd = [];
$ref['fly'] = false;
});
$udpControl->on('ftrim',
function() use (&$client, &$commandCreator) {
function () use (&$client, &$commandCreator) {
$client->send($commandCreator->createFtrimCommand());
});
$udpControl->on('takeoff',
function() use (&$ref, &$pcmd) {
$pcmd = array();
function () use (&$ref, &$pcmd) {
$pcmd = [];
$ref['fly'] = true;
});
$udpControl->on('clockwise',
function($speed = 0.5) use (&$pcmd) {
function ($speed = 0.5) use (&$pcmd) {
$pcmd['clockwise'] = $speed;
unset($pcmd['counterClockwise']);
});
$udpControl->on('counterClockwise',
function($speed = 0.5) use (&$pcmd) {
function ($speed = 0.5) use (&$pcmd) {
$pcmd['counterClockwise'] = $speed;
unset($pcmd['clockwise']);
});
$udpControl->on('stop',
function() use (&$pcmd) {
$pcmd = array();
function () use (&$pcmd) {
$pcmd = [];
});
$udpControl->on('front',
function($speed = 0.3) use (&$pcmd) {
function ($speed = 0.3) use (&$pcmd) {
$pcmd['front'] = $speed;
unset($pcmd['back']);
});
$udpControl->on('back',
function($speed = 0.3) use (&$pcmd) {
function ($speed = 0.3) use (&$pcmd) {
$pcmd['back'] = $speed;
unset($pcmd['front']);
});
$udpControl->on('right',
function($speed = 0.3) use (&$pcmd) {
function ($speed = 0.3) use (&$pcmd) {
$pcmd['right'] = $speed;
unset($pcmd['left']);
});
$udpControl->on('left',
function($speed = 0.3) use (&$pcmd) {
function ($speed = 0.3) use (&$pcmd) {
$pcmd['left'] = $speed;
unset($pcmd['right']);
});
$udpControl->on('up',
function($speed = 0.6) use (&$pcmd) {
function ($speed = 0.6) use (&$pcmd) {
$pcmd['up'] = $speed;
unset($pcmd['down']);
});
$udpControl->on('down',
function($speed = 0.6) use (&$pcmd) {
function ($speed = 0.6) use (&$pcmd) {
$pcmd['down'] = $speed;
unset($pcmd['up']);
});
$udpControl->on('flip',
function() use (&$anim) {
function () use (&$anim) {
$anim['control:flight_anim'] = '16,5';
});
});
}
}
}
+51 -55
Ver Arquivo
@@ -1,11 +1,10 @@
<?php
namespace Joli\ArDrone\Navdata;
use Joli\ArDrone\Buffer\Buffer;
use Joli\ArDrone\Navdata\Util;
use Joli\ArDrone\Navdata\Option;
class Frame {
class Frame
{
/**
* @var \Joli\ArDrone\Buffer
*/
@@ -44,43 +43,43 @@ class Frame {
public function __construct($binaryFrame)
{
// from ARDrone_SDK_2_0/ARDroneLib/Soft/Common/config.h
$this->droneStateMasks = array(
'flying' => (1 << 0), /*!< FLY MASK => (0) ardrone is landed, (1) ardrone is flying */
'videoEnabled' => (1 << 1), /*!< VIDEO MASK => (0) video disable, (1) video enable */
'visionEnabled' => (1 << 2), /*!< VISION MASK => (0) vision disable, (1) vision enable */
'controlAlgorithm' => (1 << 3), /*!< CONTROL ALGO => (0) euler angles control, (1) angular speed control */
'altitudeControlAlgorithm' => (1 << 4), /*!< ALTITUDE CONTROL ALGO => (0) altitude control inactive (1) altitude control active */
'startButtonState' => (1 << 5), /*!< USER feedback => Start button state */
'controlCommandAck' => (1 << 6), /*!< Control command ACK => (0) None, (1) one received */
'cameraReady' => (1 << 7), /*!< CAMERA MASK => (0) camera not ready, (1) Camera ready */
'travellingEnabled' => (1 << 8), /*!< Travelling mask => (0) disable, (1) enable */
'usbReady' => (1 << 9), /*!< USB key => (0) usb key not ready, (1) usb key ready */
'navdataDemo' => (1 << 10), /*!< Navdata demo => (0) All navdata, (1) only navdata demo */
'navdataBootstrap' => (1 << 11), /*!< Navdata bootstrap => (0) options sent in all or demo mode, (1) no navdata options sent */
'motorProblem' => (1 << 12), /*!< Motors status => (0) Ok, (1) Motors problem */
'communicationLost' => (1 << 13), /*!< Communication Lost => (1) com problem, (0) Com is ok */
'softwareFault' => (1 << 14), /*!< Software fault detected - user should land as quick as possible (1) */
'lowBattery' => (1 << 15), /*!< VBat low => (1) too low, (0) Ok */
'userEmergencyLanding' => (1 << 16), /*!< User Emergency Landing => (1) User EL is ON, (0) User EL is OFF*/
'timerElapsed' => (1 << 17), /*!< Timer elapsed => (1) elapsed, (0) not elapsed */
$this->droneStateMasks = [
'flying' => (1 << 0), /*!< FLY MASK => (0) ardrone is landed, (1) ardrone is flying */
'videoEnabled' => (1 << 1), /*!< VIDEO MASK => (0) video disable, (1) video enable */
'visionEnabled' => (1 << 2), /*!< VISION MASK => (0) vision disable, (1) vision enable */
'controlAlgorithm' => (1 << 3), /*!< CONTROL ALGO => (0) euler angles control, (1) angular speed control */
'altitudeControlAlgorithm' => (1 << 4), /*!< ALTITUDE CONTROL ALGO => (0) altitude control inactive (1) altitude control active */
'startButtonState' => (1 << 5), /*!< USER feedback => Start button state */
'controlCommandAck' => (1 << 6), /*!< Control command ACK => (0) None, (1) one received */
'cameraReady' => (1 << 7), /*!< CAMERA MASK => (0) camera not ready, (1) Camera ready */
'travellingEnabled' => (1 << 8), /*!< Travelling mask => (0) disable, (1) enable */
'usbReady' => (1 << 9), /*!< USB key => (0) usb key not ready, (1) usb key ready */
'navdataDemo' => (1 << 10), /*!< Navdata demo => (0) All navdata, (1) only navdata demo */
'navdataBootstrap' => (1 << 11), /*!< Navdata bootstrap => (0) options sent in all or demo mode, (1) no navdata options sent */
'motorProblem' => (1 << 12), /*!< Motors status => (0) Ok, (1) Motors problem */
'communicationLost' => (1 << 13), /*!< Communication Lost => (1) com problem, (0) Com is ok */
'softwareFault' => (1 << 14), /*!< Software fault detected - user should land as quick as possible (1) */
'lowBattery' => (1 << 15), /*!< VBat low => (1) too low, (0) Ok */
'userEmergencyLanding' => (1 << 16), /*!< User Emergency Landing => (1) User EL is ON, (0) User EL is OFF*/
'timerElapsed' => (1 << 17), /*!< Timer elapsed => (1) elapsed, (0) not elapsed */
'MagnometerNeedsCalibration' => (1 << 18), /*!< Magnetometer calibration state => (0) Ok, no calibration needed, (1) not ok, calibration needed */
'anglesOutOfRange' => (1 << 19), /*!< Angles => (0) Ok, (1) out of range */
'tooMuchWind' => (1 << 20), /*!< WIND MASK=> (0) ok, (1) Too much wind */
'ultrasonicSensorDeaf' => (1 << 21), /*!< Ultrasonic sensor => (0) Ok, (1) deaf */
'cutoutDetected' => (1 << 22), /*!< Cutout system detection => (0) Not detected, (1) detected */
'picVersionNumberOk' => (1 << 23), /*!< PIC Version number OK => (0) a bad version number, (1) version number is OK */
'atCodecThreadOn' => (1 << 24), /*!< ATCodec thread ON => (0) thread OFF (1) thread ON */
'navdataThreadOn' => (1 << 25), /*!< Navdata thread ON => (0) thread OFF (1) thread ON */
'videoThreadOn' => (1 << 26), /*!< Video thread ON => (0) thread OFF (1) thread ON */
'acquisitionThreadOn' => (1 << 27), /*!< Acquisition thread ON => (0) thread OFF (1) thread ON */
'controlWatchdogDelay' => (1 << 28), /*!< CTRL watchdog => (1) delay in control execution (> 5ms), (0) control is well scheduled */
'adcWatchdogDelay' => (1 << 29), /*!< ADC Watchdog => (1) delay in uart2 dsr (> 5ms), (0) uart2 is good */
'comWatchdogProblem' => (1 << 30), /*!< Communication Watchdog => (1) com problem, (0) Com is ok */
'emergencyLanding' => (1 << 31) /*!< Emergency landing : (0) no emergency, (1) emergency */
);
'anglesOutOfRange' => (1 << 19), /*!< Angles => (0) Ok, (1) out of range */
'tooMuchWind' => (1 << 20), /*!< WIND MASK=> (0) ok, (1) Too much wind */
'ultrasonicSensorDeaf' => (1 << 21), /*!< Ultrasonic sensor => (0) Ok, (1) deaf */
'cutoutDetected' => (1 << 22), /*!< Cutout system detection => (0) Not detected, (1) detected */
'picVersionNumberOk' => (1 << 23), /*!< PIC Version number OK => (0) a bad version number, (1) version number is OK */
'atCodecThreadOn' => (1 << 24), /*!< ATCodec thread ON => (0) thread OFF (1) thread ON */
'navdataThreadOn' => (1 << 25), /*!< Navdata thread ON => (0) thread OFF (1) thread ON */
'videoThreadOn' => (1 << 26), /*!< Video thread ON => (0) thread OFF (1) thread ON */
'acquisitionThreadOn' => (1 << 27), /*!< Acquisition thread ON => (0) thread OFF (1) thread ON */
'controlWatchdogDelay' => (1 << 28), /*!< CTRL watchdog => (1) delay in control execution (> 5ms), (0) control is well scheduled */
'adcWatchdogDelay' => (1 << 29), /*!< ADC Watchdog => (1) delay in uart2 dsr (> 5ms), (0) uart2 is good */
'comWatchdogProblem' => (1 << 30), /*!< Communication Watchdog => (1) com problem, (0) Com is ok */
'emergencyLanding' => (1 << 31), /*!< Emergency landing : (0) no emergency, (1) emergency */
];
$this->buffer = new Buffer($binaryFrame);
$this->options = array();
$this->options = [];
$this->header = $this->buffer->getUint32LE();
@@ -109,8 +108,8 @@ class Frame {
{
$isChecksum = false;
while(!$isChecksum) {
$idOption = hexdec($this->buffer->getUint16LE());
while (!$isChecksum) {
$idOption = hexdec($this->buffer->getUint16LE());
$nameOption = Option::$optionIds[$idOption];
$sizeOption = $this->buffer->getUint16LE();
@@ -119,19 +118,16 @@ class Frame {
$expectedChecksum = 0;
$checksum = $this->buffer->getUint32LE();
$data = $this->buffer->getData();
for ($i = 0; $i < $this->buffer->getLength() - $sizeOption; $i++) {
for ($i = 0; $i < $this->buffer->getLength() - $sizeOption; ++$i) {
$expectedChecksum = $expectedChecksum + hexdec(bin2hex($data[$i]));
}
$expectedChecksum = dechex($expectedChecksum);
if ($checksum !== $expectedChecksum) {
throw new \Exception('Invalid checksum');
}
}
$option = new Option($idOption, $this->buffer);
@@ -142,7 +138,7 @@ class Frame {
private function checkHeaderIntegrity()
{
return ($this->header === '55667788' || $this->header === '55667789');
return $this->header === '55667788' || $this->header === '55667789';
}
public function getHeader()
@@ -173,22 +169,22 @@ class Frame {
return $this->options;
}
function __toString()
public function __toString()
{
$toString = '';
$toString .= 'HEADER: ' . $this->getHeader() . PHP_EOL;
$toString .= 'DRONE STATE: ' . print_r($this->getDroneState()) . PHP_EOL;
$toString .= 'SEQUENCE NUMBER: ' . $this->getSequenceNumber() . PHP_EOL;
$toString .= 'VISION FLAG: ' . $this->getVisionFlag() . PHP_EOL;
$toString .= 'HEADER: '.$this->getHeader().PHP_EOL;
$toString .= 'DRONE STATE: '.print_r($this->getDroneState()).PHP_EOL;
$toString .= 'SEQUENCE NUMBER: '.$this->getSequenceNumber().PHP_EOL;
$toString .= 'VISION FLAG: '.$this->getVisionFlag().PHP_EOL;
foreach($this->getOptions() as $option) {
$toString .= '------------------------------------------' . PHP_EOL;
$toString .= 'OPTION: ' . $option->getOptionName() . PHP_EOL;
print_r($option->getData());
foreach ($this->getOptions() as $option) {
$toString .= '------------------------------------------'.PHP_EOL;
$toString .= 'OPTION: '.$option->getOptionName().PHP_EOL;
print_r($option->getData());
}
$toString .= '==========================================' . PHP_EOL;
$toString .= '=========================================='.PHP_EOL;
return $toString;
}
+139 -137
Ver Arquivo
@@ -1,9 +1,11 @@
<?php
namespace Joli\ArDrone\Navdata;
use Joli\ArDrone\Buffer\Buffer;
class Option {
class Option
{
/**
* @var \Joli\ArDrone\Buffer
*/
@@ -26,41 +28,42 @@ class Option {
/**
* @property array
*
* @see from ARDrone_SDK_2_0/ARDroneLib/Soft/Common/navdata_keys.h
*/
public static $optionIds = array(
0 => 'demo',
1 => 'time',
2 => 'rawMeasures',
3 => 'physMeasures',
4 => 'gyrosOffsets',
5 => 'eulerAngles',
6 => 'references',
7 => 'trims',
8 => 'rcReferences',
9 => 'pwm',
10 => 'altitude',
11 => 'visionRaw',
12 => 'visionOf',
13 => 'vision',
14 => 'visionPerf',
15 => 'trackersSend',
16 => 'visionDetect',
17 => 'watchdog',
18 => 'adcDataFrame',
19 => 'videoStream',
20 => 'games',
21 => 'pressureRaw',
22 => 'magneto',
23 => 'windSpeed',
24 => 'kalmanPressure',
25 => 'hdvideoStream',
26 => 'wifi',
27 => 'zimmu3000',
65535 => 'checksum'
);
public static $optionIds = [
0 => 'demo',
1 => 'time',
2 => 'rawMeasures',
3 => 'physMeasures',
4 => 'gyrosOffsets',
5 => 'eulerAngles',
6 => 'references',
7 => 'trims',
8 => 'rcReferences',
9 => 'pwm',
10 => 'altitude',
11 => 'visionRaw',
12 => 'visionOf',
13 => 'vision',
14 => 'visionPerf',
15 => 'trackersSend',
16 => 'visionDetect',
17 => 'watchdog',
18 => 'adcDataFrame',
19 => 'videoStream',
20 => 'games',
21 => 'pressureRaw',
22 => 'magneto',
23 => 'windSpeed',
24 => 'kalmanPressure',
25 => 'hdvideoStream',
26 => 'wifi',
27 => 'zimmu3000',
65535 => 'checksum',
];
public static $controlState = array(
public static $controlState = [
0 => 'CTRL_DEFAULT',
1 => 'CTRL_INIT',
2 => 'CTRL_LANDED',
@@ -70,25 +73,25 @@ class Option {
6 => 'CTRL_TRANS_TAKEOFF',
7 => 'CTRL_TRANS_GOTOFIX',
8 => 'CTRL_TRANS_LANDING',
9 => 'CTRL_TRANS_LOOPING'
);
9 => 'CTRL_TRANS_LOOPING',
];
public static $flyState = array(
public static $flyState = [
0 => 'FLYING_OK',
1 => 'FLYING_LOST_ALT',
2 => 'FLYING_LOST_ALT_GO_DOWN',
3 => 'FLYING_ALT_OUT_ZONE',
4 => 'FLYING_COMBINED_YAW',
5 => 'FLYING_BRAKE',
6 => 'FLYING_NO_VISION'
);
6 => 'FLYING_NO_VISION',
];
public function __construct($idOption, Buffer $buffer)
{
$this->buffer = $buffer;
$this->buffer = $buffer;
$this->idOption = $idOption;
$this->data = array();
$this->name = Option::$optionIds[$idOption];
$this->data = [];
$this->name = self::$optionIds[$idOption];
$this->processOption();
}
@@ -113,154 +116,153 @@ class Option {
private function getDemoOptionData()
{
$flyState = Option::$flyState[$this->buffer->getUint16LE()];
$controlState = Option::$controlState[$this->buffer->getUint16LE()];
$flyState = self::$flyState[$this->buffer->getUint16LE()];
$controlState = self::$controlState[$this->buffer->getUint16LE()];
$batteryPercentage = $this->buffer->getUint32LE();
$theta = $this->buffer->getFloat32() / 1000; // [mdeg]
$phi = $this->buffer->getFloat32() / 1000; // [mdeg]
$psi = $this->buffer->getFloat32() / 1000; // [mdeg]
$altitude = $this->buffer->getUint32LE() / 1000; // [mm]
$velocity = $this->buffer->getVector31(); // [mm/s]
$frameIndex = $this->buffer->getUint32LE();
$theta = $this->buffer->getFloat32() / 1000; // [mdeg]
$phi = $this->buffer->getFloat32() / 1000; // [mdeg]
$psi = $this->buffer->getFloat32() / 1000; // [mdeg]
$altitude = $this->buffer->getUint32LE() / 1000; // [mm]
$velocity = $this->buffer->getVector31(); // [mm/s]
$frameIndex = $this->buffer->getUint32LE();
$detection = array(
'camera' => array(
$detection = [
'camera' => [
'rotation' => $this->buffer->getMatrix33(),
'translation' => $this->buffer->getVector31()
),
'tagIndex' => $this->buffer->getUint32LE()
);
'translation' => $this->buffer->getVector31(),
],
'tagIndex' => $this->buffer->getUint32LE(),
];
$detection['camera']['type'] = $this->buffer->getUint32LE();
$drone = array(
'camera' => array(
$drone = [
'camera' => [
'rotation' => $this->buffer->getMatrix33(),
'translation' => $this->buffer->getVector31()
),
);
'translation' => $this->buffer->getVector31(),
],
];
$rotation = array(
$rotation = [
'frontBack' => $theta,
'pitch' => $theta,
'theta' => $theta,
'y' => $theta,
'pitch' => $theta,
'theta' => $theta,
'y' => $theta,
'leftRight' => $phi,
'roll' => $phi,
'phi' => $phi,
'x' => $phi,
'roll' => $phi,
'phi' => $phi,
'x' => $phi,
'clockwise' => $psi,
'yaw' => $psi,
'psi' => $psi,
'z' => $psi
);
'yaw' => $psi,
'psi' => $psi,
'z' => $psi,
];
$data = array(
'controlState' => $controlState,
'flyState' => $flyState,
$data = [
'controlState' => $controlState,
'flyState' => $flyState,
'batteryPercentage' => hexdec($batteryPercentage),
'rotation' => $rotation,
'frontBackDegrees' => $theta,
'leftRightDegrees' => $phi,
'clockwiseDegrees' => $psi,
'altitude' => $altitude,
'altitudeMeters' => $altitude,
'velocity' => $velocity,
'xVelocity' => $velocity['x'],
'yVelocity' => $velocity['y'],
'zVelocity' => $velocity['z'],
'frameIndex' => $frameIndex,
'detection' => $detection,
'drone' => $drone
);
'rotation' => $rotation,
'frontBackDegrees' => $theta,
'leftRightDegrees' => $phi,
'clockwiseDegrees' => $psi,
'altitude' => $altitude,
'altitudeMeters' => $altitude,
'velocity' => $velocity,
'xVelocity' => $velocity['x'],
'yVelocity' => $velocity['y'],
'zVelocity' => $velocity['z'],
'frameIndex' => $frameIndex,
'detection' => $detection,
'drone' => $drone,
];
return $data;
}
private function getVisionDetectData()
{
return array(
'nbDetected' => $this->buffer->getUint32LE(),
'type' => $this->timesMap(4, 'uint32LE'),
'xc' => $this->timesMap(4, 'uint32LE'),
'yc' => $this->timesMap(4, 'uint32LE'),
'width' => $this->timesMap(4, 'uint32LE'),
'height' => $this->timesMap(4, 'uint32LE'),
'dist' => $this->timesMap(4, 'uint32LE'),
'orientationAngle' => $this->timesMap(4, 'float32'),
'rotation' => $this->timesMap(4, 'matrix33'),
'translation' => $this->timesMap(4, 'vector31'),
'cameraSource' => $this->timesMap(4, 'uint32LE')
);
return [
'nbDetected' => $this->buffer->getUint32LE(),
'type' => $this->timesMap(4, 'uint32LE'),
'xc' => $this->timesMap(4, 'uint32LE'),
'yc' => $this->timesMap(4, 'uint32LE'),
'width' => $this->timesMap(4, 'uint32LE'),
'height' => $this->timesMap(4, 'uint32LE'),
'dist' => $this->timesMap(4, 'uint32LE'),
'orientationAngle' => $this->timesMap(4, 'float32'),
'rotation' => $this->timesMap(4, 'matrix33'),
'translation' => $this->timesMap(4, 'vector31'),
'cameraSource' => $this->timesMap(4, 'uint32LE'),
];
}
private function getPwmData()
{
return array(
'motor' => $this->timesMap(4, 'uint8'),
'satMotors' => $this->timesMap(4, 'uint8'),
'gazFeedForward' => $this->buffer->getFloat32(),
'gazAltitude' => $this->buffer->getFloat32(),
return [
'motor' => $this->timesMap(4, 'uint8'),
'satMotors' => $this->timesMap(4, 'uint8'),
'gazFeedForward' => $this->buffer->getFloat32(),
'gazAltitude' => $this->buffer->getFloat32(),
'altitudeIntegral' => $this->buffer->getFloat32(),
'vzRef' => $this->buffer->getFloat32(),
'uPitch' => $this->buffer->getInt32(),
'uRoll' => $this->buffer->getInt32(),
'uYaw' => $this->buffer->getInt32(),
'yawUI' => $this->buffer->getFloat32(),
'uPitchPlanif' => $this->buffer->getInt32(),
'uRollPlanif' => $this->buffer->getInt32(),
'uYawPlanif' => $this->buffer->getInt32(),
'uGazPlanif' => $this->buffer->getFloat32(),
'motorCurrents' => $this->timesMap(4, 'uint16LE'),
'altitudeProp' => $this->buffer->getFloat32(),
'altitudeDer' => $this->buffer->getFloat32()
);
'vzRef' => $this->buffer->getFloat32(),
'uPitch' => $this->buffer->getInt32(),
'uRoll' => $this->buffer->getInt32(),
'uYaw' => $this->buffer->getInt32(),
'yawUI' => $this->buffer->getFloat32(),
'uPitchPlanif' => $this->buffer->getInt32(),
'uRollPlanif' => $this->buffer->getInt32(),
'uYawPlanif' => $this->buffer->getInt32(),
'uGazPlanif' => $this->buffer->getFloat32(),
'motorCurrents' => $this->timesMap(4, 'uint16LE'),
'altitudeProp' => $this->buffer->getFloat32(),
'altitudeDer' => $this->buffer->getFloat32(),
];
}
private function getPhysMeasuresData()
{
return array(
'temperature' => array(
return [
'temperature' => [
'accelerometer' => $this->buffer->getFloat32(),
'gyroscope' => $this->buffer->getUint16LE()
),
'gyroscope' => $this->buffer->getUint16LE(),
],
'accelerometers' => $this->buffer->getVector31(),
'gyroscopes' => $this->buffer->getVector31(),
'alim3V3' => $this->buffer->getUint32LE(),
'vrefEpson' => $this->buffer->getUint32LE(),
'vrefIDG' => $this->buffer->getUint32LE()
);
'gyroscopes' => $this->buffer->getVector31(),
'alim3V3' => $this->buffer->getUint32LE(),
'vrefEpson' => $this->buffer->getUint32LE(),
'vrefIDG' => $this->buffer->getUint32LE(),
];
}
private function timesMap($n, $type)
{
$data = array();
$data = [];
for($i = 0; $i < $n; $i++) {
for ($i = 0; $i < $n; ++$i) {
$value = null;
if ($type === 'uint32LE') {
$value = $this->buffer->getUint32LE();
} else if ($type === 'uint16LE') {
} elseif ($type === 'uint16LE') {
$value = $this->buffer->getUint16LE();
} else if ($type === 'float32') {
} elseif ($type === 'float32') {
$value = $this->buffer->getFloat32();
} else if ($type === 'matrix33') {
} elseif ($type === 'matrix33') {
$value = $this->buffer->getMatrix33();
} else if ($type === 'vector31') {
} elseif ($type === 'vector31') {
$value = $this->buffer->getVector31();
} else if ($type === 'uint8') {
} elseif ($type === 'uint8') {
$value = $this->buffer->getUint8();
}
array_push($data, $value);
}
}
public function getOptionName()
{
return Option::$optionIds[$this->idOption];
return self::$optionIds[$this->idOption];
}
/**
+7 -10
Ver Arquivo
@@ -4,9 +4,8 @@ namespace Joli\ArDrone\Navdata;
use Evenement\EventEmitter;
use Joli\ArDrone\Config\Config;
use React\Datagram\Factory AS UdpFactory;
use React\Datagram\Socket AS UdpSocket;
use Joli\ArDrone\Navdata\Frame;
use React\Datagram\Factory as UdpFactory;
use React\Datagram\Socket as UdpSocket;
class UdpNavdata extends EventEmitter
{
@@ -28,7 +27,7 @@ class UdpNavdata extends EventEmitter
public function __construct($loop)
{
$this->port = Config::CONTROL_PORT;
$this->ip = Config::DRONE_IP;
$this->ip = Config::DRONE_IP;
$this->loop = $loop;
$this->start();
@@ -36,11 +35,9 @@ class UdpNavdata extends EventEmitter
private function start()
{
// $socket = $this->socket;
$udpFactory = new UdpFactory($this->loop);
$udpNavdata = $this;
//var_dump(Config::DRONE_IP); var_dump(Config::NAVDATA_PORT);die();
// Navdata stream
$udpFactory->createClient(Config::DRONE_IP.':'.Config::NAVDATA_PORT)->then(function (UdpSocket $client) use (&$udpNavdata) {
// Start dialog
@@ -48,10 +45,10 @@ class UdpNavdata extends EventEmitter
$client->send('1');
$client->on('message',
function($message) use (&$udpNavdata) {
function ($message) use (&$udpNavdata) {
$frame = new Frame($message);
$udpNavdata->emit('navdata', array($frame));
$udpNavdata->emit('navdata', [$frame]);
});
});
}
}
}
+11 -9
Ver Arquivo
@@ -1,11 +1,12 @@
<?php
namespace Joli\ArDrone;
use Evenement\EventEmitter;
use Joli\ArDrone\Config\Config;
class Repl extends EventEmitter {
class Repl extends EventEmitter
{
/**
* @var \React\EventLoop\LibEventLoop|\React\EventLoop\StreamSelectLoop
*/
@@ -18,7 +19,7 @@ class Repl extends EventEmitter {
public function __construct($loop)
{
$this->loop = $loop;
$this->loop = $loop;
$this->prompt = 'drone> ';
}
@@ -30,14 +31,14 @@ class Repl extends EventEmitter {
$this->loop->addReadStream(STDIN, function ($stdin) use ($that) {
$input = trim(fgets($stdin));
if(in_array($input, Config::$commands)) {
if($input === 'exit') {
if (in_array($input, Config::$commands)) {
if ($input === 'exit') {
exit;
} else {
$that->emit('action', array($input));
$that->emit('action', [$input]);
}
} else {
echo 'Unknown command' . PHP_EOL;
echo 'Unknown command'.PHP_EOL;
}
echo $that->prompt;
@@ -48,7 +49,8 @@ class Repl extends EventEmitter {
echo $this->prompt;
}
private function getAsciiArt() {
private function getAsciiArt()
{
return "
_ __ | |__ _ __ __ _ _ __ __| |_ __ ___ _ __ ___
| '_ \| '_ \| '_ \ _____ / _` | '__|____ / _` | '__/ _ \| '_ \ / _ \
@@ -57,4 +59,4 @@ class Repl extends EventEmitter {
|_| |_|
";
}
}
}