Add flip, ftrim commands, improve stability

Esse commit está contido em:
Shinework
2013-11-19 18:57:34 +01:00
commit f543c6bd56
8 arquivos alterados com 72 adições e 35 exclusões
+1 -1
Ver Arquivo
@@ -1,6 +1,6 @@
{
"name": "jolicode/php-ar-drone",
"description": "Control your AR Drone with PHP",
"description": "Control your AR Drone over PHP",
"require": {
"clue/datagram": "dev-master",
"react/socket-client": "dev-master"
+5 -9
Ver Arquivo
@@ -11,7 +11,6 @@ use Joli\ArDrone\Config\Config;
class Client extends EventEmitter {
private $udpFactory;
private $udpControl;
private $udpNavdata;
private $timerOffset;
@@ -25,8 +24,7 @@ class Client extends EventEmitter {
{
$this->loop = LoopFactory::create();
$udpFactory = new UdpFactory($this->loop);
$this->udpFactory = $udpFactory;
$this->udpFactory = new UdpFactory($this->loop);
$this->socket = null;
$this->timerOffset = 0;
$this->lastState = 'CTRL_LANDED';
@@ -47,10 +45,9 @@ class Client extends EventEmitter {
if (count($navdata->getDroneState()) > 0) {
$stateData = $navdata->getDroneState();
if ($stateData['emergencyLanding'] && $self->disableEmergency) {
// this._ref.emergency = true;
//todo: disable emergency state
} else {
// this._ref.emergency = false;
// this._disableEmergency = false;
//todo: disable emergency state
}
}
@@ -72,7 +69,6 @@ class Client extends EventEmitter {
$battery = $demoData['batteryPercentage'];
// battery events
$stateData = $navdata->getDroneState();
@@ -156,13 +152,13 @@ class Client extends EventEmitter {
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') {
} else if ($name === 'stop' || $name === 'ftrim' || $name === 'flip') {
$this->udpControl->emit($name);
// Control commands
} else {
+2
Ver Arquivo
@@ -21,6 +21,8 @@ class Config {
'down',
'stop',
'exit',
'ftrim',
'flip'
);
+9 -1
Ver Arquivo
@@ -12,6 +12,7 @@ class AtCommand {
const TYPE_CALIB = 'CALIB';
const TYPE_CONFIG = 'CONFIG';
const TYPE_FTRIM = 'FTRIM';
const TYPE_ANIM = 'ANIM';
public function __construct($sequence, $type, $args)
{
@@ -22,7 +23,14 @@ class AtCommand {
function __toString()
{
$command = 'AT*' . $this->type . '=' . $this->sequence . ','. implode(',', $this->args) ."\r";
$command = 'AT*' . $this->type . '=' . $this->sequence;
if (count($this->args) > 0) {
$command .= ','. implode(',', $this->args);
}
$command .= "\r";
return $command;
}
+30 -9
Ver Arquivo
@@ -68,11 +68,28 @@ class AtCommandCreator {
$args[$alias['index']] = $this->floatToIEEE($value);
}
if ($args[1] != 0 || $args[2] != 0 ) {
$args[0] = 1;
}
$this->sequence++;
return new AtCommand($this->sequence, AtCommand::TYPE_PCMD, $args);
}
public function createFtrimCommand()
{
return new AtCommand($this->sequence, AtCommand::TYPE_FTRIM, array());
}
public function createAnimCommand()
{
$args = array(17, 1);
return new AtCommand($this->sequence, AtCommand::TYPE_ANIM, $args);
}
private function floatToIEEE($floatInt) {
$floatInt = (float) $floatInt;
$binInt = pack('f', $floatInt);
@@ -83,17 +100,21 @@ class AtCommandCreator {
$hexInt = sprintf("%02X", $c).$hexInt;
}
$binIntString = decbin(hexdec($hexInt));
$twoComplement = '';
if ($floatInt < 0) {
$binIntString = decbin(hexdec($hexInt));
$twoComplement = '';
for($i=0; $i < strlen($binIntString); $i++) {
if ($binIntString[$i] == '0') {
$twoComplement .= '1';
} else {
$twoComplement .= '0';
for($i=0; $i < strlen($binIntString); $i++) {
if ($binIntString[$i] == '0') {
$twoComplement .= '1';
} else {
$twoComplement .= '0';
}
}
}
return -(bindec($twoComplement) + 1);
return -(bindec($twoComplement) + 1);
} else {
return hexdec($hexInt);
}
}
}
+22 -11
Ver Arquivo
@@ -50,7 +50,7 @@ class UdpControl extends EventEmitter {
array_push($cmds, $commandCreator->createConfigCommand('general:navdata_demo', 'TRUE'));
array_push($cmds, $commandCreator->createPcmdCommand($pcmd));
array_push($cmds, $commandCreator->createRefCommand($ref));
//
$cmds = implode('', $cmds);
$client->send($cmds);
sleep(0.03);
@@ -60,10 +60,10 @@ class UdpControl extends EventEmitter {
// by sending the AT-commands every 30 ms for smooth drone movements.
$loop->addPeriodicTimer(0.03, function() use ($client, $commandCreator, &$ref, &$pcmd) {
$cmds = array();
//
array_push($cmds, $commandCreator->createRefCommand($ref));
array_push($cmds, $commandCreator->createPcmdCommand($pcmd));
//
$cmds = implode('', $cmds);
$client->send($cmds);
});
@@ -74,17 +74,21 @@ class UdpControl extends EventEmitter {
$ref['fly'] = false;
});
$udpControl->on('ftrim', function() use (&$client, &$commandCreator) {
$client->send($commandCreator->createFtrimCommand());
});
$udpControl->on('takeoff', function() use (&$ref, &$pcmd) {
$pcmd = array();
$ref['fly'] = true;
});
$udpControl->on('clockwise', function($speed) use (&$pcmd) {
$udpControl->on('clockwise', function($speed = 0.5) use (&$pcmd) {
$pcmd['clockwise'] = $speed;
unset($pcmd['counterClockwise']);
});
$udpControl->on('counterClockwise', function($speed) use (&$pcmd) {
$udpControl->on('counterClockwise', function($speed = 0.5) use (&$pcmd) {
$pcmd['counterClockwise'] = $speed;
unset($pcmd['clockwise']);
});
@@ -93,35 +97,42 @@ class UdpControl extends EventEmitter {
$pcmd = array();
});
$udpControl->on('front', function($speed) use (&$pcmd) {
$udpControl->on('front', function($speed = 0.3) use (&$pcmd) {
$pcmd['front'] = $speed;
unset($pcmd['back']);
});
$udpControl->on('back', function($speed) use (&$pcmd) {
$udpControl->on('back', function($speed = 0.3) use (&$pcmd) {
$pcmd['back'] = $speed;
unset($pcmd['front']);
});
$udpControl->on('right', function($speed) use (&$pcmd) {
$udpControl->on('right', function($speed = 0.3) use (&$pcmd) {
$pcmd['right'] = $speed;
unset($pcmd['left']);
});
$udpControl->on('left', function($speed) use (&$pcmd) {
$udpControl->on('left', function($speed = 0.3) use (&$pcmd) {
$pcmd['left'] = $speed;
unset($pcmd['right']);
});
$udpControl->on('up', function($speed) use (&$pcmd) {
$udpControl->on('up', function($speed = 0.3) use (&$pcmd) {
$pcmd['up'] = $speed;
unset($pcmd['down']);
});
$udpControl->on('down', function($speed) use (&$pcmd) {
$udpControl->on('down', function($speed = 0.3) use (&$pcmd) {
$pcmd['down'] = $speed;
unset($pcmd['up']);
});
$udpControl->on('flip', function() use (&$client, &$commandCreator) {
for ($i=0; $i++; $i < 20) {
$client->send($commandCreator->createConfigCommand('control:flight_anim', '16,5'));
}
});
});
}
}
+2 -3
Ver Arquivo
@@ -5,7 +5,6 @@ use Joli\ArDrone\Buffer\Buffer;
use Joli\ArDrone\Navdata\Util;
use Joli\ArDrone\Navdata\Option;
class Frame {
private $buffer;
@@ -120,8 +119,7 @@ class Frame {
private function checkHeaderIntegrity()
{
return true;
// return ($this->header === 55667788);
return ($this->header === '55667788' || $this->header === '55667789');
}
public function getHeader()
@@ -164,6 +162,7 @@ class Frame {
foreach($this->getOptions() as $option) {
$toString .= '------------------------------------------' . PHP_EOL;
$toString .= 'OPTION: ' . $option->getOptionName() . PHP_EOL;
print_r($option->getData());
}
$toString .= '==========================================' . PHP_EOL;
+1 -1
Ver Arquivo
@@ -245,7 +245,7 @@ class Option {
}
/**
* @return \jolicode\PhpARDrone\Buffer\Buffer
* @return \Joli\ArDrone\Buffer\Buffer
*/
public function getBuffer()
{