Kenji Arai / Mbed 2 deprecated NucleoL152_stepper_w_lib

Dependencies:   mbed stepper

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Application program for the mbed ST Nucleo L152RE & L401RE
00003  *      Stepping Motor
00004  *
00005  * Copyright (c) 2014 Kenji Arai / JH1PJL
00006  *  http://www.page.sannet.ne.jp/kenjia/index.html
00007  *  http://mbed.org/users/kenjiArai/
00008  *      Created: July      12th, 2014
00009  *      Revised: August    24th, 2014
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00012  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00013  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00014  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00015  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016  */
00017 //
00018 //  Stepping Motor SPG27-1702 COPAL ELECTRONICS
00019 //      http://akizukidenshi.com/catalog/g/gP-05708/
00020 //  Drive voltage: 5V       Driver IC: TD62003AP
00021 //  
00022 //  CAUTION: This is only for Unipolar Type Stepping Motor!
00023 //           Cannot use for Bipolar Type
00024 //      Plese refer http://en.wikipedia.org/wiki/Stepper_motor
00025 //
00026 
00027 //  Include ---------------------------------------------------------------------------------------
00028 #include    "mbed.h"
00029 #include    "stepper.h"
00030 
00031 //  Object ----------------------------------------------------------------------------------------
00032 #if defined(TARGET_LPC1114)
00033 Serial      pc(dp16,dp15);      // Communication with Host
00034 STEPPER     sm_r(dp1,  dp2, dp4, dp6);
00035 STEPPER     sm_l(dp17,dp18,dp25,dp26);
00036 #elif defined(TARGET_NUCLEO_L152RE) || defined(TARGET_STM32F401RE)
00037 Serial      pc(SERIAL_TX, SERIAL_RX);  // Communication with Host
00038 STEPPER     sm_r(D5, D4, D3, D2);
00039 STEPPER     sm_l(D9, D8, D7, D6);
00040 #endif
00041 
00042 //  Definition ------------------------------------------------------------------------------------
00043 #define TIMEBASE                18000
00044 
00045 #define FIXED_STEPS             480
00046 
00047 #define BAUD(x)                 pc.baud(x)
00048 #define GETC(x)                 pc.getc(x)
00049 #define PUTC(x)                 pc.putc(x)
00050 #define PRINTF(...)             pc.printf(__VA_ARGS__)
00051 #define READABLE(x)             pc.readable(x)
00052 
00053 //  RAM -------------------------------------------------------------------------------------------
00054 char lb[32];
00055 
00056 uint8_t pls_width[MT_SLOP_STEP] = {5, 4, 3, 2, 1, 1, 1, 1, 1, 1 };
00057 
00058 //  ROM / Constant data ---------------------------------------------------------------------------
00059 static char *const msg0 = "\r\nStepping Motor Control by JH1PJL, created on "__DATE__"\r\n";
00060 
00061 //  Function prototypes ---------------------------------------------------------------------------
00062 void help(void);
00063 void get_line (char *buff, int len);
00064 int xatoi (char **str, int32_t *res);
00065 
00066 //-------------------------------------------------------------------------------------------------
00067 //  Control Program
00068 //-------------------------------------------------------------------------------------------------
00069 // Main control -----------------------------------------------------------------------------------
00070 int main() {
00071 int32_t p1;
00072 uint8_t cnt = 0;
00073 char *ptr;
00074 char c;
00075 
00076     sm_r.set_max_speed(TIMEBASE);
00077     sm_l.set_max_speed(TIMEBASE);
00078     PRINTF(msg0);
00079     for (;;) {
00080         PUTC('>');
00081         ptr = lb;
00082         get_line(ptr, sizeof(lb));
00083         switch (*ptr++){
00084             case 'b' :
00085                 if (xatoi(&ptr, &p1)) {
00086                     sm_r.move(p1);
00087                     sm_l.move(p1);
00088                 } 
00089                 PRINTF("\rR turn: %\+-d\r\n", p1);
00090                 PRINTF("L turn: %\+-d\r\n", p1);
00091                 break;
00092             case 't' :
00093                 if (xatoi(&ptr, &p1)) {
00094                     sm_r.move(p1);
00095                     sm_l.move(-p1);
00096                 }
00097                 PRINTF("\rR turn: %\+-d\r\n", p1);
00098                 PRINTF("L turn: %\+-d\r\n", -p1);
00099                 break;
00100             case 'r' :
00101                 if (xatoi(&ptr, &p1)) {
00102                     sm_r.move(p1);
00103                 }
00104                 PRINTF("\rR turn: %\+-d\r\n", p1);
00105                 break;
00106             case 'l' :
00107                 if (xatoi(&ptr, &p1)) {
00108                     sm_l.move(p1);
00109                 }
00110                 PRINTF("\rL turn: %\+-d\r\n", p1);
00111                 break;
00112             case 's' :
00113                 PRINTF("\rR ");
00114                 if (sm_r.status()){
00115                     PRINTF("running");
00116                 } else {
00117                     PRINTF("stooped");
00118                 }
00119                 PRINTF("\r\nL ");
00120                 if (sm_l.status()){
00121                     PRINTF("running");
00122                 } else {
00123                     PRINTF("stopped");
00124                 }
00125                 PRINTF("\r\n");
00126                 break;
00127             case 0x0d :
00128                 if (cnt & 1){       // cnt = 1 or 3
00129                     if (cnt == 3){  // cnt = 3
00130                         sm_l.move(-FIXED_STEPS);
00131                         c = '-';
00132                         cnt = 0;
00133                     } else {        // cnt = 1
00134                         sm_l.move(FIXED_STEPS);
00135                         c = '+';
00136                         cnt++;
00137                     }
00138                     PRINTF("\rL turn: %c%d\r\n", c, FIXED_STEPS);
00139                 } else {            // cnt = 0 or 2
00140                     if (cnt == 2){  // cnt = 2
00141                         sm_r.move(-FIXED_STEPS);
00142                         c = '-';
00143                     } else {        // cnt = 0
00144                         sm_r.move(FIXED_STEPS);
00145                         c = '+';
00146                     }
00147                     cnt++;
00148                     PRINTF("\rR turn: %c%d\r\n", c, FIXED_STEPS);
00149                 }
00150                 break;         
00151             case '?' :
00152                 help();
00153                 break;
00154             default:
00155                 PRINTF("\r?\r\n");
00156                 help();
00157         }
00158     }
00159 }
00160 
00161 void help(void){
00162     PRINTF("\rRight motor:  r +/-step  e.g. r +100\r\n");
00163     PRINTF("Left  motor:  l +/-step  e.g. l -100\r\n");
00164     PRINTF("Turn  motors: t +/-step  e.g. t  100\r\n");
00165     PRINTF("Both  motors: b +/-step  e.g. b -100\r\n");
00166     PRINTF("Check Status: s\r\n");
00167     PRINTF("fixed data & reverse hit <Enter> key\r\n");
00168 }
00169 
00170 // Com line control -------------------------------------------------------------------------------
00171 //  Change string -> number
00172 int xatoi (char **str, int32_t *res){
00173 unsigned long val;
00174 unsigned char c, radix, s = 0;
00175 
00176     while ((c = **str) == ' '){ (*str)++;}
00177     if (c == '-') {
00178         s = 1;
00179         c = *(++(*str));
00180     } else if (c == '+') {
00181         s = 0;
00182         c = *(++(*str));
00183     }
00184     if (c == '0') {
00185         c = *(++(*str));
00186         if (c <= ' ') { *res = 0;   return 1; }
00187         if (c == 'x') {
00188             radix = 16;
00189             c = *(++(*str));
00190         } else {
00191             if (c == 'b') {
00192                 radix = 2;
00193                 c = *(++(*str));
00194             } else {
00195                 if ((c >= '0')&&(c <= '9')){ radix = 8;
00196                 } else { return 0;}
00197             }
00198         }
00199     } else {
00200         if ((c < '1')||(c > '9')){  return 0;}
00201         radix = 10;
00202     }
00203     val = 0;
00204     while (c > ' ') {
00205         if (c >= 'a'){ c -= 0x20;}
00206         c -= '0';
00207         if (c >= 17){
00208             c -= 7;
00209             if (c <= 9){ return 0;}
00210         }
00211         if (c >= radix){ return 0;}
00212         val = val * radix + c;
00213         c = *(++(*str));
00214     }
00215     if (s){ val = -val;}
00216     *res = val;
00217     return 1;
00218 }
00219 
00220 //  Get key input data
00221 void get_line (char *buff, int len){
00222 char c;
00223 int idx = 0;
00224 
00225     for (;;) {
00226         c = GETC();
00227         if (c == '\r') {
00228             buff[idx++] = c;
00229             break;
00230         }
00231         if ((c == '\b') && idx) {
00232             idx--;
00233             PUTC(c);
00234             PUTC(' ');
00235             PUTC(c);
00236         }
00237         if (((uint8_t)c >= ' ') && (idx < len - 1)) {
00238             buff[idx++] = c;
00239             PUTC(c);
00240         }
00241     }
00242     buff[idx] = 0;
00243     PUTC('\n');
00244 }