
SunTracker_BLE
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 X_NUCLEO_IHM01A1 X_NUCLEO_IKS01A1 mbed
Fork of SunTracker_BLE by
Overview
The SunTracker is a demo application running on ST Nucleo-F401RE stacking a set of ST X-NUCLEO expansion boards.
Main features provided are:
- A solar panel follows the light source, orienting the panel in order to achieve the best panel efficiency.
- Orientation is controlled thanks to a couple of VL6180X FlightSense light sensors mounted on a X-NUCLEO-6180XA1 expansion board and driven by X-NUCLEO-IHM01A1 controlled stepper motor acting as actuator to orientate the panel.
- The system features a progressive control on the stepper motor in order to modulate the panel rotation speed according to the light angle.
- The application is also able to control the panel productivity reading the panel voltage through an ADC and proving feedback on the local display.
- A manual orientation is possible by using the accelerometer on a X-NUCLEO-IKS01A1 expansion board that, according on board tilt, controls the speed and the rotate direction.
- A remote control is available using a X-NUCLEO-IDB04A1 or a X-NUCLEO-IDB05A1 Bluetooth Low Energy expansion board. Remote control software is here.
Working Status
- SunTracker has 3 working status visible on FlightSense display and switchable by pressing the User Button:
Status 0 (Idle)
- Motor: Free Turning
- Display: Waiting for User Button
Status 1
- Motor: Driven by Light
- Display: Direction and Light Intensity = Direction and Motor Speed
Status 2
- Motor: Driven by Light
- Display: Solar Panel Efficiency
Status 3
- Motor: Driven by Accelerometer
- Display: Direction and Accelerometer Intensity
Server Startup
- When you plug the power supply, the word ‘PUSH’ is shown on display.
- You can manually rotate the structure to assign the ‘Zero Point’. Then press the User Button to launch the application.
- The display will show this status, which means that the structureis oriented to maximize the efficiency of the solar panel.
- If there is a light displacement, the structure will rotate, left or right,to follow the light source and on display is shown the direction and the speed.
- You can press the User Button to show the panel efficiencywith 4 digits that represent the range from 0v (0000) to 3,3v (3300).
- Further pressing the User Button you will manual rotate the panel by tilt the Server or Client accelerometer depending by BLE connection.
Client Startup
- The Client application can remotely control the User Button and the Accelerometer functions.
- Power on the Client AFTER the Server, it will automatically search for the SunTracker and will establish a BLE connection.
- The Green Led on Nucleo Client board will be powered on.
Rotation Features
- It has been implemented a block of rotation to avoid cables twist.
- The blocking point can be set in the firmware by changing a constant.
- You can manually rotate the structure to assign the ‘Zero Point’ before press the User Button to launch the application.
- The system features a progressive control on the stepper motor in order to modulate the rotation speed according to the light or accelerometer angle.
List of Components
SERVER SunTracker_BLE
- Nucleo-F401RE platform using a STM32F401RET6 microcontroller.
- X-NUCLEO-IHM01A1 - Stepper motor driver board based on the EasySPIN L6474.
- X-NUCLEO-6180XA1 - 3-in-1 proximity and ambient light sensor board based on ST FlightSense technology.
- VL6180X-SATEL - Satellite boards compatible with X-NUCLEO-6180XA1 board.
- X-NUCLEO-IKS01A1 - Motion MEMS and environmental sensor board.
- X-NUCLEO-IDB04A1 or X-NUCLEO-IDB05A1 - Bluetooth Low Energy Bluetooth low energy evaluation board.
- Stepper Motor 400’’ (Part Number 5350401) - To orientate the Mechanical Structure.
- Solar Panel 0.446w (Part Number 0194127) - To capture sunlight and generate electrical current.
- Power Supply 12v (Part Number 7262993) - To provide power supply at the Stepper Motor.
- Flat Cable 6 ways (Part Number 1807010) - To plug VL6180X-SATEL with X-NUCLEO-6180XA1 (60cm length each x2).
- Cable Connector (Part Number 6737694) - To plug the Flat Cable (x4).
- Power Connector (Part Number 0487842) - To provide Power Supply to X-NUCLEO-IHM01A1.
CLIENT SunTracker_BLE_Remote
- Nucleo-F401RE platform using a STM32F401RET6 microcontroller.
- X-NUCLEO-IKS01A1 - Motion MEMS and environmental sensor board.
- X-NUCLEO-IDB04A1 or X-NUCLEO-IDB05A1- Bluetooth Low Energy Bluetooth low energy evaluation board.
MECHANICAL STRUCTURE
Find here the STL files to print with a 3D printer.
FLAT CABLE ASSEMBLY
HARDWARE SETUP
Nucleo ADC + Solar Panel
Connect Solar Panel cables to Nucleo Morpho PC_3 (white) and Nucleo Morpho GND (black). Connect a capacitor 10uF between PC_3 and GND to stabilize its voltage value shown on display.
EasySpin (L6474) + BLE
Hardware conflict between EasySpin DIR1 and BLE Reset, both on same Arduino Pin PA_8. Disconnect PA_8 between EasySpin and Nucleo by fold EasySpin Pin. PB_2 has been configured as EasySpin DIR1 in the firmware .Connect Nucleo Morpho PB_2 to FlightSense Arduino PA_8 by a wire.
FlightSense Satellites
In case of instability with I2C due to long flat cables, solder 4 SMD capacitors 47pF on FlightSense board in parallel between R15, R16, R17, R18 and plug 2 capacitors 15pF between FlightSense Arduino PB_8 and PB_9 to GND pin to cut-off noises over 720 KHz.
Arduino & Morpho Pinout
main_GOOD.cpp.h@7:54984d031243, 2016-02-10 (annotated)
- Committer:
- fabiombed
- Date:
- Wed Feb 10 12:52:55 2016 +0000
- Revision:
- 7:54984d031243
Only Flightsense and BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabiombed | 7:54984d031243 | 1 | /** |
fabiombed | 7:54984d031243 | 2 | ****************************************************************************** |
fabiombed | 7:54984d031243 | 3 | * @file main.cpp |
fabiombed | 7:54984d031243 | 4 | * @author Fabio Brembilla |
fabiombed | 7:54984d031243 | 5 | * @version V2.0.0 |
fabiombed | 7:54984d031243 | 6 | * @date February, 2016 |
fabiombed | 7:54984d031243 | 7 | * @brief SunTracker + RemoteControl Vertical Application |
fabiombed | 7:54984d031243 | 8 | * This application use IHM01A1, 6180XA1, IKS01A1, IDB0XA1 expansion boards |
fabiombed | 7:54984d031243 | 9 | ****************************************************************************** |
fabiombed | 7:54984d031243 | 10 | * @attention |
fabiombed | 7:54984d031243 | 11 | * |
fabiombed | 7:54984d031243 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
fabiombed | 7:54984d031243 | 13 | * |
fabiombed | 7:54984d031243 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
fabiombed | 7:54984d031243 | 15 | * are permitted provided that the following conditions are met: |
fabiombed | 7:54984d031243 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
fabiombed | 7:54984d031243 | 17 | * this list of conditions and the following disclaimer. |
fabiombed | 7:54984d031243 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fabiombed | 7:54984d031243 | 19 | * this list of conditions and the following disclaimer in the documentation |
fabiombed | 7:54984d031243 | 20 | * and/or other materials provided with the distribution. |
fabiombed | 7:54984d031243 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
fabiombed | 7:54984d031243 | 22 | * may be used to endorse or promote products derived from this software |
fabiombed | 7:54984d031243 | 23 | * without specific prior written permission. |
fabiombed | 7:54984d031243 | 24 | * |
fabiombed | 7:54984d031243 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
fabiombed | 7:54984d031243 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fabiombed | 7:54984d031243 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
fabiombed | 7:54984d031243 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
fabiombed | 7:54984d031243 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fabiombed | 7:54984d031243 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
fabiombed | 7:54984d031243 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
fabiombed | 7:54984d031243 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
fabiombed | 7:54984d031243 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
fabiombed | 7:54984d031243 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fabiombed | 7:54984d031243 | 35 | * |
fabiombed | 7:54984d031243 | 36 | ****************************************************************************** |
fabiombed | 7:54984d031243 | 37 | */ |
fabiombed | 7:54984d031243 | 38 | |
fabiombed | 7:54984d031243 | 39 | /* Includes ------------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 40 | |
fabiombed | 7:54984d031243 | 41 | // Mbed specific header files |
fabiombed | 7:54984d031243 | 42 | #include "mbed.h" |
fabiombed | 7:54984d031243 | 43 | |
fabiombed | 7:54984d031243 | 44 | // Helper header files |
fabiombed | 7:54984d031243 | 45 | #include "DevSPI.h" |
fabiombed | 7:54984d031243 | 46 | #include "DevI2C.h" |
fabiombed | 7:54984d031243 | 47 | |
fabiombed | 7:54984d031243 | 48 | // Component specific header files |
fabiombed | 7:54984d031243 | 49 | #include "l6474_class.h" |
fabiombed | 7:54984d031243 | 50 | #include "x_nucleo_6180xa1.h" |
fabiombed | 7:54984d031243 | 51 | #include "x_nucleo_iks01a1.h" |
fabiombed | 7:54984d031243 | 52 | |
fabiombed | 7:54984d031243 | 53 | // C header files |
fabiombed | 7:54984d031243 | 54 | #include <string.h> |
fabiombed | 7:54984d031243 | 55 | #include <stdlib.h> |
fabiombed | 7:54984d031243 | 56 | #include <stdio.h> |
fabiombed | 7:54984d031243 | 57 | #include <assert.h> |
fabiombed | 7:54984d031243 | 58 | |
fabiombed | 7:54984d031243 | 59 | /* BlueTooth -----------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 60 | |
fabiombed | 7:54984d031243 | 61 | #include "debug.h" // Need for PRINTF |
fabiombed | 7:54984d031243 | 62 | #include "Utils.h" // Need for STORE_LE_16 and _32 |
fabiombed | 7:54984d031243 | 63 | |
fabiombed | 7:54984d031243 | 64 | typedef struct { |
fabiombed | 7:54984d031243 | 65 | int32_t AXIS_X; |
fabiombed | 7:54984d031243 | 66 | int32_t AXIS_Y; |
fabiombed | 7:54984d031243 | 67 | int32_t AXIS_Z; |
fabiombed | 7:54984d031243 | 68 | } AxesRaw_TypeDef; |
fabiombed | 7:54984d031243 | 69 | |
fabiombed | 7:54984d031243 | 70 | typedef enum ConnectionStatus_t { |
fabiombed | 7:54984d031243 | 71 | DISCONNECTED =0, |
fabiombed | 7:54984d031243 | 72 | CONNECTED =1 |
fabiombed | 7:54984d031243 | 73 | }cns_t; |
fabiombed | 7:54984d031243 | 74 | |
fabiombed | 7:54984d031243 | 75 | const unsigned LENGTH_OF_LONG_UUID = 16; |
fabiombed | 7:54984d031243 | 76 | typedef uint16_t ShortUUIDBytes_t; |
fabiombed | 7:54984d031243 | 77 | typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; |
fabiombed | 7:54984d031243 | 78 | |
fabiombed | 7:54984d031243 | 79 | #include "CustomControlService.h" |
fabiombed | 7:54984d031243 | 80 | #include "CustomSensorsService.h" |
fabiombed | 7:54984d031243 | 81 | |
fabiombed | 7:54984d031243 | 82 | static BLE *p_BLEdev = NULL; |
fabiombed | 7:54984d031243 | 83 | |
fabiombed | 7:54984d031243 | 84 | #define BLE_DEV_NAME "SunTracker" |
fabiombed | 7:54984d031243 | 85 | #define BLE_DEV_MAC 0xAA,0xBB,0xCC,0xDD,0xEE,0xFF |
fabiombed | 7:54984d031243 | 86 | #define BLE_ADVERTISING_INTERVAL 1000 |
fabiombed | 7:54984d031243 | 87 | |
fabiombed | 7:54984d031243 | 88 | /* Definitions ---------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 89 | |
fabiombed | 7:54984d031243 | 90 | #define SET_ACC 400 // Set Motor Acceleration |
fabiombed | 7:54984d031243 | 91 | #define SET_DEC 400 // Set Motor Deceleration |
fabiombed | 7:54984d031243 | 92 | #define SET_MAX 200 // Set Motor MaxSpeed |
fabiombed | 7:54984d031243 | 93 | #define SET_MIN 100 // Set Motor MinSpeed |
fabiombed | 7:54984d031243 | 94 | #define STOP 1000 // Set Motor Stop Position |
fabiombed | 7:54984d031243 | 95 | #define TOLLERANCE 100 // Tollerance between Left and Right before Start Movement |
fabiombed | 7:54984d031243 | 96 | #define RANGE_1 200 // Range 1 for Motor Speed |
fabiombed | 7:54984d031243 | 97 | #define RANGE_2 500 // Range 2 for Motor Speed |
fabiombed | 7:54984d031243 | 98 | |
fabiombed | 7:54984d031243 | 99 | /* Variables -----------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 100 | |
fabiombed | 7:54984d031243 | 101 | int16_t dir=0; // Motor Rotation Direction: 0 = Stop, 1 = Anticlockwise, 2 = Clockwise |
fabiombed | 7:54984d031243 | 102 | int16_t changedir=0; // Change Direction: 0 = No, 1 = Yes |
fabiombed | 7:54984d031243 | 103 | int16_t babybear=0; // Difference (in Lux) between Left and Right |
fabiombed | 7:54984d031243 | 104 | int acc_data[3]; // Difference of Accelerometer |
fabiombed | 7:54984d031243 | 105 | int16_t diff=0; // Abs of Babybear or Accelerometer difference |
fabiombed | 7:54984d031243 | 106 | int16_t left=0; // Left Command for Rotate Direction |
fabiombed | 7:54984d031243 | 107 | int16_t right=0; // Right Command for Rotate Direction |
fabiombed | 7:54984d031243 | 108 | int16_t start=0; // Waiting User Button Push |
fabiombed | 7:54984d031243 | 109 | int32_t pos=0; // Motor Position |
fabiombed | 7:54984d031243 | 110 | char DisplayStr[5]; // Status Display |
fabiombed | 7:54984d031243 | 111 | int16_t Display=0; // Shown on Display: 0 = Motor Speed, 1 = Solar Panel Value, 2 = Manual Control |
fabiombed | 7:54984d031243 | 112 | int16_t status, status_t, status_b, status_l, status_r; // Babybear Status |
fabiombed | 7:54984d031243 | 113 | |
fabiombed | 7:54984d031243 | 114 | /* ---------------------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 115 | |
fabiombed | 7:54984d031243 | 116 | // Initializing SPI bus |
fabiombed | 7:54984d031243 | 117 | DevSPI dev_spi(D11, D12, D13); |
fabiombed | 7:54984d031243 | 118 | |
fabiombed | 7:54984d031243 | 119 | // Initializing I2C bus |
fabiombed | 7:54984d031243 | 120 | DevI2C dev_i2c(D14, D15); |
fabiombed | 7:54984d031243 | 121 | |
fabiombed | 7:54984d031243 | 122 | // Initializing Motor Component IHM01A1 |
fabiombed | 7:54984d031243 | 123 | static L6474 *motor; |
fabiombed | 7:54984d031243 | 124 | |
fabiombed | 7:54984d031243 | 125 | // Initializing Babybear Component 6180XA1 |
fabiombed | 7:54984d031243 | 126 | //static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&dev_i2c, NC, NC, NC, NC); |
fabiombed | 7:54984d031243 | 127 | //MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right; |
fabiombed | 7:54984d031243 | 128 | static X_NUCLEO_6180XA1 *board; |
fabiombed | 7:54984d031243 | 129 | MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right; |
fabiombed | 7:54984d031243 | 130 | |
fabiombed | 7:54984d031243 | 131 | // Initializing MEMS Component IKS01A1 |
fabiombed | 7:54984d031243 | 132 | //static X_NUCLEO_IKS01A1 *mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
fabiombed | 7:54984d031243 | 133 | //MotionSensor *accelerometer = mems->GetAccelerometer(); |
fabiombed | 7:54984d031243 | 134 | static X_NUCLEO_IKS01A1 *mems; |
fabiombed | 7:54984d031243 | 135 | MotionSensor *accelerometer; |
fabiombed | 7:54984d031243 | 136 | |
fabiombed | 7:54984d031243 | 137 | void DISP_ExecLoopBody(void) {}; |
fabiombed | 7:54984d031243 | 138 | |
fabiombed | 7:54984d031243 | 139 | //AnalogIn analog_read(A1); // A1 Conflict with BLE SPI_CS --> Changed in A???? |
fabiombed | 7:54984d031243 | 140 | |
fabiombed | 7:54984d031243 | 141 | InterruptIn mybutton(USER_BUTTON); |
fabiombed | 7:54984d031243 | 142 | |
fabiombed | 7:54984d031243 | 143 | /* User_Button_Pressed -------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 144 | |
fabiombed | 7:54984d031243 | 145 | void User_Button_Pressed() |
fabiombed | 7:54984d031243 | 146 | { |
fabiombed | 7:54984d031243 | 147 | |
fabiombed | 7:54984d031243 | 148 | if (start>0) { |
fabiombed | 7:54984d031243 | 149 | Display++; |
fabiombed | 7:54984d031243 | 150 | } |
fabiombed | 7:54984d031243 | 151 | if (Display>2) { |
fabiombed | 7:54984d031243 | 152 | Display=0; |
fabiombed | 7:54984d031243 | 153 | } |
fabiombed | 7:54984d031243 | 154 | if (start==0) { |
fabiombed | 7:54984d031243 | 155 | start=1; |
fabiombed | 7:54984d031243 | 156 | } |
fabiombed | 7:54984d031243 | 157 | |
fabiombed | 7:54984d031243 | 158 | printf("PUSH Display %d Start %d\r\n", Display, start); |
fabiombed | 7:54984d031243 | 159 | |
fabiombed | 7:54984d031243 | 160 | } |
fabiombed | 7:54984d031243 | 161 | |
fabiombed | 7:54984d031243 | 162 | /* Initialization ------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 163 | |
fabiombed | 7:54984d031243 | 164 | bool Initialization(void) |
fabiombed | 7:54984d031243 | 165 | { |
fabiombed | 7:54984d031243 | 166 | |
fabiombed | 7:54984d031243 | 167 | // Initializing MEMS Component |
fabiombed | 7:54984d031243 | 168 | mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
fabiombed | 7:54984d031243 | 169 | accelerometer = mems->GetAccelerometer(); |
fabiombed | 7:54984d031243 | 170 | |
fabiombed | 7:54984d031243 | 171 | //---- |
fabiombed | 7:54984d031243 | 172 | |
fabiombed | 7:54984d031243 | 173 | // Initializing Babybear Component |
fabiombed | 7:54984d031243 | 174 | board=X_NUCLEO_6180XA1::Instance(&dev_i2c, NC, NC, NC, NC); |
fabiombed | 7:54984d031243 | 175 | |
fabiombed | 7:54984d031243 | 176 | /* FABIO |
fabiombed | 7:54984d031243 | 177 | status=board->InitBoard(); |
fabiombed | 7:54984d031243 | 178 | if(status) VL6180x_ErrLog("Failed to init the board!\n\r"); |
fabiombed | 7:54984d031243 | 179 | */ |
fabiombed | 7:54984d031243 | 180 | |
fabiombed | 7:54984d031243 | 181 | // Put GPIO not used as Interrupt in Hi-Z |
fabiombed | 7:54984d031243 | 182 | status_t=board->sensor_top->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 7:54984d031243 | 183 | //status_b=board->sensor_botton->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); No Present |
fabiombed | 7:54984d031243 | 184 | status_l=board->sensor_left->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 7:54984d031243 | 185 | status_r=board->sensor_right->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 7:54984d031243 | 186 | |
fabiombed | 7:54984d031243 | 187 | // Set Babybears |
fabiombed | 7:54984d031243 | 188 | status_l=board->sensor_left->AlsSetAnalogueGain(3); |
fabiombed | 7:54984d031243 | 189 | status_r=board->sensor_right->AlsSetAnalogueGain(3); |
fabiombed | 7:54984d031243 | 190 | status_l=board->sensor_left->StartMeasurement(als_continuous_polling, NULL, NULL, NULL); |
fabiombed | 7:54984d031243 | 191 | status_r=board->sensor_right->StartMeasurement(als_continuous_polling, NULL, NULL, NULL); |
fabiombed | 7:54984d031243 | 192 | |
fabiombed | 7:54984d031243 | 193 | //---- |
fabiombed | 7:54984d031243 | 194 | |
fabiombed | 7:54984d031243 | 195 | // Initializing Motor Component |
fabiombed | 7:54984d031243 | 196 | motor = new L6474(D2, D8, D7, D9, D10, dev_spi); |
fabiombed | 7:54984d031243 | 197 | if (motor->Init(NULL) != COMPONENT_OK) |
fabiombed | 7:54984d031243 | 198 | return false; |
fabiombed | 7:54984d031243 | 199 | |
fabiombed | 7:54984d031243 | 200 | motor->SetStepMode(STEP_MODE_1_8); // Default is STEP_MODE_1_16 |
fabiombed | 7:54984d031243 | 201 | |
fabiombed | 7:54984d031243 | 202 | // Set defaults Motor Speed |
fabiombed | 7:54984d031243 | 203 | motor->SetAcceleration(SET_ACC); |
fabiombed | 7:54984d031243 | 204 | motor->SetDeceleration(SET_DEC); |
fabiombed | 7:54984d031243 | 205 | motor->SetMaxSpeed(SET_MAX); // Variable by Light/Mems Sensors |
fabiombed | 7:54984d031243 | 206 | motor->SetMinSpeed(SET_MIN); |
fabiombed | 7:54984d031243 | 207 | |
fabiombed | 7:54984d031243 | 208 | return true; |
fabiombed | 7:54984d031243 | 209 | |
fabiombed | 7:54984d031243 | 210 | } |
fabiombed | 7:54984d031243 | 211 | |
fabiombed | 7:54984d031243 | 212 | /* Measure_Babybear ----------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 213 | |
fabiombed | 7:54984d031243 | 214 | void Measure_Babybear(void) |
fabiombed | 7:54984d031243 | 215 | { |
fabiombed | 7:54984d031243 | 216 | |
fabiombed | 7:54984d031243 | 217 | status_l=board->sensor_left->GetMeasurement(als_continuous_polling, &data_sensor_left); |
fabiombed | 7:54984d031243 | 218 | status_r=board->sensor_right->GetMeasurement(als_continuous_polling, &data_sensor_right); |
fabiombed | 7:54984d031243 | 219 | |
fabiombed | 7:54984d031243 | 220 | babybear = data_sensor_right.lux - data_sensor_left.lux; |
fabiombed | 7:54984d031243 | 221 | |
fabiombed | 7:54984d031243 | 222 | diff = abs(babybear); |
fabiombed | 7:54984d031243 | 223 | |
fabiombed | 7:54984d031243 | 224 | if (babybear>0) { |
fabiombed | 7:54984d031243 | 225 | left=0; |
fabiombed | 7:54984d031243 | 226 | right=1; |
fabiombed | 7:54984d031243 | 227 | } |
fabiombed | 7:54984d031243 | 228 | if (babybear<0) { |
fabiombed | 7:54984d031243 | 229 | left=1; |
fabiombed | 7:54984d031243 | 230 | right=0; |
fabiombed | 7:54984d031243 | 231 | } |
fabiombed | 7:54984d031243 | 232 | |
fabiombed | 7:54984d031243 | 233 | } |
fabiombed | 7:54984d031243 | 234 | |
fabiombed | 7:54984d031243 | 235 | /* Measure_Accelerometer -----------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 236 | |
fabiombed | 7:54984d031243 | 237 | void Measure_Accelerometer(void) |
fabiombed | 7:54984d031243 | 238 | { |
fabiombed | 7:54984d031243 | 239 | |
fabiombed | 7:54984d031243 | 240 | accelerometer->Get_X_Axes(acc_data); |
fabiombed | 7:54984d031243 | 241 | |
fabiombed | 7:54984d031243 | 242 | diff = abs(acc_data[0]); |
fabiombed | 7:54984d031243 | 243 | |
fabiombed | 7:54984d031243 | 244 | if (acc_data[0]>0) { |
fabiombed | 7:54984d031243 | 245 | left=0; |
fabiombed | 7:54984d031243 | 246 | right=1; |
fabiombed | 7:54984d031243 | 247 | } |
fabiombed | 7:54984d031243 | 248 | if (acc_data[0]<0) { |
fabiombed | 7:54984d031243 | 249 | left=1; |
fabiombed | 7:54984d031243 | 250 | right=0; |
fabiombed | 7:54984d031243 | 251 | } |
fabiombed | 7:54984d031243 | 252 | |
fabiombed | 7:54984d031243 | 253 | } |
fabiombed | 7:54984d031243 | 254 | |
fabiombed | 7:54984d031243 | 255 | /* Control_Motor -------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 256 | |
fabiombed | 7:54984d031243 | 257 | void Control_Motor(void) |
fabiombed | 7:54984d031243 | 258 | { |
fabiombed | 7:54984d031243 | 259 | |
fabiombed | 7:54984d031243 | 260 | //printf("Diff: %d lux/mems\n\r", diff); |
fabiombed | 7:54984d031243 | 261 | motor->SetMaxSpeed(diff); |
fabiombed | 7:54984d031243 | 262 | |
fabiombed | 7:54984d031243 | 263 | if (diff>TOLLERANCE) { |
fabiombed | 7:54984d031243 | 264 | if (diff <=RANGE_1) |
fabiombed | 7:54984d031243 | 265 | { |
fabiombed | 7:54984d031243 | 266 | if (left) { strcpy(DisplayStr,"E___"); } |
fabiombed | 7:54984d031243 | 267 | if (right) { strcpy(DisplayStr,"___3"); } |
fabiombed | 7:54984d031243 | 268 | } |
fabiombed | 7:54984d031243 | 269 | else if (diff >RANGE_1 & diff <=RANGE_2) |
fabiombed | 7:54984d031243 | 270 | { |
fabiombed | 7:54984d031243 | 271 | if (left) { strcpy(DisplayStr,"E==="); } |
fabiombed | 7:54984d031243 | 272 | if (right) { strcpy(DisplayStr,"===3"); } |
fabiombed | 7:54984d031243 | 273 | } |
fabiombed | 7:54984d031243 | 274 | else if (diff >RANGE_2) |
fabiombed | 7:54984d031243 | 275 | { |
fabiombed | 7:54984d031243 | 276 | if (left) { strcpy(DisplayStr,"E~~~"); } |
fabiombed | 7:54984d031243 | 277 | if (right) { strcpy(DisplayStr,"~~~3"); } |
fabiombed | 7:54984d031243 | 278 | } |
fabiombed | 7:54984d031243 | 279 | |
fabiombed | 7:54984d031243 | 280 | // In Case of Change Direction |
fabiombed | 7:54984d031243 | 281 | if (left & dir==2) { changedir=1; } |
fabiombed | 7:54984d031243 | 282 | if (right & dir==1) { changedir=1; } |
fabiombed | 7:54984d031243 | 283 | |
fabiombed | 7:54984d031243 | 284 | // Run only if Stop or Change Direction |
fabiombed | 7:54984d031243 | 285 | if (diff>TOLLERANCE & (dir==0 | changedir==1)) { |
fabiombed | 7:54984d031243 | 286 | if (left) { motor->Run(StepperMotor::FWD); dir=1; changedir=0; } |
fabiombed | 7:54984d031243 | 287 | if (right) { motor->Run(StepperMotor::BWD); dir=2; changedir=0; } |
fabiombed | 7:54984d031243 | 288 | } |
fabiombed | 7:54984d031243 | 289 | } |
fabiombed | 7:54984d031243 | 290 | |
fabiombed | 7:54984d031243 | 291 | // Get Motor Position and Control Rotation Block |
fabiombed | 7:54984d031243 | 292 | pos = motor->GetPosition(); |
fabiombed | 7:54984d031243 | 293 | if (pos>STOP | pos<-STOP) { |
fabiombed | 7:54984d031243 | 294 | if (pos>0) { motor->GoTo(STOP); } |
fabiombed | 7:54984d031243 | 295 | if (pos<0) { motor->GoTo(-STOP); } |
fabiombed | 7:54984d031243 | 296 | printf("GOTO\n\r"); // Without this command, the motor remain in stop |
fabiombed | 7:54984d031243 | 297 | } |
fabiombed | 7:54984d031243 | 298 | |
fabiombed | 7:54984d031243 | 299 | // Stop Motor |
fabiombed | 7:54984d031243 | 300 | if (diff<=TOLLERANCE) { // It continues to send the command to stop the motor. Think to do it just one time |
fabiombed | 7:54984d031243 | 301 | motor->HardStop(); |
fabiombed | 7:54984d031243 | 302 | if (Display==0) { strcpy(DisplayStr,"----"); } |
fabiombed | 7:54984d031243 | 303 | if (Display==2) { strcpy(DisplayStr,"E 3"); } |
fabiombed | 7:54984d031243 | 304 | dir=0; |
fabiombed | 7:54984d031243 | 305 | changedir=0; |
fabiombed | 7:54984d031243 | 306 | //printf("STOP\n\r"); |
fabiombed | 7:54984d031243 | 307 | } |
fabiombed | 7:54984d031243 | 308 | |
fabiombed | 7:54984d031243 | 309 | } |
fabiombed | 7:54984d031243 | 310 | |
fabiombed | 7:54984d031243 | 311 | /* Measure_SolarPanel --------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 312 | |
fabiombed | 7:54984d031243 | 313 | void Measure_SolarPanel(void) |
fabiombed | 7:54984d031243 | 314 | { |
fabiombed | 7:54984d031243 | 315 | |
fabiombed | 7:54984d031243 | 316 | // AnalogIn: 0V return 0.0 , 3.3V return 1.0 |
fabiombed | 7:54984d031243 | 317 | // float measure = analog_read.read() * 3300; |
fabiombed | 7:54984d031243 | 318 | float measure = 0; |
fabiombed | 7:54984d031243 | 319 | //printf("Measure = %.0f mV\r\n", measure); |
fabiombed | 7:54984d031243 | 320 | //board->display->DisplayDigit("A", 0); |
fabiombed | 7:54984d031243 | 321 | |
fabiombed | 7:54984d031243 | 322 | if (Display==1) { |
fabiombed | 7:54984d031243 | 323 | sprintf(DisplayStr, "%.0f", measure); |
fabiombed | 7:54984d031243 | 324 | } |
fabiombed | 7:54984d031243 | 325 | |
fabiombed | 7:54984d031243 | 326 | board->display->DisplayString(DisplayStr, 4); |
fabiombed | 7:54984d031243 | 327 | printf("%s\n\r", DisplayStr); |
fabiombed | 7:54984d031243 | 328 | |
fabiombed | 7:54984d031243 | 329 | } |
fabiombed | 7:54984d031243 | 330 | |
fabiombed | 7:54984d031243 | 331 | /* Bluetooth CallBack ---------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 332 | |
fabiombed | 7:54984d031243 | 333 | static void onUpdatesEnabledCallback(GattAttribute::Handle_t handle) |
fabiombed | 7:54984d031243 | 334 | { |
fabiombed | 7:54984d031243 | 335 | |
fabiombed | 7:54984d031243 | 336 | } |
fabiombed | 7:54984d031243 | 337 | |
fabiombed | 7:54984d031243 | 338 | static void onUpdatesDisabledCallback(Gap::Handle_t handle) |
fabiombed | 7:54984d031243 | 339 | { |
fabiombed | 7:54984d031243 | 340 | |
fabiombed | 7:54984d031243 | 341 | } |
fabiombed | 7:54984d031243 | 342 | |
fabiombed | 7:54984d031243 | 343 | static void onDataReadCallback(const GattReadCallbackParams *eventDataP) |
fabiombed | 7:54984d031243 | 344 | { |
fabiombed | 7:54984d031243 | 345 | |
fabiombed | 7:54984d031243 | 346 | } |
fabiombed | 7:54984d031243 | 347 | |
fabiombed | 7:54984d031243 | 348 | static void myonDataWriteCallback(const GattWriteCallbackParams *eventDataP) |
fabiombed | 7:54984d031243 | 349 | { |
fabiombed | 7:54984d031243 | 350 | |
fabiombed | 7:54984d031243 | 351 | } |
fabiombed | 7:54984d031243 | 352 | |
fabiombed | 7:54984d031243 | 353 | static void onConnectionCallback(const Gap::ConnectionCallbackParams_t * connectionParams) |
fabiombed | 7:54984d031243 | 354 | { |
fabiombed | 7:54984d031243 | 355 | |
fabiombed | 7:54984d031243 | 356 | } |
fabiombed | 7:54984d031243 | 357 | |
fabiombed | 7:54984d031243 | 358 | static void onDisconnectionCallback(const Gap::DisconnectionCallbackParams_t * disConnectionReason) |
fabiombed | 7:54984d031243 | 359 | { |
fabiombed | 7:54984d031243 | 360 | |
fabiombed | 7:54984d031243 | 361 | } |
fabiombed | 7:54984d031243 | 362 | |
fabiombed | 7:54984d031243 | 363 | /* Bluetooth Initialization ---------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 364 | |
fabiombed | 7:54984d031243 | 365 | void BLE_Initialization(void) |
fabiombed | 7:54984d031243 | 366 | { |
fabiombed | 7:54984d031243 | 367 | |
fabiombed | 7:54984d031243 | 368 | p_BLEdev = new BLE; |
fabiombed | 7:54984d031243 | 369 | if (!p_BLEdev) { printf("\r\nBLE Device creation failed\r\n"); } |
fabiombed | 7:54984d031243 | 370 | const Gap::Address_t BLE_address_BE = {BLE_DEV_MAC}; |
fabiombed | 7:54984d031243 | 371 | p_BLEdev->gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE); |
fabiombed | 7:54984d031243 | 372 | |
fabiombed | 7:54984d031243 | 373 | p_BLEdev->init(); |
fabiombed | 7:54984d031243 | 374 | |
fabiombed | 7:54984d031243 | 375 | // Set BLE CallBack Functions |
fabiombed | 7:54984d031243 | 376 | p_BLEdev->gattServer().onUpdatesEnabled(onUpdatesEnabledCallback); |
fabiombed | 7:54984d031243 | 377 | p_BLEdev->gattServer().onUpdatesDisabled(onUpdatesDisabledCallback); |
fabiombed | 7:54984d031243 | 378 | p_BLEdev->gattServer().onDataRead(onDataReadCallback); |
fabiombed | 7:54984d031243 | 379 | p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback); |
fabiombed | 7:54984d031243 | 380 | p_BLEdev->gap().onConnection(onConnectionCallback); |
fabiombed | 7:54984d031243 | 381 | p_BLEdev->gap().onDisconnection(onDisconnectionCallback); |
fabiombed | 7:54984d031243 | 382 | //p_BLEdev->gattServer().onConfirmationReceived(onConfirmationReceivedCallback); |
fabiombed | 7:54984d031243 | 383 | //p_BLEdev->gattServer().onDataSent(onDataSentCallback); |
fabiombed | 7:54984d031243 | 384 | //p_BLEdev->gap().onTimeout(onTimeoutCallback); |
fabiombed | 7:54984d031243 | 385 | |
fabiombed | 7:54984d031243 | 386 | // Setup BLE Advertising |
fabiombed | 7:54984d031243 | 387 | const static char DEVICE_NAME[] = BLE_DEV_NAME; |
fabiombed | 7:54984d031243 | 388 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
fabiombed | 7:54984d031243 | 389 | #ifdef USE_SENSOR_FUSION_LIB |
fabiombed | 7:54984d031243 | 390 | uint8_t dat[] = {0x01,0x80,0x00,0xFC,0x01,0x80}; |
fabiombed | 7:54984d031243 | 391 | #else |
fabiombed | 7:54984d031243 | 392 | uint8_t dat[] = {0x01,0x80,0x00,0xFC,0x00,0x00}; |
fabiombed | 7:54984d031243 | 393 | #endif |
fabiombed | 7:54984d031243 | 394 | p_BLEdev->gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,dat,6); |
fabiombed | 7:54984d031243 | 395 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); |
fabiombed | 7:54984d031243 | 396 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
fabiombed | 7:54984d031243 | 397 | p_BLEdev->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
fabiombed | 7:54984d031243 | 398 | p_BLEdev->gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL); |
fabiombed | 7:54984d031243 | 399 | p_BLEdev->gap().startAdvertising(); |
fabiombed | 7:54984d031243 | 400 | |
fabiombed | 7:54984d031243 | 401 | } |
fabiombed | 7:54984d031243 | 402 | |
fabiombed | 7:54984d031243 | 403 | /* Main ----------------------------------------------------------------------*/ |
fabiombed | 7:54984d031243 | 404 | |
fabiombed | 7:54984d031243 | 405 | int main() |
fabiombed | 7:54984d031243 | 406 | { |
fabiombed | 7:54984d031243 | 407 | |
fabiombed | 7:54984d031243 | 408 | // Printing to the console |
fabiombed | 7:54984d031243 | 409 | printf("SunTracker by Fabio Brembilla\r\n\n"); |
fabiombed | 7:54984d031243 | 410 | |
fabiombed | 7:54984d031243 | 411 | Initialization(); |
fabiombed | 7:54984d031243 | 412 | |
fabiombed | 7:54984d031243 | 413 | printf("Initialization OK (Line %d)\r\n", __LINE__); |
fabiombed | 7:54984d031243 | 414 | |
fabiombed | 7:54984d031243 | 415 | BLE_Initialization(); |
fabiombed | 7:54984d031243 | 416 | |
fabiombed | 7:54984d031243 | 417 | printf("BLE_Initialization OK (Line %d)\r\n", __LINE__); |
fabiombed | 7:54984d031243 | 418 | |
fabiombed | 7:54984d031243 | 419 | mybutton.fall(&User_Button_Pressed); |
fabiombed | 7:54984d031243 | 420 | |
fabiombed | 7:54984d031243 | 421 | printf("Main Initializations OK (Line %d)\r\n", __LINE__); |
fabiombed | 7:54984d031243 | 422 | printf("Wait Push Button\r\n"); |
fabiombed | 7:54984d031243 | 423 | |
fabiombed | 7:54984d031243 | 424 | // Loop until push User Button to Set 0 Point |
fabiombed | 7:54984d031243 | 425 | strcpy(DisplayStr,"pusH"); |
fabiombed | 7:54984d031243 | 426 | while(start<1) { |
fabiombed | 7:54984d031243 | 427 | board->display->DisplayString(DisplayStr, 4); |
fabiombed | 7:54984d031243 | 428 | printf("%s\n\r", DisplayStr); |
fabiombed | 7:54984d031243 | 429 | //wait(0.1); // Need one command otherwise remain always in loop |
fabiombed | 7:54984d031243 | 430 | } |
fabiombed | 7:54984d031243 | 431 | |
fabiombed | 7:54984d031243 | 432 | printf("Start Main Loop\r\n"); |
fabiombed | 7:54984d031243 | 433 | |
fabiombed | 7:54984d031243 | 434 | // Main Loop |
fabiombed | 7:54984d031243 | 435 | while(true) { |
fabiombed | 7:54984d031243 | 436 | if (Display==0 | Display==1) { |
fabiombed | 7:54984d031243 | 437 | Measure_Babybear(); |
fabiombed | 7:54984d031243 | 438 | } |
fabiombed | 7:54984d031243 | 439 | if (Display==2) { |
fabiombed | 7:54984d031243 | 440 | Measure_Accelerometer(); |
fabiombed | 7:54984d031243 | 441 | } |
fabiombed | 7:54984d031243 | 442 | |
fabiombed | 7:54984d031243 | 443 | Control_Motor(); |
fabiombed | 7:54984d031243 | 444 | Measure_SolarPanel(); |
fabiombed | 7:54984d031243 | 445 | |
fabiombed | 7:54984d031243 | 446 | p_BLEdev->waitForEvent(); |
fabiombed | 7:54984d031243 | 447 | } |
fabiombed | 7:54984d031243 | 448 | |
fabiombed | 7:54984d031243 | 449 | //status_l=board->sensor_left->StopMeasurement(als_continuous_polling); |
fabiombed | 7:54984d031243 | 450 | //status_r=board->sensor_right->StopMeasurement(als_continuous_polling); |
fabiombed | 7:54984d031243 | 451 | |
fabiombed | 7:54984d031243 | 452 | } |