Threaded version
stepper.cpp@3:e98454afa038, 2019-08-28 (annotated)
- Committer:
- ibiltari
- Date:
- Wed Aug 28 19:02:39 2019 +0000
- Revision:
- 3:e98454afa038
- Parent:
- 2:ad11f550b379
Treads init;
Who changed what in which revision?
User | Revision | Line number | New 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 |
ibiltari | 3:e98454afa038 | 26 | #define START_STOP_SPEED 500 |
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 | |
ibiltari | 3:e98454afa038 | 33 | stepper::stepper(PinName clk, PinName dir) : _clk(clk), _dir(dir), s_thread(osPriorityNormal) { |
franzachatz | 0:27b548e34acc | 34 | _clk = 0, _dir = 0; |
ibiltari | 3:e98454afa038 | 35 | s_thread.start(this, &stepper::run); |
franzachatz | 0:27b548e34acc | 36 | } |
franzachatz | 0:27b548e34acc | 37 | |
franzachatz | 0:27b548e34acc | 38 | void stepper::step(int n_steps, bool direction, int speed, bool accel) { |
ibiltari | 3:e98454afa038 | 39 | if(accel) s_accelspeed = START_STOP_SPEED; |
ibiltari | 3:e98454afa038 | 40 | else s_accelspeed = speed; |
ibiltari | 3:e98454afa038 | 41 | s_speed = speed; |
ibiltari | 3:e98454afa038 | 42 | s_direction = direction; |
ibiltari | 3:e98454afa038 | 43 | s_accel = accel; |
ibiltari | 3:e98454afa038 | 44 | s_n_steps = n_steps; |
ibiltari | 3:e98454afa038 | 45 | |
ibiltari | 3:e98454afa038 | 46 | } |
ibiltari | 3:e98454afa038 | 47 | void stepper::run() { |
ibiltari | 3:e98454afa038 | 48 | printf("stepper thread running"); |
ibiltari | 3:e98454afa038 | 49 | while (true) { |
ibiltari | 3:e98454afa038 | 50 | if (s_n_steps != 0){ |
ibiltari | 3:e98454afa038 | 51 | for(int i=0; i<s_n_steps; i++) |
ibiltari | 3:e98454afa038 | 52 | { |
ibiltari | 3:e98454afa038 | 53 | if(s_accel)//linear acceleration |
ibiltari | 3:e98454afa038 | 54 | { |
ibiltari | 3:e98454afa038 | 55 | if(i < START_STOP_SPEED) if (--s_accelspeed == s_speed) s_accelspeed ++; |
ibiltari | 3:e98454afa038 | 56 | if(i > (s_n_steps-START_STOP_SPEED)) if(++s_accelspeed == START_STOP_SPEED) s_accelspeed--; |
ibiltari | 3:e98454afa038 | 57 | } |
ibiltari | 3:e98454afa038 | 58 | _dir = s_direction; |
ibiltari | 3:e98454afa038 | 59 | _clk = 1; |
ibiltari | 3:e98454afa038 | 60 | wait_us(1); |
ibiltari | 3:e98454afa038 | 61 | _clk = 0; |
ibiltari | 3:e98454afa038 | 62 | wait_us(1); |
ibiltari | 3:e98454afa038 | 63 | wait_us(s_accelspeed); |
ibiltari | 3:e98454afa038 | 64 | } |
ibiltari | 3:e98454afa038 | 65 | s_n_steps = 0; |
ibiltari | 3:e98454afa038 | 66 | } |
ibiltari | 3:e98454afa038 | 67 | |
ibiltari | 3:e98454afa038 | 68 | |
ibiltari | 3:e98454afa038 | 69 | ThisThread::sleep_for(10); |
franzachatz | 0:27b548e34acc | 70 | } |
ibiltari | 3:e98454afa038 | 71 | |
franzachatz | 0:27b548e34acc | 72 | } |
franzachatz | 0:27b548e34acc | 73 | |
franzachatz | 0:27b548e34acc | 74 | // version() returns the version number of the library |
franzachatz | 0:27b548e34acc | 75 | float stepper::version(void) { |
franzachatz | 0:27b548e34acc | 76 | return VERSION; |
ibiltari | 3:e98454afa038 | 77 | } |