simple library for applying number of steps, direction, speed and a linear acceleration-deceleration to a Stepper Motor Controller

Dependents:   16A_Autopancakemaker

Committer:
franzachatz
Date:
Wed Nov 03 18:41:28 2010 +0000
Revision:
2:ad11f550b379
Parent:
1:ef0414c3ce83
added some doc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
franzachatz 0:27b548e34acc 1 /* mbed Stepper Library
franzachatz 0:27b548e34acc 2 * Copyright (c) 2010 fachatz
franzachatz 0:27b548e34acc 3 *
franzachatz 0:27b548e34acc 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
franzachatz 0:27b548e34acc 5 * of this software and associated documentation files (the "Software"), to deal
franzachatz 0:27b548e34acc 6 * in the Software without restriction, including without limitation the rights
franzachatz 0:27b548e34acc 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
franzachatz 0:27b548e34acc 8 * copies of the Software, and to permit persons to whom the Software is
franzachatz 0:27b548e34acc 9 * furnished to do so, subject to the following conditions:
franzachatz 0:27b548e34acc 10 *
franzachatz 0:27b548e34acc 11 * The above copyright notice and this permission notice shall be included in
franzachatz 0:27b548e34acc 12 * all copies or substantial portions of the Software.
franzachatz 0:27b548e34acc 13 *
franzachatz 0:27b548e34acc 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
franzachatz 0:27b548e34acc 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
franzachatz 0:27b548e34acc 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
franzachatz 0:27b548e34acc 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
franzachatz 0:27b548e34acc 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
franzachatz 0:27b548e34acc 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
franzachatz 0:27b548e34acc 20 * THE SOFTWARE.
franzachatz 0:27b548e34acc 21 */
franzachatz 0:27b548e34acc 22 #include "stepper.h"
franzachatz 0:27b548e34acc 23 #include "mbed.h"
franzachatz 0:27b548e34acc 24
franzachatz 0:27b548e34acc 25 // define the Stepper Motor save start/stop speed
franzachatz 0:27b548e34acc 26 #define START_STOP_SPEED 300
franzachatz 0:27b548e34acc 27
franzachatz 0:27b548e34acc 28 // define Library version number
franzachatz 2:ad11f550b379 29 #define VERSION 0.3
franzachatz 0:27b548e34acc 30
franzachatz 0:27b548e34acc 31 // led4, used for testing the direction signal
franzachatz 0:27b548e34acc 32 DigitalOut led4(LED4);
franzachatz 0:27b548e34acc 33
franzachatz 0:27b548e34acc 34 stepper::stepper(PinName clk, PinName dir) : _clk(clk), _dir(dir) {
franzachatz 0:27b548e34acc 35 _clk = 0, _dir = 0;
franzachatz 0:27b548e34acc 36 }
franzachatz 0:27b548e34acc 37
franzachatz 0:27b548e34acc 38 void stepper::step(int n_steps, bool direction, int speed, bool accel) {
franzachatz 0:27b548e34acc 39 int accelspeed;
franzachatz 0:27b548e34acc 40 if(accel) accelspeed = START_STOP_SPEED;
franzachatz 0:27b548e34acc 41 else accelspeed = speed;
franzachatz 0:27b548e34acc 42 for(int i=0; i<n_steps; i++)
franzachatz 0:27b548e34acc 43 {
franzachatz 0:27b548e34acc 44 if(accel)//linear acceleration
franzachatz 0:27b548e34acc 45 {
franzachatz 0:27b548e34acc 46 if(i < START_STOP_SPEED) if (--accelspeed == speed) accelspeed ++;
franzachatz 0:27b548e34acc 47 if(i > (n_steps-START_STOP_SPEED)) if(++accelspeed == START_STOP_SPEED) accelspeed--;
franzachatz 0:27b548e34acc 48 }
franzachatz 0:27b548e34acc 49 _dir = led4 = direction;
franzachatz 0:27b548e34acc 50 _clk = 1;
franzachatz 0:27b548e34acc 51 wait_us(1);
franzachatz 0:27b548e34acc 52 _clk = 0;
franzachatz 0:27b548e34acc 53 wait_us(1);
franzachatz 0:27b548e34acc 54 wait_us(accelspeed);
franzachatz 0:27b548e34acc 55 }
franzachatz 0:27b548e34acc 56 }
franzachatz 0:27b548e34acc 57
franzachatz 0:27b548e34acc 58 // version() returns the version number of the library
franzachatz 0:27b548e34acc 59 float stepper::version(void) {
franzachatz 0:27b548e34acc 60 return VERSION;
franzachatz 0:27b548e34acc 61 }