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