MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file mavrimini.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * 00037 * \brief Autopilot board based on STM32 00038 * 00039 ******************************************************************************/ 00040 00041 00042 #ifndef MAVRIMINI_HPP_ 00043 #define MAVRIMINI_HPP_ 00044 00045 #include "hal/stm32/gpio_stm32.hpp" 00046 #include "hal/stm32/i2c_stm32.hpp" 00047 #include "hal/stm32/pwm_stm32.hpp" 00048 #include "hal/stm32/serial_stm32.hpp" 00049 00050 #include "drivers/airspeed_analog.hpp" 00051 #include "drivers/battery.hpp" 00052 #include "drivers/servo.hpp" 00053 #include "drivers/sonar_i2cxl.hpp" 00054 #include "drivers/spektrum_satellite.hpp" 00055 #include "drivers/state_display_mavrimini.hpp" 00056 00057 #include "simulation/dynamic_model_quad_diag.hpp" 00058 #include "simulation/simulation.hpp" 00059 00060 #include "hal/dummy/serial_dummy.hpp" 00061 #include "hal/dummy/i2c_dummy.hpp" 00062 #include "hal/dummy/adc_dummy.hpp" 00063 #include "hal/dummy/file_dummy.hpp" 00064 #include "hal/dummy/pwm_dummy.hpp" 00065 #include "hal/dummy/gpio_dummy.hpp" 00066 #include "hal/common/led_gpio.hpp" 00067 00068 extern "C" 00069 { 00070 #include "util/streams.h" 00071 } 00072 00073 00074 // Preprocessor definitions 00075 00076 /* 00077 * Should the ESC be calibrated? 00078 * 0 for false (normal flight) 00079 * 1 for true (calibration) 00080 * !!!IMPORTANT!!! 00081 * IF CALIBRATING, TAKE OFF PROPS 00082 */ 00083 #define CALIBRATE_ESC 0 00084 00085 00089 typedef struct 00090 { 00091 gpio_stm32_conf_t dsm_receiver_gpio_config; 00092 gpio_stm32_conf_t dsm_power_gpio_config; 00093 gpio_stm32_conf_t green_led_gpio_config; 00094 gpio_stm32_conf_t red_led_gpio_config; 00095 serial_stm32_conf_t serial_1_config; 00096 serial_stm32_conf_t serial_2_config; 00097 i2c_stm32_conf_t i2c_1_config; 00098 i2c_stm32_conf_t i2c_2_config; 00099 imu_conf_t imu_config; 00100 Pwm_stm32::config_t pwm_config[8]; 00101 servo_conf_t servo_config[8]; 00102 } mavrimini_conf_t; 00103 00104 00110 static inline mavrimini_conf_t mavrimini_default_config(); 00111 00112 00117 class Mavrimini 00118 { 00119 public: 00125 Mavrimini(mavrimini_conf_t config = mavrimini_default_config()); 00126 00127 00133 bool init(void); 00134 00138 Gpio_stm32 dsm_receiver_gpio; 00139 Gpio_stm32 dsm_power_gpio; 00140 Gpio_stm32 green_led_gpio; 00141 Gpio_stm32 red_led_gpio; 00142 Led_gpio green_led; 00143 Led_gpio red_led; 00144 State_display_mavrimini state_display_mavrimini_; 00145 File_dummy file_flash; 00146 Serial_stm32 serial_1; 00147 Serial_stm32 serial_2; 00148 I2c_stm32 i2c_1; 00149 I2c_stm32 i2c_2; 00150 Spektrum_satellite spektrum_satellite; 00151 Sonar_i2cxl sonar_i2cxl; 00152 Adc_dummy adc_battery; 00153 Battery battery; 00154 Adc_dummy adc_airspeed; 00155 Airspeed_analog airspeed_analog; 00156 Pwm_stm32 pwm_0; 00157 Pwm_stm32 pwm_1; 00158 Pwm_stm32 pwm_2; 00159 Pwm_stm32 pwm_3; 00160 Pwm_stm32 pwm_4; 00161 Pwm_stm32 pwm_5; 00162 Pwm_dummy pwm_6; 00163 Pwm_dummy pwm_7; 00164 Servo servo_0; 00165 Servo servo_1; 00166 Servo servo_2; 00167 Servo servo_3; 00168 Servo servo_4; 00169 Servo servo_5; 00170 Servo servo_6; 00171 Servo servo_7; 00172 Dynamic_model_quad_diag sim_model; 00173 Simulation sim; 00174 Imu imu; 00175 00176 private: 00177 byte_stream_t dbg_stream_; 00178 }; 00179 00180 00186 static inline mavrimini_conf_t mavrimini_default_config() 00187 { 00188 mavrimini_conf_t conf = {}; 00189 00190 // ------------------------------------------------------------------------- 00191 // GPIO config 00192 // ------------------------------------------------------------------------- 00193 00194 // GPIO dsm power pin configuration 00195 conf.dsm_power_gpio_config.port = GPIO_STM32_PORT_A; 00196 conf.dsm_power_gpio_config.pin = GPIO_STM32_PIN_4; 00197 conf.dsm_power_gpio_config.dir = GPIO_OUTPUT; 00198 conf.dsm_power_gpio_config.pull = GPIO_PULL_UPDOWN_NONE; //physical pull up 00199 00200 // GPIO dsm receiver pin configuration 00201 conf.dsm_receiver_gpio_config.port = GPIO_STM32_PORT_A; 00202 conf.dsm_receiver_gpio_config.pin = GPIO_STM32_PIN_3; 00203 conf.dsm_receiver_gpio_config.dir = GPIO_INPUT; 00204 conf.dsm_receiver_gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00205 00206 // Green led 00207 conf.green_led_gpio_config.port = GPIO_STM32_PORT_C; 00208 conf.green_led_gpio_config.pin = GPIO_STM32_PIN_15; 00209 conf.green_led_gpio_config.dir = GPIO_OUTPUT; 00210 conf.green_led_gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00211 00212 // Red led 00213 conf.red_led_gpio_config.port = GPIO_STM32_PORT_C; 00214 conf.red_led_gpio_config.pin = GPIO_STM32_PIN_14; 00215 conf.red_led_gpio_config.dir = GPIO_OUTPUT; 00216 conf.red_led_gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00217 00218 00219 // ------------------------------------------------------------------------- 00220 // Serial config 00221 // ------------------------------------------------------------------------- 00222 conf.serial_1_config = serial_stm32_default_config(); 00223 conf.serial_1_config.device = SERIAL_STM32_4; 00224 conf.serial_1_config.baudrate = 57600; 00225 conf.serial_1_config.databits = SERIAL_STM32_DATABITS_8; 00226 conf.serial_1_config.stopbits = SERIAL_STM32_STOPBITS_1; 00227 conf.serial_1_config.parity = SERIAL_STM32_PARITY_NONE; 00228 conf.serial_1_config.mode = SERIAL_STM32_MODE_TX_RX; 00229 conf.serial_1_config.flow_control = SERIAL_STM32_FLOWCONTROL_NONE; 00230 conf.serial_1_config.rx_port = GPIO_STM32_PORT_A; 00231 conf.serial_1_config.rx_pin = GPIO_STM32_PIN_1; 00232 conf.serial_1_config.rx_af = GPIO_STM32_AF_8; 00233 conf.serial_1_config.tx_port = GPIO_STM32_PORT_A; 00234 conf.serial_1_config.tx_pin = GPIO_STM32_PIN_0; 00235 conf.serial_1_config.tx_af = GPIO_STM32_AF_8; 00236 00237 // ------------------------------------------------------------------------- 00238 // Serial config 00239 // ------------------------------------------------------------------------- 00240 conf.serial_2_config = serial_stm32_default_config(); 00241 conf.serial_2_config.device = SERIAL_STM32_2; 00242 conf.serial_2_config.baudrate = 115200; 00243 conf.serial_2_config.databits = SERIAL_STM32_DATABITS_8; 00244 conf.serial_2_config.stopbits = SERIAL_STM32_STOPBITS_1; 00245 conf.serial_2_config.parity = SERIAL_STM32_PARITY_NONE; 00246 conf.serial_2_config.mode = SERIAL_STM32_MODE_TX_RX; 00247 conf.serial_2_config.flow_control = SERIAL_STM32_FLOWCONTROL_NONE; 00248 conf.serial_2_config.rx_port = GPIO_STM32_PORT_A; 00249 conf.serial_2_config.rx_pin = GPIO_STM32_PIN_3; 00250 conf.serial_2_config.rx_af = GPIO_STM32_AF_7; 00251 conf.serial_2_config.tx_port = GPIO_STM32_PORT_A; 00252 conf.serial_2_config.tx_pin = GPIO_STM32_PIN_2; 00253 conf.serial_2_config.tx_af = GPIO_STM32_AF_7; 00254 00255 // ------------------------------------------------------------------------- 00256 // I2C config 00257 // ------------------------------------------------------------------------- 00258 conf.i2c_1_config = i2c_stm32_default_config(); 00259 conf.i2c_1_config.i2c_device_config = STM32_I2C1; 00260 conf.i2c_1_config.rcc_i2c_config = RCC_I2C1; 00261 conf.i2c_1_config.rcc_sda_port_config = RCC_GPIOB; 00262 conf.i2c_1_config.sda_config.port = GPIO_STM32_PORT_B; 00263 conf.i2c_1_config.sda_config.pin = GPIO_STM32_PIN_7; 00264 conf.i2c_1_config.sda_config.alt_fct = GPIO_STM32_AF_4; 00265 conf.i2c_1_config.rcc_clk_port_config = RCC_GPIOB; 00266 conf.i2c_1_config.clk_config.port = GPIO_STM32_PORT_B; 00267 conf.i2c_1_config.clk_config.pin = GPIO_STM32_PIN_6; 00268 conf.i2c_1_config.clk_config.alt_fct = GPIO_STM32_AF_4; 00269 conf.i2c_1_config.clk_speed = 400000; 00270 conf.i2c_1_config.tenbit_config = false; // 10 bits address is not supported 00271 00272 // ------------------------------------------------------------------------- 00273 // I2C config 00274 // ------------------------------------------------------------------------- 00275 conf.i2c_2_config = i2c_stm32_default_config(); 00276 conf.i2c_2_config.i2c_device_config = STM32_I2C2; 00277 conf.i2c_2_config.rcc_i2c_config = RCC_I2C2; 00278 conf.i2c_2_config.rcc_sda_port_config = RCC_GPIOB; 00279 conf.i2c_2_config.sda_config.port = GPIO_STM32_PORT_B; 00280 conf.i2c_2_config.sda_config.pin = GPIO_STM32_PIN_11; 00281 conf.i2c_2_config.sda_config.alt_fct = GPIO_STM32_AF_4; 00282 conf.i2c_2_config.rcc_clk_port_config = RCC_GPIOB; 00283 conf.i2c_2_config.clk_config.port = GPIO_STM32_PORT_B; 00284 conf.i2c_2_config.clk_config.pin = GPIO_STM32_PIN_10; 00285 conf.i2c_2_config.clk_config.alt_fct = GPIO_STM32_AF_4; 00286 conf.i2c_2_config.clk_speed = 100000; 00287 conf.i2c_2_config.tenbit_config = false; // 10 bits address is not supported 00288 00289 // ------------------------------------------------------------------------- 00290 // PWM config 00291 // ------------------------------------------------------------------------- 00292 conf.pwm_config[0].gpio_config.port = GPIO_STM32_PORT_B; 00293 conf.pwm_config[0].gpio_config.pin = GPIO_STM32_PIN_3; 00294 conf.pwm_config[0].gpio_config.dir = GPIO_OUTPUT; 00295 conf.pwm_config[0].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00296 conf.pwm_config[0].gpio_config.alt_fct = GPIO_STM32_AF_1; 00297 conf.pwm_config[0].timer = TIM2; 00298 conf.pwm_config[0].rcc_timer = RCC_TIM2; 00299 conf.pwm_config[0].channel = Pwm_stm32::PWM_STM32_CHANNEL_2; 00300 conf.pwm_config[0].prescaler = 84; //since APB1 clock is main_clk/2 00301 conf.pwm_config[0].period = 20000; //50Hz 00302 conf.pwm_config[0].pulse_us = 5000; 00303 00304 conf.pwm_config[1].gpio_config.port = GPIO_STM32_PORT_B; 00305 conf.pwm_config[1].gpio_config.pin = GPIO_STM32_PIN_4; 00306 conf.pwm_config[1].gpio_config.dir = GPIO_OUTPUT; 00307 conf.pwm_config[1].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00308 conf.pwm_config[1].gpio_config.alt_fct = GPIO_STM32_AF_2; 00309 conf.pwm_config[1].timer = TIM3; 00310 conf.pwm_config[1].rcc_timer = RCC_TIM3; 00311 conf.pwm_config[1].channel = Pwm_stm32::PWM_STM32_CHANNEL_1; 00312 conf.pwm_config[1].prescaler = 84; 00313 conf.pwm_config[1].period = 20000; //50Hz 00314 conf.pwm_config[1].pulse_us = 5000; 00315 00316 conf.pwm_config[2].gpio_config.port = GPIO_STM32_PORT_B; 00317 conf.pwm_config[2].gpio_config.pin = GPIO_STM32_PIN_5; 00318 conf.pwm_config[2].gpio_config.dir = GPIO_OUTPUT; 00319 conf.pwm_config[2].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00320 conf.pwm_config[2].gpio_config.alt_fct = GPIO_STM32_AF_2; 00321 conf.pwm_config[2].timer = TIM3; 00322 conf.pwm_config[2].rcc_timer = RCC_TIM3; 00323 conf.pwm_config[2].channel = Pwm_stm32::PWM_STM32_CHANNEL_2; 00324 conf.pwm_config[2].prescaler = 84; 00325 conf.pwm_config[2].period = 20000; //50Hz 00326 conf.pwm_config[2].pulse_us = 5000; 00327 00328 conf.pwm_config[3].gpio_config.port = GPIO_STM32_PORT_B; 00329 conf.pwm_config[3].gpio_config.pin = GPIO_STM32_PIN_6; 00330 conf.pwm_config[3].gpio_config.dir = GPIO_OUTPUT; 00331 conf.pwm_config[3].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00332 conf.pwm_config[3].gpio_config.alt_fct = GPIO_STM32_AF_2; 00333 conf.pwm_config[3].timer = TIM4; 00334 conf.pwm_config[3].rcc_timer = RCC_TIM4; 00335 conf.pwm_config[3].channel = Pwm_stm32::PWM_STM32_CHANNEL_1; 00336 conf.pwm_config[3].prescaler = 84; 00337 conf.pwm_config[3].period = 20000; //50Hz 00338 conf.pwm_config[3].pulse_us = 5000; 00339 00340 conf.pwm_config[4].gpio_config.port = GPIO_STM32_PORT_B; 00341 conf.pwm_config[4].gpio_config.pin = GPIO_STM32_PIN_7; 00342 conf.pwm_config[4].gpio_config.dir = GPIO_OUTPUT; 00343 conf.pwm_config[4].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00344 conf.pwm_config[4].gpio_config.alt_fct = GPIO_STM32_AF_2; 00345 conf.pwm_config[4].timer = TIM4; 00346 conf.pwm_config[4].rcc_timer = RCC_TIM4; 00347 conf.pwm_config[4].channel = Pwm_stm32::PWM_STM32_CHANNEL_2; 00348 conf.pwm_config[4].prescaler = 84; 00349 conf.pwm_config[4].period = 20000; //50Hz 00350 conf.pwm_config[4].pulse_us = 5000; 00351 00352 conf.pwm_config[5].gpio_config.port = GPIO_STM32_PORT_B; 00353 conf.pwm_config[5].gpio_config.pin = GPIO_STM32_PIN_8; 00354 conf.pwm_config[5].gpio_config.dir = GPIO_OUTPUT; 00355 conf.pwm_config[5].gpio_config.pull = GPIO_PULL_UPDOWN_NONE; 00356 conf.pwm_config[5].gpio_config.alt_fct = GPIO_STM32_AF_2; 00357 conf.pwm_config[5].timer = TIM4; 00358 conf.pwm_config[5].rcc_timer = RCC_TIM4; 00359 conf.pwm_config[5].channel = Pwm_stm32::PWM_STM32_CHANNEL_3; 00360 conf.pwm_config[5].prescaler = 84; 00361 conf.pwm_config[5].period = 20000; //50Hz 00362 conf.pwm_config[5].pulse_us = 5000; 00363 00364 // ------------------------------------------------------------------------- 00365 // Servo config 00366 // ------------------------------------------------------------------------- 00367 00368 // Warning: servo 0,1,3,5 are configured for a dc brush motor 00369 // ,servo 2,4 are thus free but servo 1 and 2 are sharing the 00370 // same clock and so are servo 3,4,5. Servos sharing the same 00371 // clock cannot have different period therfore all the servos 00372 // must have the same configuration for now. 00373 conf.servo_config[0] = servo_default_config_brush_motor(); 00374 conf.servo_config[1] = servo_default_config_brush_motor(); 00375 conf.servo_config[2] = servo_default_config_brush_motor(); 00376 conf.servo_config[3] = servo_default_config_brush_motor(); 00377 conf.servo_config[4] = servo_default_config_brush_motor(); 00378 conf.servo_config[5] = servo_default_config_brush_motor(); 00379 conf.servo_config[6] = servo_default_config_brush_motor(); 00380 conf.servo_config[7] = servo_default_config_brush_motor(); 00381 00382 return conf; 00383 } 00384 00385 00386 #endif /* MAVRIMINI_HPP_ */