Signori, questo è il codice di doukietown...... divertitevi
Dependencies: HCSR04 TCS3200 X_NUCLEO_IHM12A1 mbed
Fork of ovvioBug by
main.cpp@7:b0ba8a7b6e9f, 2017-04-05 (annotated)
- Committer:
- LorenzoCR
- Date:
- Wed Apr 05 08:36:05 2017 +0000
- Revision:
- 7:b0ba8a7b6e9f
- Parent:
- 6:bd2fa888c101
hallo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Manu_L | 0:773e2a2be16f | 1 | /** |
Manu_L | 0:773e2a2be16f | 2 | ****************************************************************************** |
Manu_L | 0:773e2a2be16f | 3 | * @file main.cpp |
Manu_L | 0:773e2a2be16f | 4 | * @author IPC Rennes |
Manu_L | 0:773e2a2be16f | 5 | * @version V1.0.0 |
Manu_L | 0:773e2a2be16f | 6 | * @date April 25th, 2016 |
Manu_L | 0:773e2a2be16f | 7 | * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM12A1 |
Manu_L | 0:773e2a2be16f | 8 | * Motor Control Expansion Board: control of 2 Brush DC motors. |
Manu_L | 0:773e2a2be16f | 9 | ****************************************************************************** |
Manu_L | 0:773e2a2be16f | 10 | * @attention |
Manu_L | 0:773e2a2be16f | 11 | * |
Manu_L | 0:773e2a2be16f | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
Manu_L | 0:773e2a2be16f | 13 | * |
Manu_L | 0:773e2a2be16f | 14 | * Redistribution and use in source and binary forms, with or without modification, |
Manu_L | 0:773e2a2be16f | 15 | * are permitted provided that the following conditions are met: |
Manu_L | 0:773e2a2be16f | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Manu_L | 0:773e2a2be16f | 17 | * this list of conditions and the following disclaimer. |
Manu_L | 0:773e2a2be16f | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Manu_L | 0:773e2a2be16f | 19 | * this list of conditions and the following disclaimer in the documentation |
Manu_L | 0:773e2a2be16f | 20 | * and/or other materials provided with the distribution. |
Manu_L | 0:773e2a2be16f | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Manu_L | 0:773e2a2be16f | 22 | * may be used to endorse or promote products derived from this software |
Manu_L | 0:773e2a2be16f | 23 | * without specific prior written permission. |
Manu_L | 0:773e2a2be16f | 24 | * |
Manu_L | 0:773e2a2be16f | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Manu_L | 0:773e2a2be16f | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Manu_L | 0:773e2a2be16f | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Manu_L | 0:773e2a2be16f | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Manu_L | 0:773e2a2be16f | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Manu_L | 0:773e2a2be16f | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Manu_L | 0:773e2a2be16f | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Manu_L | 0:773e2a2be16f | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Manu_L | 0:773e2a2be16f | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Manu_L | 0:773e2a2be16f | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Manu_L | 0:773e2a2be16f | 35 | * |
Manu_L | 0:773e2a2be16f | 36 | ****************************************************************************** |
Manu_L | 0:773e2a2be16f | 37 | */ |
Manu_L | 0:773e2a2be16f | 38 | |
Manu_L | 0:773e2a2be16f | 39 | /* Includes ------------------------------------------------------------------*/ |
Manu_L | 0:773e2a2be16f | 40 | |
Manu_L | 0:773e2a2be16f | 41 | /* mbed specific header files. */ |
Manu_L | 0:773e2a2be16f | 42 | #include "mbed.h" |
LorenzoCR | 7:b0ba8a7b6e9f | 43 | #include "color.h" |
LorenzoCR | 7:b0ba8a7b6e9f | 44 | #include "hcsr04.h" |
LorenzoCR | 7:b0ba8a7b6e9f | 45 | #include "BDCMotor.h" |
Manu_L | 0:773e2a2be16f | 46 | |
Manu_L | 0:773e2a2be16f | 47 | /* Component specific header files. */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 48 | #include "STSpin240_250.h" |
LorenzoCR | 7:b0ba8a7b6e9f | 49 | HCSR04 sensor1(D10, D11); |
LorenzoCR | 7:b0ba8a7b6e9f | 50 | |
LorenzoCR | 7:b0ba8a7b6e9f | 51 | ColorSensor color(D4, D5, D6, D7, D8); |
Manu_L | 0:773e2a2be16f | 52 | |
Manu_L | 0:773e2a2be16f | 53 | /* Variables -----------------------------------------------------------------*/ |
Manu_L | 0:773e2a2be16f | 54 | |
Manu_L | 0:773e2a2be16f | 55 | /* Initialization parameters of the motor connected to the expansion board. */ |
Davidroid | 6:bd2fa888c101 | 56 | STSpin240_250_init_t init = |
Manu_L | 0:773e2a2be16f | 57 | { |
Manu_L | 0:773e2a2be16f | 58 | 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */ |
Manu_L | 0:773e2a2be16f | 59 | 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */ |
Manu_L | 0:773e2a2be16f | 60 | 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */ |
Manu_L | 0:773e2a2be16f | 61 | 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */ |
Manu_L | 0:773e2a2be16f | 62 | TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */ |
Manu_L | 0:773e2a2be16f | 63 | }; |
Manu_L | 0:773e2a2be16f | 64 | |
Manu_L | 0:773e2a2be16f | 65 | /* Motor Control Component. */ |
Davidroid | 6:bd2fa888c101 | 66 | STSpin240_250 *motor; |
Manu_L | 0:773e2a2be16f | 67 | |
Manu_L | 0:773e2a2be16f | 68 | /* Functions -----------------------------------------------------------------*/ |
Manu_L | 0:773e2a2be16f | 69 | |
Manu_L | 0:773e2a2be16f | 70 | /** |
Manu_L | 0:773e2a2be16f | 71 | * @brief This is an example of error handler. |
Manu_L | 0:773e2a2be16f | 72 | * @param[in] error Number of the error |
Manu_L | 0:773e2a2be16f | 73 | * @retval None |
Manu_L | 0:773e2a2be16f | 74 | * @note If needed, implement it, and then attach it: |
davide.aliprandi@st.com | 5:7517162fb2b9 | 75 | * + motor->attach_error_handler(&my_error_handler); |
Manu_L | 0:773e2a2be16f | 76 | */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 77 | void my_error_handler(uint16_t error) |
Manu_L | 0:773e2a2be16f | 78 | { |
Manu_L | 0:773e2a2be16f | 79 | /* Printing to the console. */ |
Manu_L | 0:773e2a2be16f | 80 | printf("Error %d detected\r\n\n", error); |
Manu_L | 0:773e2a2be16f | 81 | |
Manu_L | 0:773e2a2be16f | 82 | /* Infinite loop */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 83 | while (true) { |
Manu_L | 0:773e2a2be16f | 84 | } |
Manu_L | 0:773e2a2be16f | 85 | } |
Manu_L | 0:773e2a2be16f | 86 | |
Manu_L | 0:773e2a2be16f | 87 | /** |
Manu_L | 0:773e2a2be16f | 88 | * @brief This is an example of user handler for the flag interrupt. |
Manu_L | 0:773e2a2be16f | 89 | * @param None |
Manu_L | 0:773e2a2be16f | 90 | * @retval None |
Manu_L | 0:773e2a2be16f | 91 | * @note If needed, implement it, and then attach and enable it: |
davide.aliprandi@st.com | 5:7517162fb2b9 | 92 | * + motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:7517162fb2b9 | 93 | * + motor->enable_flag_irq(); |
Manu_L | 0:773e2a2be16f | 94 | * To disable it: |
Manu_L | 0:773e2a2be16f | 95 | * + motor->DisbleFlagIRQ(); |
Manu_L | 0:773e2a2be16f | 96 | */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 97 | void my_flag_irq_handler(void) |
Manu_L | 0:773e2a2be16f | 98 | { |
Manu_L | 0:773e2a2be16f | 99 | /* Code to be customised */ |
Manu_L | 0:773e2a2be16f | 100 | /************************/ |
Manu_L | 0:773e2a2be16f | 101 | |
Manu_L | 0:773e2a2be16f | 102 | printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); |
Manu_L | 0:773e2a2be16f | 103 | |
Manu_L | 0:773e2a2be16f | 104 | /* Get the state of bridge A */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 105 | uint16_t bridgeState = motor->get_bridge_status(0); |
Manu_L | 0:773e2a2be16f | 106 | |
davide.aliprandi@st.com | 5:7517162fb2b9 | 107 | if (bridgeState == 0) { |
davide.aliprandi@st.com | 5:7517162fb2b9 | 108 | if (motor->get_device_state(0) != INACTIVE) { |
Manu_L | 0:773e2a2be16f | 109 | /* Bridges were disabled due to overcurrent or over temperature */ |
Manu_L | 0:773e2a2be16f | 110 | /* When motor was running */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 111 | my_error_handler(0XBAD0); |
Manu_L | 0:773e2a2be16f | 112 | } |
Manu_L | 0:773e2a2be16f | 113 | } |
Manu_L | 0:773e2a2be16f | 114 | } |
Manu_L | 0:773e2a2be16f | 115 | |
Manu_L | 0:773e2a2be16f | 116 | /* Main ----------------------------------------------------------------------*/ |
Manu_L | 0:773e2a2be16f | 117 | |
Manu_L | 0:773e2a2be16f | 118 | int main() |
Manu_L | 0:773e2a2be16f | 119 | { |
Manu_L | 0:773e2a2be16f | 120 | |
Manu_L | 0:773e2a2be16f | 121 | /* Printing to the console. */ |
Manu_L | 0:773e2a2be16f | 122 | printf("STARTING MAIN PROGRAM\r\n"); |
Manu_L | 0:773e2a2be16f | 123 | |
Manu_L | 0:773e2a2be16f | 124 | //----- Initialization |
Manu_L | 0:773e2a2be16f | 125 | |
Manu_L | 0:773e2a2be16f | 126 | /* Initializing Motor Control Component. */ |
Manu_L | 0:773e2a2be16f | 127 | #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8) |
Davidroid | 6:bd2fa888c101 | 128 | motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A2); |
Manu_L | 0:773e2a2be16f | 129 | #elif (defined TARGET_NUCLEO_L152RE) |
Davidroid | 6:bd2fa888c101 | 130 | motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A3); |
Manu_L | 0:773e2a2be16f | 131 | #else |
Davidroid | 6:bd2fa888c101 | 132 | motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A0); |
Manu_L | 0:773e2a2be16f | 133 | #endif |
davide.aliprandi@st.com | 5:7517162fb2b9 | 134 | if (motor->init(&init) != COMPONENT_OK) exit(EXIT_FAILURE); |
Manu_L | 0:773e2a2be16f | 135 | |
Manu_L | 0:773e2a2be16f | 136 | /* Set dual bridge enabled as two motors are used*/ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 137 | motor->set_dual_full_bridge_config(1); |
Manu_L | 0:773e2a2be16f | 138 | |
Manu_L | 0:773e2a2be16f | 139 | /* Attaching and enabling an interrupt handler. */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 140 | motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:7517162fb2b9 | 141 | motor->enable_flag_irq(); |
Manu_L | 0:773e2a2be16f | 142 | |
Manu_L | 0:773e2a2be16f | 143 | /* Attaching an error handler */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 144 | motor->attach_error_handler(&my_error_handler); |
Manu_L | 0:773e2a2be16f | 145 | |
Manu_L | 0:773e2a2be16f | 146 | /* Printing to the console. */ |
Manu_L | 0:773e2a2be16f | 147 | printf("Motor Control Application Example for 2 brush DC motors\r\n"); |
Manu_L | 0:773e2a2be16f | 148 | |
Manu_L | 0:773e2a2be16f | 149 | /* Set PWM Frequency of Ref to 15000 Hz */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 150 | motor->set_ref_pwm_freq(0, 15000); |
Manu_L | 0:773e2a2be16f | 151 | |
Manu_L | 0:773e2a2be16f | 152 | /* Set PWM duty cycle of Ref to 60% */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 153 | motor->set_ref_pwm_dc(0, 60); |
Manu_L | 0:773e2a2be16f | 154 | |
Manu_L | 0:773e2a2be16f | 155 | /* Set PWM Frequency of bridge A inputs to 10000 Hz */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 156 | motor->set_bridge_input_pwm_freq(0,10000); |
Manu_L | 0:773e2a2be16f | 157 | |
Manu_L | 0:773e2a2be16f | 158 | /* Set PWM Frequency of bridge B inputs to 10000 Hz */ |
davide.aliprandi@st.com | 5:7517162fb2b9 | 159 | motor->set_bridge_input_pwm_freq(1,10000); |
Manu_L | 0:773e2a2be16f | 160 | |
Manu_L | 0:773e2a2be16f | 161 | /* Infinite Loop. */ |
Manu_L | 0:773e2a2be16f | 162 | printf("--> Infinite Loop...\r\n"); |
LorenzoCR | 7:b0ba8a7b6e9f | 163 | bool fase = 1; |
davide.aliprandi@st.com | 5:7517162fb2b9 | 164 | while (true) { |
LorenzoCR | 7:b0ba8a7b6e9f | 165 | motor->run(0, BDCMotor::FWD); |
LorenzoCR | 7:b0ba8a7b6e9f | 166 | motor->run(1, BDCMotor::FWD); |
LorenzoCR | 7:b0ba8a7b6e9f | 167 | motor->set_speed(1,49); |
LorenzoCR | 7:b0ba8a7b6e9f | 168 | motor->set_speed(0,50); |
LorenzoCR | 7:b0ba8a7b6e9f | 169 | sensor1.start(); |
LorenzoCR | 7:b0ba8a7b6e9f | 170 | if (sensor1.get_dist_cm() < 10){ |
LorenzoCR | 7:b0ba8a7b6e9f | 171 | fase = !fase; |
LorenzoCR | 7:b0ba8a7b6e9f | 172 | if (fase == 0){ |
LorenzoCR | 7:b0ba8a7b6e9f | 173 | wait_ms(2500); |
LorenzoCR | 7:b0ba8a7b6e9f | 174 | motor->set_speed(1,15); |
LorenzoCR | 7:b0ba8a7b6e9f | 175 | motor->set_speed(0,50); |
LorenzoCR | 7:b0ba8a7b6e9f | 176 | wait_ms(500); |
LorenzoCR | 7:b0ba8a7b6e9f | 177 | motor->set_speed(1,49); |
davide.aliprandi@st.com | 5:7517162fb2b9 | 178 | motor->set_speed(0,50); |
LorenzoCR | 7:b0ba8a7b6e9f | 179 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 180 | else |
LorenzoCR | 7:b0ba8a7b6e9f | 181 | { |
LorenzoCR | 7:b0ba8a7b6e9f | 182 | wait_ms(2500); |
LorenzoCR | 7:b0ba8a7b6e9f | 183 | motor->set_speed(1,50); |
LorenzoCR | 7:b0ba8a7b6e9f | 184 | motor->set_speed(0,15); |
LorenzoCR | 7:b0ba8a7b6e9f | 185 | wait_ms(500); |
LorenzoCR | 7:b0ba8a7b6e9f | 186 | motor->set_speed(1,49); |
LorenzoCR | 7:b0ba8a7b6e9f | 187 | motor->set_speed(0,50); |
LorenzoCR | 7:b0ba8a7b6e9f | 188 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 189 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 190 | |
LorenzoCR | 7:b0ba8a7b6e9f | 191 | if(color.getRed()>100&&color.getGreen()>100) |
LorenzoCR | 7:b0ba8a7b6e9f | 192 | { |
LorenzoCR | 7:b0ba8a7b6e9f | 193 | //se vede il giallo allora fa questo |
LorenzoCR | 7:b0ba8a7b6e9f | 194 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 195 | if(color.getRed()>130&&color.getGreen()>130&&color.getBlue()>130){ |
LorenzoCR | 7:b0ba8a7b6e9f | 196 | //se vede il bianco allora fa questo |
LorenzoCR | 7:b0ba8a7b6e9f | 197 | unsigned int motorino= |
LorenzoCR | 7:b0ba8a7b6e9f | 198 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 199 | if(color.getRed()>110){ |
LorenzoCR | 7:b0ba8a7b6e9f | 200 | //se vede il rosso |
LorenzoCR | 7:b0ba8a7b6e9f | 201 | motor->hard_stop(0); |
LorenzoCR | 7:b0ba8a7b6e9f | 202 | motor->hard_stop(1); |
LorenzoCR | 7:b0ba8a7b6e9f | 203 | wait(2); |
LorenzoCR | 7:b0ba8a7b6e9f | 204 | } |
LorenzoCR | 7:b0ba8a7b6e9f | 205 | } |
Manu_L | 0:773e2a2be16f | 206 | } |
davide.aliprandi@st.com | 5:7517162fb2b9 | 207 | |
LorenzoCR | 7:b0ba8a7b6e9f | 208 | /*********** (C) COPYRIGHT STMicroelectronics ********** END OF FILE **********/ |