Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ Just call the script you want among:
Example:

``` bash
php src/forward.php
./bin/forward.php
# or
php bin/forward.php
```

Or using docker:

``` bash
make bash

php src/forward.php
./bin/forward.php
```


Expand Down
43 changes: 43 additions & 0 deletions bin/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

$autoloadLocations = [
// In case library is the project and vendor are installed
__DIR__.'/../vendor/autoload.php',

// In case library is loaded as vendor
__DIR__.'/../../../autoload.php',
];

$found = false;

foreach ($autoloadLocations as $autoloadLocation) {
if (file_exists($autoloadLocation)) {
$found = true;
require_once($autoloadLocation);
}
}

if (!$found) {
throw new Exception('Impossible to autoload dependencies. Are composer dependencies installed ?');
}

$envFile = __DIR__.'/../.env';

foreach ($argv as $arg) {
if (preg_match('/^--env=(.*)$/', $arg, $matches)) {
$envFile = realpath($matches[1]);
}
}

if (file_exists($envFile)) {
$dotenv = new \Dotenv\Dotenv(dirname($envFile), basename($envFile));
$dotenv->load();
} else {
if (!getenv('PIN_MOTOR_LEFT_EN')) {
trigger_error(
'Environment variables seems to not be loaded. '
.'Be sure to provide .env file with --env=path/to/.env',
E_USER_WARNING
);
}
}
18 changes: 18 additions & 0 deletions bin/backward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/php
<?php

require_once __DIR__.'/autoload.php';

use Drop\RobotApi\Api;

$api = new Api();

echo 'Lets go backward...', PHP_EOL;

$api->backward();

$api->sleepMoveTime();

$api->stop();

echo 'Ok, Im now stopped.', PHP_EOL;
18 changes: 18 additions & 0 deletions bin/forward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/php
<?php

require_once __DIR__.'/autoload.php';

use Drop\RobotApi\Api;

$api = new Api();

echo 'Lets go forward...', PHP_EOL;

$api->forward();

$api->sleepMoveTime();

$api->stop();

echo 'Ok, Im now stopped.', PHP_EOL;
18 changes: 18 additions & 0 deletions bin/left.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/php
<?php

require_once __DIR__.'/autoload.php';

use Drop\RobotApi\Api;

$api = new Api();

echo 'Lets turn left...', PHP_EOL;

$api->left();

$api->sleepRotateTime();

$api->stop();

echo 'Ok, Im now stopped.', PHP_EOL;
18 changes: 18 additions & 0 deletions bin/right.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/php
<?php

require_once __DIR__.'/autoload.php';

use Drop\RobotApi\Api;

$api = new Api();

echo 'Lets turn right...', PHP_EOL;

$api->right();

$api->sleepRotateTime();

$api->stop();

echo 'Ok, Im now stopped.', PHP_EOL;
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"name": "darkmira/drop-robotapi",
"bin": [
"bin/forward.php",
"bin/backward.php",
"bin/left.php",
"bin/right.php"
],
"require": {
"calcinai/phpi": "^0.3.0",
"vlucas/phpdotenv": "^2.4"
},
"autoload": {
"psr-4": {
"": "src/"
"Drop\\RobotApi\\": "src/"
}
}
}
61 changes: 61 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drop\RobotApi;

use Calcinai\PHPi\Factory as BoardFactory;
use Calcinai\PHPi\Pin\PinFunction;

class Api
{
public function __construct()
{
$board = BoardFactory::create();

$motorLeft = new Motor(
$board->getPin(intval(getenv('PIN_MOTOR_LEFT_EN')))->setFunction(PinFunction::OUTPUT),
$board->getPin(intval(getenv('PIN_MOTOR_LEFT_A')))->setFunction(PinFunction::OUTPUT),
$board->getPin(intval(getenv('PIN_MOTOR_LEFT_B')))->setFunction(PinFunction::OUTPUT)
);

$motorRight = new Motor(
$board->getPin(intval(getenv('PIN_MOTOR_RIGHT_EN')))->setFunction(PinFunction::OUTPUT),
$board->getPin(intval(getenv('PIN_MOTOR_RIGHT_A')))->setFunction(PinFunction::OUTPUT),
$board->getPin(intval(getenv('PIN_MOTOR_RIGHT_B')))->setFunction(PinFunction::OUTPUT)
);

$this->robot = new Robot($motorLeft, $motorRight);
}

public function forward()
{
$this->robot->forward();
}

public function backward()
{
$this->robot->backward();
}

public function left()
{
$this->robot->left();
}

public function right()
{
$this->robot->right();
}

public function stop()
{
$this->robot->stop();
}

public function sleepMoveTime() {
usleep(1E6 * floatval(getenv('MOVE_TIME')));
}

public function sleepRotateTime() {
usleep(1E6 * floatval(getenv('ROTATE_TIME')));
}
}
2 changes: 2 additions & 0 deletions src/Motor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Drop\RobotApi;

use Calcinai\PHPi\Pin;

class Motor
Expand Down
2 changes: 2 additions & 0 deletions src/Robot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Drop\RobotApi;

class Robot
{
/**
Expand Down
13 changes: 0 additions & 13 deletions src/backward.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/config.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/forward.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/left.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/right.php

This file was deleted.