diff --git a/README.md b/README.md index 270e795..0e4a96d 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,9 @@ Just call the script you want among: Example: ``` bash -php src/forward.php +./bin/forward.php +# or +php bin/forward.php ``` Or using docker: @@ -40,7 +42,7 @@ Or using docker: ``` bash make bash -php src/forward.php +./bin/forward.php ``` diff --git a/bin/autoload.php b/bin/autoload.php new file mode 100644 index 0000000..317dca9 --- /dev/null +++ b/bin/autoload.php @@ -0,0 +1,43 @@ +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 + ); + } +} diff --git a/bin/backward.php b/bin/backward.php new file mode 100755 index 0000000..b770d68 --- /dev/null +++ b/bin/backward.php @@ -0,0 +1,18 @@ +#!/usr/bin/php +backward(); + +$api->sleepMoveTime(); + +$api->stop(); + +echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/bin/forward.php b/bin/forward.php new file mode 100755 index 0000000..44fe527 --- /dev/null +++ b/bin/forward.php @@ -0,0 +1,18 @@ +#!/usr/bin/php +forward(); + +$api->sleepMoveTime(); + +$api->stop(); + +echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/bin/left.php b/bin/left.php new file mode 100755 index 0000000..4f37dd4 --- /dev/null +++ b/bin/left.php @@ -0,0 +1,18 @@ +#!/usr/bin/php +left(); + +$api->sleepRotateTime(); + +$api->stop(); + +echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/bin/right.php b/bin/right.php new file mode 100755 index 0000000..7265a0f --- /dev/null +++ b/bin/right.php @@ -0,0 +1,18 @@ +#!/usr/bin/php +right(); + +$api->sleepRotateTime(); + +$api->stop(); + +echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/composer.json b/composer.json index 7abc966..b1937f6 100644 --- a/composer.json +++ b/composer.json @@ -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/" } } } diff --git a/src/Api.php b/src/Api.php new file mode 100644 index 0000000..c497a7d --- /dev/null +++ b/src/Api.php @@ -0,0 +1,61 @@ +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'))); + } +} diff --git a/src/Motor.php b/src/Motor.php index f842198..a98f203 100644 --- a/src/Motor.php +++ b/src/Motor.php @@ -1,5 +1,7 @@ backward(); - -sleepMoveTime(); - -$robot->stop(); - -echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/src/config.php b/src/config.php deleted file mode 100644 index 152fc7e..0000000 --- a/src/config.php +++ /dev/null @@ -1,33 +0,0 @@ -load(); - -$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) -); - -$robot = new Robot($motorLeft, $motorRight); - -function sleepMoveTime() { - usleep(1E6 * floatval(getenv('MOVE_TIME'))); -} - -function sleepRotateTime() { - usleep(1E6 * floatval(getenv('ROTATE_TIME'))); -} diff --git a/src/forward.php b/src/forward.php deleted file mode 100644 index c157536..0000000 --- a/src/forward.php +++ /dev/null @@ -1,13 +0,0 @@ -forward(); - -sleepMoveTime(); - -$robot->stop(); - -echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/src/left.php b/src/left.php deleted file mode 100644 index d67b38e..0000000 --- a/src/left.php +++ /dev/null @@ -1,13 +0,0 @@ -left(); - -sleepRotateTime(); - -$robot->stop(); - -echo 'Ok, Im now stopped.', PHP_EOL; diff --git a/src/right.php b/src/right.php deleted file mode 100644 index 35b33dd..0000000 --- a/src/right.php +++ /dev/null @@ -1,13 +0,0 @@ -right(); - -sleepRotateTime(); - -$robot->stop(); - -echo 'Ok, Im now stopped.', PHP_EOL;