Signori, questo è il codice di doukietown...... divertitevi
Dependencies: HCSR04 TCS3200 X_NUCLEO_IHM12A1 mbed
Fork of ovvioBug by
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author IPC Rennes 00005 * @version V1.0.0 00006 * @date April 25th, 2016 00007 * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM12A1 00008 * Motor Control Expansion Board: control of 2 Brush DC motors. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 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 /* Includes ------------------------------------------------------------------*/ 00040 00041 /* mbed specific header files. */ 00042 #include "mbed.h" 00043 #include "color.h" 00044 #include "hcsr04.h" 00045 #include "BDCMotor.h" 00046 00047 /* Component specific header files. */ 00048 #include "STSpin240_250.h" 00049 HCSR04 sensor1(D10, D11); 00050 00051 ColorSensor color(D4, D5, D6, D7, D8); 00052 00053 /* Variables -----------------------------------------------------------------*/ 00054 00055 /* Initialization parameters of the motor connected to the expansion board. */ 00056 STSpin240_250_init_t init = 00057 { 00058 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */ 00059 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */ 00060 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */ 00061 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */ 00062 TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */ 00063 }; 00064 00065 /* Motor Control Component. */ 00066 STSpin240_250 *motor; 00067 00068 /* Functions -----------------------------------------------------------------*/ 00069 00070 /** 00071 * @brief This is an example of error handler. 00072 * @param[in] error Number of the error 00073 * @retval None 00074 * @note If needed, implement it, and then attach it: 00075 * + motor->attach_error_handler(&my_error_handler); 00076 */ 00077 void my_error_handler(uint16_t error) 00078 { 00079 /* Printing to the console. */ 00080 printf("Error %d detected\r\n\n", error); 00081 00082 /* Infinite loop */ 00083 while (true) { 00084 } 00085 } 00086 00087 /** 00088 * @brief This is an example of user handler for the flag interrupt. 00089 * @param None 00090 * @retval None 00091 * @note If needed, implement it, and then attach and enable it: 00092 * + motor->attach_flag_irq(&my_flag_irq_handler); 00093 * + motor->enable_flag_irq(); 00094 * To disable it: 00095 * + motor->DisbleFlagIRQ(); 00096 */ 00097 void my_flag_irq_handler(void) 00098 { 00099 /* Code to be customised */ 00100 /************************/ 00101 00102 printf(" WARNING: \"FLAG\" interrupt triggered.\r\n"); 00103 00104 /* Get the state of bridge A */ 00105 uint16_t bridgeState = motor->get_bridge_status(0); 00106 00107 if (bridgeState == 0) { 00108 if (motor->get_device_state(0) != INACTIVE) { 00109 /* Bridges were disabled due to overcurrent or over temperature */ 00110 /* When motor was running */ 00111 my_error_handler(0XBAD0); 00112 } 00113 } 00114 } 00115 00116 /* Main ----------------------------------------------------------------------*/ 00117 00118 int main() 00119 { 00120 00121 /* Printing to the console. */ 00122 printf("STARTING MAIN PROGRAM\r\n"); 00123 00124 //----- Initialization 00125 00126 /* Initializing Motor Control Component. */ 00127 #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8) 00128 motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A2); 00129 #elif (defined TARGET_NUCLEO_L152RE) 00130 motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A3); 00131 #else 00132 motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A0); 00133 #endif 00134 if (motor->init(&init) != COMPONENT_OK) exit(EXIT_FAILURE); 00135 00136 /* Set dual bridge enabled as two motors are used*/ 00137 motor->set_dual_full_bridge_config(1); 00138 00139 /* Attaching and enabling an interrupt handler. */ 00140 motor->attach_flag_irq(&my_flag_irq_handler); 00141 motor->enable_flag_irq(); 00142 00143 /* Attaching an error handler */ 00144 motor->attach_error_handler(&my_error_handler); 00145 00146 /* Printing to the console. */ 00147 printf("Motor Control Application Example for 2 brush DC motors\r\n"); 00148 00149 /* Set PWM Frequency of Ref to 15000 Hz */ 00150 motor->set_ref_pwm_freq(0, 15000); 00151 00152 /* Set PWM duty cycle of Ref to 60% */ 00153 motor->set_ref_pwm_dc(0, 60); 00154 00155 /* Set PWM Frequency of bridge A inputs to 10000 Hz */ 00156 motor->set_bridge_input_pwm_freq(0,10000); 00157 00158 /* Set PWM Frequency of bridge B inputs to 10000 Hz */ 00159 motor->set_bridge_input_pwm_freq(1,10000); 00160 00161 /* Infinite Loop. */ 00162 printf("--> Infinite Loop...\r\n"); 00163 bool fase = 1; 00164 while (true) { 00165 motor->run(0, BDCMotor::FWD); 00166 motor->run(1, BDCMotor::FWD); 00167 motor->set_speed(1,49); 00168 motor->set_speed(0,50); 00169 sensor1.start(); 00170 if (sensor1.get_dist_cm() < 10){ 00171 fase = !fase; 00172 if (fase == 0){ 00173 wait_ms(2500); 00174 motor->set_speed(1,15); 00175 motor->set_speed(0,50); 00176 wait_ms(500); 00177 motor->set_speed(1,49); 00178 motor->set_speed(0,50); 00179 } 00180 else 00181 { 00182 wait_ms(2500); 00183 motor->set_speed(1,50); 00184 motor->set_speed(0,15); 00185 wait_ms(500); 00186 motor->set_speed(1,49); 00187 motor->set_speed(0,50); 00188 } 00189 } 00190 00191 if(color.getRed()>100&&color.getGreen()>100) 00192 { 00193 //se vede il giallo allora fa questo 00194 } 00195 if(color.getRed()>130&&color.getGreen()>130&&color.getBlue()>130){ 00196 //se vede il bianco allora fa questo 00197 unsigned int motorino= 00198 } 00199 if(color.getRed()>110){ 00200 //se vede il rosso 00201 motor->hard_stop(0); 00202 motor->hard_stop(1); 00203 wait(2); 00204 } 00205 } 00206 } 00207 00208 /*********** (C) COPYRIGHT STMicroelectronics ********** END OF FILE **********/
Generated on Tue Jul 19 2022 20:07:51 by 1.7.2