Skip to content

Commit

Permalink
Added stepper motor blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
JackAtKitronik committed Feb 24, 2023
1 parent 8277f05 commit ceb3812
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 81 additions & 0 deletions motors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ namespace kitronik_motor_driver {
Motor2
}

let stepperState = 0;
let stepSequence: MotorDirection[] = [MotorDirection.Forward, MotorDirection.Reverse, MotorDirection.Reverse, MotorDirection.Forward];
export let stepperSteps = 200;

/**
* Turns on motor specified by eMotors in the direction specified
* by eDirection, at the requested speed
Expand Down Expand Up @@ -85,4 +89,81 @@ namespace kitronik_motor_driver {
break
}
}

/**
* Sets the stepper motor to a chosen angle relative to the start position.
* @param dir which direction to go
* @param angle how far to turn the motor relative to start
*/
//% blockId=kitronik_stepper_motor_turn_angle
//% block="stepper turn |%dir|%angle|degrees"
//% angle.min=1 angle.max=360
export function stepperMotorTurnAngle(dir: MotorDirection, angle: number): void {
let angleToSteps = 0;

//convert angle to motor steps, depends on which stepper is being turned to set the number of steps for a full rotation
angleToSteps = pins.map(angle, 1, 360, 1, stepperSteps);

stepperMotorTurnSteps(dir, angleToSteps);
}

/**
* Sets the stepper motor to turn a set number of steps.
* @param dir which direction to go
* @param stepperSteps how many steps to turn the motor
*/
//% blockId=kitronik_motordriver_stepper_motor_turn_steps
//% block="stepper turn |%dir|%steps| steps"
export function stepperMotorTurnSteps(dir: MotorDirection, steps: number): void {
let stepCount = 0;

while (stepCount < steps) {
if (dir == MotorDirection.Forward) {
// Move the stepper forward by 1 step
stepperState++;
} else {
// Move the stepper backward by 1 step
stepperState--;
}

if (stepperState > 3) {
// Wrap stepper state down to 0
stepperState = 0;
}

if (stepperState < 0) {
// Wrap stepper state up to 3
stepperState = 3;
}

if (stepperState % 2 == 0) {
// Turn Motor1 on in the direction set in stepSequence
motorOn(Motors.Motor1, stepSequence[stepperState], 100);
// Set Motor2 to be inactive
motorOff(Motors.Motor2);
} else {
// Turn Motor2 on in the direction set in stepSequence
motorOn(Motors.Motor2, stepSequence[stepperState], 100);
// Set Motor1 to be inactive
motorOff(Motors.Motor1);
}

stepCount++;
basic.pause(50);
}
}

/**
* Set the number of steps per full rotation for the stepper motor.
* stepperSteps is defaulted to 200.
* @param steps number of steps for a full rotation, eg: 200
*/
//% subcategory=Settings
//% group=Settings
//% blockId=kitronik_motordriver_set_stepper_steps
//% block="stepper has |%steps| steps in one full rotation"
//% weight=100 blockGap=8
export function setStepperMotorSteps(steps: number): void {
kitronik_motor_driver.stepperSteps = steps;
}
}
2 changes: 1 addition & 1 deletion pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pxt-kitronik-motor-driver",
"version": "0.0.6",
"version": "0.0.8",
"description": "Blocks to simplify using Kitronik products in PXT",
"license": "MIT",
"dependencies": {
Expand Down

0 comments on commit ceb3812

Please sign in to comment.