hello
Dependencies: X_NUCLEO_IHM01A1
Fork of HelloWorld_IHM01A1_2Motors_mbedOS by
main.cpp@32:9fe53ea8430c, 2018-03-28 (annotated)
- Committer:
- gianlucamgf
- Date:
- Wed Mar 28 14:52:12 2018 +0000
- Revision:
- 32:9fe53ea8430c
- Parent:
- 29:526970c1d998
Hello Team this is the botton program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:e6a49a092e2a | 1 | /** |
Davidroid | 0:e6a49a092e2a | 2 | ****************************************************************************** |
Davidroid | 0:e6a49a092e2a | 3 | * @file main.cpp |
Davidroid | 17:aae1446c67f4 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:e6a49a092e2a | 5 | * @version V1.0.0 |
Davidroid | 0:e6a49a092e2a | 6 | * @date October 14th, 2015 |
Davidroid | 17:aae1446c67f4 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1 |
Davidroid | 3:02d9ec4f88b2 | 8 | * Motor Control Expansion Board: control of 2 motors. |
Davidroid | 0:e6a49a092e2a | 9 | ****************************************************************************** |
Davidroid | 0:e6a49a092e2a | 10 | * @attention |
Davidroid | 0:e6a49a092e2a | 11 | * |
Davidroid | 0:e6a49a092e2a | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
Davidroid | 0:e6a49a092e2a | 13 | * |
Davidroid | 0:e6a49a092e2a | 14 | * Redistribution and use in source and binary forms, with or without modification, |
Davidroid | 0:e6a49a092e2a | 15 | * are permitted provided that the following conditions are met: |
Davidroid | 0:e6a49a092e2a | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:e6a49a092e2a | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:e6a49a092e2a | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Davidroid | 0:e6a49a092e2a | 19 | * this list of conditions and the following disclaimer in the documentation |
Davidroid | 0:e6a49a092e2a | 20 | * and/or other materials provided with the distribution. |
Davidroid | 0:e6a49a092e2a | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Davidroid | 0:e6a49a092e2a | 22 | * may be used to endorse or promote products derived from this software |
Davidroid | 0:e6a49a092e2a | 23 | * without specific prior written permission. |
Davidroid | 0:e6a49a092e2a | 24 | * |
Davidroid | 0:e6a49a092e2a | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:e6a49a092e2a | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:e6a49a092e2a | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Davidroid | 0:e6a49a092e2a | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Davidroid | 0:e6a49a092e2a | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Davidroid | 0:e6a49a092e2a | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Davidroid | 0:e6a49a092e2a | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Davidroid | 0:e6a49a092e2a | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Davidroid | 0:e6a49a092e2a | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Davidroid | 0:e6a49a092e2a | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:e6a49a092e2a | 35 | * |
Davidroid | 0:e6a49a092e2a | 36 | ****************************************************************************** |
Davidroid | 0:e6a49a092e2a | 37 | */ |
Davidroid | 0:e6a49a092e2a | 38 | |
Davidroid | 0:e6a49a092e2a | 39 | |
Davidroid | 0:e6a49a092e2a | 40 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 41 | |
Davidroid | 0:e6a49a092e2a | 42 | /* mbed specific header files. */ |
Davidroid | 0:e6a49a092e2a | 43 | #include "mbed.h" |
Davidroid | 0:e6a49a092e2a | 44 | |
Davidroid | 0:e6a49a092e2a | 45 | /* Helper header files. */ |
Davidroid | 0:e6a49a092e2a | 46 | #include "DevSPI.h" |
Davidroid | 0:e6a49a092e2a | 47 | |
Davidroid | 0:e6a49a092e2a | 48 | /* Component specific header files. */ |
Davidroid | 29:526970c1d998 | 49 | #include "L6474.h" |
Davidroid | 0:e6a49a092e2a | 50 | |
Davidroid | 0:e6a49a092e2a | 51 | |
Davidroid | 0:e6a49a092e2a | 52 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 53 | |
Davidroid | 24:8cb3c4ad055f | 54 | /* Number of steps. */ |
gianlucamgf | 32:9fe53ea8430c | 55 | #define STEPS 100 |
Davidroid | 0:e6a49a092e2a | 56 | |
Davidroid | 24:8cb3c4ad055f | 57 | /* Delay in milliseconds. */ |
gianlucamgf | 32:9fe53ea8430c | 58 | #define DELAY_1 100 |
gianlucamgf | 32:9fe53ea8430c | 59 | #define DELAY_2 3000 |
gianlucamgf | 32:9fe53ea8430c | 60 | #define DELAY_3 4000 |
Davidroid | 24:8cb3c4ad055f | 61 | |
Davidroid | 24:8cb3c4ad055f | 62 | /* Speed in pps (Pulses Per Second). |
Davidroid | 24:8cb3c4ad055f | 63 | In Full Step mode: 1 pps = 1 step/s). |
Davidroid | 24:8cb3c4ad055f | 64 | In 1/N Step Mode: N pps = 1 step/s). */ |
Davidroid | 24:8cb3c4ad055f | 65 | #define SPEED_1 2400 |
Davidroid | 24:8cb3c4ad055f | 66 | #define SPEED_2 1200 |
Davidroid | 24:8cb3c4ad055f | 67 | |
Davidroid | 0:e6a49a092e2a | 68 | |
Davidroid | 0:e6a49a092e2a | 69 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 70 | |
gianlucamgf | 32:9fe53ea8430c | 71 | DigitalIn mybutton(USER_BUTTON); |
gianlucamgf | 32:9fe53ea8430c | 72 | bool x=true; |
Davidroid | 0:e6a49a092e2a | 73 | /* Motor Control Component. */ |
Davidroid | 3:02d9ec4f88b2 | 74 | L6474 *motor1; |
Davidroid | 3:02d9ec4f88b2 | 75 | L6474 *motor2; |
Davidroid | 0:e6a49a092e2a | 76 | |
Davidroid | 0:e6a49a092e2a | 77 | |
Davidroid | 0:e6a49a092e2a | 78 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:e6a49a092e2a | 79 | |
Davidroid | 0:e6a49a092e2a | 80 | int main() |
Davidroid | 0:e6a49a092e2a | 81 | { |
Davidroid | 8:cec4c2c03a27 | 82 | /*----- Initialization. -----*/ |
Davidroid | 8:cec4c2c03a27 | 83 | |
Davidroid | 0:e6a49a092e2a | 84 | /* Initializing SPI bus. */ |
Davidroid | 0:e6a49a092e2a | 85 | DevSPI dev_spi(D11, D12, D13); |
Davidroid | 0:e6a49a092e2a | 86 | |
Davidroid | 9:a9e51320aee4 | 87 | /* Initializing Motor Control Components. */ |
Davidroid | 5:a0268a435bb1 | 88 | motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi); |
Davidroid | 16:810667a9f31f | 89 | motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi); |
Davidroid | 29:526970c1d998 | 90 | if (motor1->init() != COMPONENT_OK) { |
Davidroid | 14:fcd452b03d1a | 91 | exit(EXIT_FAILURE); |
Davidroid | 28:3f17a4152bcf | 92 | } |
Davidroid | 29:526970c1d998 | 93 | if (motor2->init() != COMPONENT_OK) { |
Davidroid | 14:fcd452b03d1a | 94 | exit(EXIT_FAILURE); |
Davidroid | 28:3f17a4152bcf | 95 | } |
Davidroid | 0:e6a49a092e2a | 96 | |
Davidroid | 0:e6a49a092e2a | 97 | /* Printing to the console. */ |
Davidroid | 3:02d9ec4f88b2 | 98 | printf("Motor Control Application Example for 2 Motors\r\n\n"); |
Davidroid | 0:e6a49a092e2a | 99 | |
gianlucamgf | 32:9fe53ea8430c | 100 | /*----- Moving F. -----*/ |
gianlucamgf | 32:9fe53ea8430c | 101 | while(true) { |
gianlucamgf | 32:9fe53ea8430c | 102 | if (mybutton == 0) // Button is pressed |
gianlucamgf | 32:9fe53ea8430c | 103 | { |
gianlucamgf | 32:9fe53ea8430c | 104 | x=!x; |
gianlucamgf | 32:9fe53ea8430c | 105 | printf("x =%d", x); |
gianlucamgf | 32:9fe53ea8430c | 106 | } |
gianlucamgf | 32:9fe53ea8430c | 107 | if (x==true) // Button is pressed |
gianlucamgf | 32:9fe53ea8430c | 108 | { |
gianlucamgf | 32:9fe53ea8430c | 109 | /* Printing to the console. */ |
gianlucamgf | 32:9fe53ea8430c | 110 | printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS); |
Davidroid | 8:cec4c2c03a27 | 111 | |
gianlucamgf | 32:9fe53ea8430c | 112 | /* Moving N steps in the forward direction. */ |
gianlucamgf | 32:9fe53ea8430c | 113 | motor1->move(StepperMotor::FWD, STEPS >> 1); |
gianlucamgf | 32:9fe53ea8430c | 114 | motor2->move(StepperMotor::FWD, STEPS >> 1); |
Davidroid | 8:cec4c2c03a27 | 115 | |
gianlucamgf | 32:9fe53ea8430c | 116 | /* Waiting while the motor is active. */ |
gianlucamgf | 32:9fe53ea8430c | 117 | motor1->wait_while_active(); |
gianlucamgf | 32:9fe53ea8430c | 118 | motor2->wait_while_active(); |
gianlucamgf | 32:9fe53ea8430c | 119 | |
gianlucamgf | 32:9fe53ea8430c | 120 | /* Getting current position. */ |
gianlucamgf | 32:9fe53ea8430c | 121 | int position1 = motor1->get_position(); |
gianlucamgf | 32:9fe53ea8430c | 122 | int position2 = motor2->get_position(); |
Davidroid | 8:cec4c2c03a27 | 123 | |
gianlucamgf | 32:9fe53ea8430c | 124 | /* Printing to the console. */ |
gianlucamgf | 32:9fe53ea8430c | 125 | printf(" Position: M1 %d, M2 %d.\r\n", position1, position2); |
Davidroid | 8:cec4c2c03a27 | 126 | |
gianlucamgf | 32:9fe53ea8430c | 127 | /* Waiting 2 seconds. */ |
gianlucamgf | 32:9fe53ea8430c | 128 | wait_ms(DELAY_1); |
gianlucamgf | 32:9fe53ea8430c | 129 | } |
gianlucamgf | 32:9fe53ea8430c | 130 | if (x==false) // Button is pressed |
gianlucamgf | 32:9fe53ea8430c | 131 | { |
gianlucamgf | 32:9fe53ea8430c | 132 | /*----- Moving B. -----*/ |
Davidroid | 8:cec4c2c03a27 | 133 | |
gianlucamgf | 32:9fe53ea8430c | 134 | /* Printing to the console. */ |
gianlucamgf | 32:9fe53ea8430c | 135 | |
gianlucamgf | 32:9fe53ea8430c | 136 | printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", STEPS >> 1, STEPS); |
Davidroid | 8:cec4c2c03a27 | 137 | |
Davidroid | 8:cec4c2c03a27 | 138 | |
gianlucamgf | 32:9fe53ea8430c | 139 | /* Moving N steps in the backward direction. */ |
gianlucamgf | 32:9fe53ea8430c | 140 | motor1->move(StepperMotor::BWD, STEPS >> 1); |
gianlucamgf | 32:9fe53ea8430c | 141 | motor2->move(StepperMotor::BWD, STEPS >> 1); |
Davidroid | 8:cec4c2c03a27 | 142 | |
gianlucamgf | 32:9fe53ea8430c | 143 | /* Waiting while the motor is active. */ |
gianlucamgf | 32:9fe53ea8430c | 144 | motor1->wait_while_active(); |
gianlucamgf | 32:9fe53ea8430c | 145 | motor2->wait_while_active(); |
Davidroid | 8:cec4c2c03a27 | 146 | |
gianlucamgf | 32:9fe53ea8430c | 147 | /* Getting current position. */ |
gianlucamgf | 32:9fe53ea8430c | 148 | int position1 = motor1->get_position(); |
gianlucamgf | 32:9fe53ea8430c | 149 | int position2 = motor2->get_position(); |
Davidroid | 8:cec4c2c03a27 | 150 | |
gianlucamgf | 32:9fe53ea8430c | 151 | /* Printing to the console. */ |
gianlucamgf | 32:9fe53ea8430c | 152 | printf(" Position: M1 %d, M2 %d.\r\n", position1, position2); |
gianlucamgf | 32:9fe53ea8430c | 153 | printf("--> Setting Home.\r\n"); |
Davidroid | 8:cec4c2c03a27 | 154 | |
Davidroid | 8:cec4c2c03a27 | 155 | |
gianlucamgf | 32:9fe53ea8430c | 156 | /* Waiting 2 seconds. */ |
gianlucamgf | 32:9fe53ea8430c | 157 | wait_ms(DELAY_1); |
gianlucamgf | 32:9fe53ea8430c | 158 | } |
Davidroid | 0:e6a49a092e2a | 159 | } |
Davidroid | 0:e6a49a092e2a | 160 | } |