/** * Start the server by listening on a specified port and address. * @param int $port * @param string $host * @throws ConnectionException */ public function listen($port, $host = '0.0.0.0') { $factory = new Factory($this->loop); $factory->createServer($host . ':' . $port)->then(function (Socket $server) { $this->socket = $server; $server->on('message', function ($message) { $this->emit('data', [$message]); }); }); }
private function start() { $udpFactory = new UdpFactory($this->loop); $udpNavdata = $this; // Navdata stream $udpFactory->createClient(Config::DRONE_IP . ':' . Config::NAVDATA_PORT)->then(function (UdpSocket $client) use(&$udpNavdata) { // Start dialog $client->send('1'); $client->send('1'); $client->on('message', function ($message) use(&$udpNavdata) { $frame = new Frame($message); $udpNavdata->emit('navdata', [$frame]); }); }); }
private function start() { $udpFactory = new UdpFactory($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; // Start dialog $client->send('1'); $client->send('1'); for ($j = 0; $j < 5; ++$j) { $cmds = []; 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); } // 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_push($cmds, $commandCreator->createRefCommand($ref)); array_push($cmds, $commandCreator->createPcmdCommand($pcmd)); if (count($anim) > 0) { for ($i = 0; $i <= 10; ++$i) { foreach ($anim as $name => $duration) { array_push($cmds, $commandCreator->createConfigCommand($name, $duration)); } } $anim = []; } $cmds = implode('', $cmds); $client->send($cmds); }); $udpControl->on('land', function () use(&$ref, &$pcmd) { $pcmd = []; $ref['fly'] = false; }); $udpControl->on('ftrim', function () use(&$client, &$commandCreator) { $client->send($commandCreator->createFtrimCommand()); }); $udpControl->on('takeoff', function () use(&$ref, &$pcmd) { $pcmd = []; $ref['fly'] = true; }); $udpControl->on('clockwise', function ($speed = 0.5) use(&$pcmd) { $pcmd['clockwise'] = $speed; unset($pcmd['counterClockwise']); }); $udpControl->on('counterClockwise', function ($speed = 0.5) use(&$pcmd) { $pcmd['counterClockwise'] = $speed; unset($pcmd['clockwise']); }); $udpControl->on('stop', function () use(&$pcmd) { $pcmd = []; }); $udpControl->on('front', function ($speed = 0.3) use(&$pcmd) { $pcmd['front'] = $speed; unset($pcmd['back']); }); $udpControl->on('back', function ($speed = 0.3) use(&$pcmd) { $pcmd['back'] = $speed; unset($pcmd['front']); }); $udpControl->on('right', function ($speed = 0.3) use(&$pcmd) { $pcmd['right'] = $speed; unset($pcmd['left']); }); $udpControl->on('left', function ($speed = 0.3) use(&$pcmd) { $pcmd['left'] = $speed; unset($pcmd['right']); }); $udpControl->on('up', function ($speed = 0.6) use(&$pcmd) { $pcmd['up'] = $speed; unset($pcmd['down']); }); $udpControl->on('down', function ($speed = 0.6) use(&$pcmd) { $pcmd['down'] = $speed; unset($pcmd['up']); }); $udpControl->on('flip', function () use(&$anim) { $anim['control:flight_anim'] = '16,5'; }); }); }