Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kolban committed Apr 30, 2016
1 parent 4761d5c commit d0ff681
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 40 deletions.
154 changes: 114 additions & 40 deletions src/stepper-wiringpi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var wpi = require('wiring-pi');

wpi.setup('gpio');


// Create definitions for the constants.
var FORWARD = 1;
var BACKWARD = -1;
Expand All @@ -31,7 +30,7 @@ var motorIndex=1;


function setup2Wire(motorPin1, motorPin2)
{
{
// Pi pins for the motor control connection:
this._motorPin1 = motorPin1;
this._motorPin2 = motorPin2;
Expand Down Expand Up @@ -92,6 +91,45 @@ function setup5Wire(motorPin1, motorPin2, motorPin3, motorPin4, motorPin5)
this._pinCount = 5;
} // End of setup5Wire


exports.setupDigital = function(stepsPerRevolution, stepPin, directionPin, usePWM) {
var context = {
step: step, // Function
setSpeed: setSpeed, // Function
forward: forward, // Function
backward: backward, // Function
stop: stop, // Function
halt: halt, // Function

_stepDelay: 60*1000/stepsPerRevolution, // Set the default step delay to 1 rpm.
_direction: FORWARD, // Motor direction.
_timerId: null, // Interval object for stepping fixed number of steps.
_moveTimeoutId: null, // Timeout object for continuous rotation
_motorIndex: motorIndex, // The index of the motor used for debugging purposes.
_stepPin: stepPin, // The pin used to step via a pulse
_directionPin: directionPin, // The direction to rotate
_stepsPerRevolution: stepsPerRevolution, // Total number of steps for this motor.
_isDigital: true, // Is this a digital controller?
_usePWM: usePWM // Use pwm for timing?
};
motorIndex++; // Increment the global motorIndex count (used for debugging).

wpi.pinMode(directionPin, wpi.OUTPUT);

// If we are using PWM, then set the step pin to be PWM and set its
// initial value to zero.
if (usePWM) {
wpi.pwmSetMode(wpi.PWM_MODE_MS);
wpi.pwmSetClock(1920);
wpi.pwmSetRange(10); // Some value
wpi.pinMode(stepPin, wpi.PWM_OUTPUT);
wpi.pwmWrite(stepPin, 0);
} else {
wpi.pinMode(stepPin, wpi.OUTPUT);
}
return context;
}

/**
* Setup the object for usage.
*/
Expand All @@ -111,7 +149,8 @@ exports.setup = function(stepsPerRevolution, motorPin1, motorPin2, motorPin3, mo
_direction: FORWARD, // Motor direction.
_timerId: null, // Interval object for stepping fixed number of steps.
_moveTimeoutId: null, // Timeout object for continuous rotation
_stepsPerRevolution: stepsPerRevolution // Total number of steps for this motor.
_stepsPerRevolution: stepsPerRevolution, // Total number of steps for this motor.
_isDigital: false
}

motorIndex++; // Increment the global motorIndex count (used for debugging).
Expand Down Expand Up @@ -148,8 +187,8 @@ function setSpeed(desiredRPM)
// consider the number of milliseconds in a second ... this is 60*1000. Now
// contemplate the number of steps in a revolution. This is stored in the
// 'numberOfSteps' property. This tells us that to achieve 1 RPM, we would need
// to move a step each interval. As we increase the RPM, we will delay LESS
// per step.
// to move a step each delay interval. As we increase the RPM, we will delay
// LESS per step.
var maxRPM = 60 * 1000 / this._stepsPerRevolution;
if (desiredRPM > maxRPM) {
desiredRPM = maxRPM;
Expand All @@ -158,42 +197,58 @@ function setSpeed(desiredRPM)
} // End of setSpeed


//PUBLIC: Set the motor to rotate forwards at the current set speed.
/**
* PUBLIC:
* Set the motor to rotate continuously forwards at the current set speed.
*/
function forward() {
stop.call(this);
move.call(this, FORWARD);
}
this._direction = FORWARD;
move.call(this);
} // End of forward

// PUBLIC: Set the motor to rotate backwards at the current set speed.

/**
* PUBLIC:
* Set the motor to rotate backwards at the current set speed.
*/
function backward() {
stop.call(this);
move.call(this, BACKWARD);
}
this._direction = BACKWARD;
move.call(this);
} // End of backward


// PRIVATE: Setup for continuous rotation at given speed.
function move(direction) {
//console.log("move: direction: %d", direction);
//console.log("move: %j", this);
if (direction == FORWARD) {
incrementStepNumber.call(this);
} else {
decrementStepNumber.call(this);
function move() {
if (this._usePWM) {
// PWM clock is 0.1ms
// PWM range must be stepDelay * 10 and PWM value must be 1/2 of PWM range
var range = this._stepDelay * 10
wpi.pwmSetRange(range);
wpi.pwmWrite(this._stepPin, range/2);
return;
}
console.log("Step number: %d", this._stepNumber);
stepMotor.call(this, this._stepNumber);
this._moveTimeoutId = setTimeout(move.bind(this), this._stepDelay, direction);
}
//console.log("Step number: %d", this._stepNumber);
stepMotor.call(this);
this._moveTimeoutId = setTimeout(move.bind(this), this._stepDelay);
} // End of move


// PUBLIC: Stop any continuous movement.
function stop() {
if (this._moveTimeoutId != null) {
clearTimeout(this._moveTimeoutId);
this._moveTimeoutId = null;
}
}
if (this._usePWM) {
wpi.pwmWrite(this._stepPin, 0);
}
} // End of stop


/**
* PRIVATE:
* PUBLIC:
* Moves the motor a fixed number of steps defined by `stepsToMove`. If the number is negative,
* the motor moves in the reverse direction. The optional callback
* function will be invoked when the number of steps being asked to
Expand Down Expand Up @@ -244,19 +299,9 @@ function step(stepsToMove, callback)
}
return;
} // End of stepsLeft <= 0

// increment or decrement the step number, depending on direction:
if (this._direction == FORWARD)
{
incrementStepNumber.call(this);
}
else
{
decrementStepNumber.call(this);
}

// step the motor to step number 0, 1, ..., {3 or 10}
stepMotor.call(this, this._stepNumber);
stepMotor.call(this);

stepsLeft--; // Decrement the steps left to move.

Expand All @@ -268,6 +313,9 @@ function step(stepsToMove, callback)
// Halt the motors by setting all the voltages to low. There will now be no
// force restricting the movement of the motors.
function halt() {
if (this._isDigital === true) {
return;
}
wpi.digitalWrite(this._motorPin1, wpi.LOW);
wpi.digitalWrite(this._motorPin2, wpi.LOW);
if (this._pinCount == 2 || this._pintCount == 4) {
Expand All @@ -285,23 +333,49 @@ function incrementStepNumber() {
if (this._stepNumber >= this._stepsPerRevolution) {
this._stepNumber = 0;
}
}
} // End of incrementStepNumber


function decrementStepNumber() {
this._stepNumber--;
if (this._stepNumber < 0) {
this._stepNumber = this._stepsPerRevolution - 1;
}
}
} // End of decrementStepNumber


/*
* PRIVATE:
* Moves the motor forward or backwards.
*/
function stepMotor(thisStep)
function stepMotor()
{
thisStep = Math.abs(thisStep);
console.log("Step: %d, \tmod4=%d", thisStep, thisStep%4);
// For digital movement, we need only pulse the stepPin. First we set the direction
// pin to the desired direction.
// ENHANCEMENT: It is possible we can eliminate the cost of setting the direction and merely look
// set it when the direction changes from the last time we stepped the motor.
console.log("stepMotor: isDigital=%d, stepDelay=%d, stepPin=%d, directionPin=%d",
this._isDigital, this._stepDelay, this._stepPin, this._directionPin);
if (this._isDigital === true) {
if (this._direction == FORWARD) {
wpi.digitalWrite(this._directionPin, wpi.HIGH);
} else {
wpi.digitalWrite(this._directionPin, wpi.LOW);
}
wpi.digitalWrite(this._stepPin, wpi.HIGH);
wpi.delay(5);
wpi.digitalWrite(this._stepPin, wpi.LOW);
return;
}

// We are here ONLY if we are not a digital driver.
if (this._direction == FORWARD) {
incrementStepNumber.call(this);
} else {
decrementStepNumber.call(this);
}
thisStep = Math.abs(this._stepNumber);
//console.log("Step: %d, \tmod4=%d", thisStep, thisStep%4);

if (this._pinCount == 2) {
switch (thisStep % 4) {
Expand Down
41 changes: 41 additions & 0 deletions tests/digital_ForwardBackward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Sample for the stepper-wiringpi.js.
*
* Here we are testing a Stepper motor that has 200 steps per revolution
* which equates to 360/200 = 1.8 degrees per step.
*
* In this test we continuously run forwards for 5 seconds and then continuously run
* backwards for 5 seconds and then keep repeating.
*/

var speed = 150; // RPM
var directionPin = 20; // Pin used for direction
var stepPin = 21; // Pin used for stepping

console.log("Starting stepper-wiringpi - digital_ForwardBackward");

var stepperWiringPi = require("../src/stepper-wiringpi");
var motor1 = stepperWiringPi.setupDigital(200, stepPin, directionPin);
var direction = stepperWiringPi.FORWARD;

console.log("Globals: FORWARD=%d, BACKWARD=%d", stepperWiringPi.FORWARD, stepperWiringPi.BACKWARD);

function changeDirection() {
console.log("Changing direction from %d", direction);
if (direction == stepperWiringPi.FORWARD) {
direction = stepperWiringPi.BACKWARD;
motor1.backward();
} else {
direction = stepperWiringPi.FORWARD;
motor1.forward();
}
setTimeout(changeDirection.bind(this), 5000);
} // End of changeDirection

debugger;
motor1.setSpeed(speed);

changeDirection();

console.log("Starting to move ...");
// End of file
41 changes: 41 additions & 0 deletions tests/digital_ForwardBackward_pwm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Sample for the stepper-wiringpi.js.
*
* Here we are testing a Stepper motor that has 200 steps per revolution
* which equates to 360/200 = 1.8 degrees per step.
*
* In this test we continuously run forwards for 5 seconds and then continuously run
* backwards for 5 seconds and then keep repeating.
*/

var speed = 150; // RPM
var directionPin = 20; // Pin used for direction
var stepPin = 12; // Pin used for stepping

console.log("Starting stepper-wiringpi - digital_ForwardBackward");

var stepperWiringPi = require("../src/stepper-wiringpi");
var motor1 = stepperWiringPi.setupDigital(200, stepPin, directionPin, true);
var direction = stepperWiringPi.FORWARD;

console.log("Globals: FORWARD=%d, BACKWARD=%d", stepperWiringPi.FORWARD, stepperWiringPi.BACKWARD);

function changeDirection() {
console.log("Changing direction from %d", direction);
if (direction == stepperWiringPi.FORWARD) {
direction = stepperWiringPi.BACKWARD;
motor1.backward();
} else {
direction = stepperWiringPi.FORWARD;
motor1.forward();
}
setTimeout(changeDirection.bind(this), 5000);
} // End of changeDirection

debugger;
motor1.setSpeed(speed);

changeDirection();

console.log("Starting to move ...");
// End of file
56 changes: 56 additions & 0 deletions tests/test6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Sample for the stepper-wiringpi.js.
*
* Here we are testing a Stepper motor that has 200 steps per revolution
* which equates to 360/200 = 1.8 degrees per step.
*/
var CH0_Pin = 21;
var wpi = require("wiring-pi");
wpi.setup("gpio");
wpi.pinMode(CH0_Pin, wpi.INPUT);
wpi.pcf8574Setup(100, 0x20);

// In this test we are going to use a PCF8574 for control and control
// two motors
console.log("Starting stepper-wiringpi - test5");

var stepperWiringPi = require("../src/stepper-wiringpi");
var m1pinIN1 = 100; // Stepper Red
var m1pinIN2 = 101; // Stepper Blue
var m1pinIN3 = 102; // Stepper Green
var m1pinIN4 = 103; // Stepper Black

var m2pinIN1 = 104; // Stepper Red
var m2pinIN2 = 105; // Stepper Blue
var m2pinIN3 = 106; // Stepper Green
var m2pinIN4 = 107; // Stepper Black

var motor1 = stepperWiringPi.setup(200, m1pinIN1, m1pinIN2, m1pinIN3, m1pinIN4);
var motor2 = stepperWiringPi.setup(200, m2pinIN1, m2pinIN2, m2pinIN3, m2pinIN4);
var direction = 1;
console.log("Globals: FORWARD=%d, BACKWARD=%d", stepperWiringPi.FORWARD, stepperWiringPi.BACKWARD);

function changeDirection() {
console.log("Changing direction from %d", direction);
if (direction == stepperWiringPi.FORWARD) {
direction = stepperWiringPi.BACKWARD;
motor1.backward();
motor2.backward();
} else {
direction = stepperWiringPi.FORWARD;
motor1.forward();
motor2.forward();
}
setTimeout(changeDirection.bind(this), 2000);
}

motor1.setSpeed(1);
motor2.setSpeed(1);

changeDirection();

setInterval(function() {
var i = wpi.pulseIn(CH0_Pin, wpi.HIGH);
console.log("CH0: " + i);
}, 500);
console.log("Step requested submitted.");

0 comments on commit d0ff681

Please sign in to comment.