Skip to content

Commit

Permalink
Fixed motor direction
Browse files Browse the repository at this point in the history
  • Loading branch information
calcinai committed Aug 13, 2016
1 parent d82ba5b commit 764d414
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
20 changes: 17 additions & 3 deletions examples/motor-step.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@
*/


use Calcinai\PHPi\External\Generic\Button;
use Calcinai\PHPi\External\Generic\Motor\Stepper;

include __DIR__.'/../vendor/autoload.php';

$board = \Calcinai\PHPi\Factory::create();
$loop = \React\EventLoop\Factory::create();
$board = \Calcinai\PHPi\Factory::create($loop);

//This is for a 4 phase motor - can be any number of phases
$motor = new Stepper([$board->getPin(5), $board->getPin(6), $board->getPin(13), $board->getPin(19)]);
$motor->setSpeed(10);

$board->getLoop()->addPeriodicTimer(0.5, [$motor, 'step']);
//Can also hook up events like this:
//$button = new Button($board->getPin(17));
//$button->on('press', [$motor, 'step']);


$board->getLoop()->run();
//Forward for 2 sec, rev for 2 sec
$loop->addPeriodicTimer(2, function() use($motor){
//Long winded way to toggle direction
$motor->getDirection() === Stepper::DIRECTION_FORWARD ? $motor->reverse() : $motor->forward();
});

$motor->forward();


$loop->run();
11 changes: 11 additions & 0 deletions src/PHPi/Exception/InvalidModeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* @package calcinai/phpi
* @author Michael Calcinai <[email protected]>
*/

namespace Calcinai\PHPi\Exception;

class InvalidModeException extends \Exception {

}
22 changes: 18 additions & 4 deletions src/PHPi/External/Generic/Motor/Stepper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ public function setMode($mode){
public function setSpeed($speed){
$this->speed = $speed;
$this->period = 1/$speed;
return $this->stop()->start();

if($this->isRunning()){
$this->restart();
}
}

/**
Expand All @@ -135,6 +138,13 @@ public function setDirection($direction){
return $this;
}

/**
* @return mixed
*/
public function getDirection(){
return $this->direction;
}

/**
* Seconds until full speed
*
Expand All @@ -146,12 +156,12 @@ public function setAcceleration($acceleration){

public function forward() {
return $this->setDirection(self::DIRECTION_FORWARD)
->start();
->restart();
}

public function reverse() {
return $this->setDirection(self::DIRECTION_REVERSE)
->start();
->restart();
}

public function stop() {
Expand Down Expand Up @@ -197,6 +207,10 @@ public function start(){
}


public function restart(){
return $this->stop()->start();
}

/**
* Shift the pattern to the next phase
* This really is write-only code!
Expand All @@ -208,7 +222,7 @@ public function step(){

//Preform a circular shift of the pattern in the appropriate direction
//This is for the main pattern
if($this->direction){
if($this->direction === self::DIRECTION_FORWARD){
$shifted = $this->mask << 1 | $this->mask >> ($this->phase_msb);
} else {
$shifted = $this->mask >> 1 | $this->mask << ($this->phase_msb);
Expand Down

0 comments on commit 764d414

Please sign in to comment.