Pecunia_Insper / Mbed OS V0_Button_two_Buttonfff

Dependencies:   X_NUCLEO_IHM01A1

Fork of Button_two_Buttonfff by Pecunia_Insper

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    October 14th, 2015
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
00008  *          Motor Control Expansion Board: control of 2 motors.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* mbed specific header files. */
00043 #include "mbed.h"
00044 
00045 /* Helper header files. */
00046 #include "DevSPI.h"
00047 
00048 /* Component specific header files. */
00049 #include "L6474.h"
00050 
00051 
00052 /* Definitions ---------------------------------------------------------------*/
00053 
00054 /* Number of steps. */
00055 #define STEPS 100
00056 
00057 /* Delay in milliseconds. */
00058 #define DELAY_1 100
00059 #define DELAY_2 3000
00060 #define DELAY_3 4000
00061 
00062 /* Speed in pps (Pulses Per Second).
00063    In Full Step mode: 1 pps = 1 step/s).
00064    In 1/N Step Mode:  N pps = 1 step/s). */
00065 #define SPEED_1 2400
00066 #define SPEED_2 1200
00067 
00068 
00069 /* Variables -----------------------------------------------------------------*/
00070 
00071 DigitalIn mybutton(USER_BUTTON);
00072 bool x=true;
00073 /* Motor Control Component. */
00074 L6474 *motor1;
00075 L6474 *motor2;
00076 
00077 
00078 /* Main ----------------------------------------------------------------------*/
00079 
00080 int main()
00081 {
00082     /*----- Initialization. -----*/
00083 
00084     /* Initializing SPI bus. */
00085     DevSPI dev_spi(D11, D12, D13);
00086 
00087     /* Initializing Motor Control Components. */
00088     motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
00089     motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
00090     if (motor1->init() != COMPONENT_OK) {
00091         exit(EXIT_FAILURE);
00092     }
00093     if (motor2->init() != COMPONENT_OK) {
00094         exit(EXIT_FAILURE);
00095     }
00096 
00097     /* Printing to the console. */
00098     printf("Motor Control Application Example for 2 Motors\r\n\n");
00099 
00100     /*----- Moving F. -----*/
00101     while(true) {
00102         if (mybutton == 0) // Button is pressed
00103                 {
00104                  x=!x;
00105                  printf("x =%d", x);
00106                 }
00107         if (x==true) // Button is pressed
00108             {
00109             /* Printing to the console. */
00110             printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
00111 
00112             /* Moving N steps in the forward direction. */
00113             motor1->move(StepperMotor::FWD, STEPS >> 1);
00114             motor2->move(StepperMotor::FWD, STEPS >> 1);
00115 
00116             /* Waiting while the motor is active. */
00117             motor1->wait_while_active();
00118             motor2->wait_while_active();
00119 
00120             /* Getting current position. */
00121             int position1 = motor1->get_position();
00122             int position2 = motor2->get_position();
00123     
00124             /* Printing to the console. */
00125             printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00126 
00127             /* Waiting 2 seconds. */
00128             wait_ms(DELAY_1);
00129             }
00130         if (x==false) // Button is pressed
00131             {
00132             /*----- Moving B. -----*/
00133     
00134             /* Printing to the console. */
00135 
00136             printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS);
00137 
00138     
00139             /* Moving N steps in the backward direction. */
00140             motor1->move(StepperMotor::BWD, STEPS >> 1);
00141             motor2->move(StepperMotor::BWD, STEPS >> 1);
00142     
00143             /* Waiting while the motor is active. */
00144             motor1->wait_while_active();
00145             motor2->wait_while_active();
00146 
00147             /* Getting current position. */
00148             int position1 = motor1->get_position();
00149             int position2 = motor2->get_position();
00150     
00151             /* Printing to the console. */
00152             printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
00153             printf("--> Setting Home.\r\n");
00154 
00155 
00156             /* Waiting 2 seconds. */
00157             wait_ms(DELAY_1);
00158             }
00159     }
00160 }
00161