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.cpp@11:b5e23d64d366, 2016-03-03 (annotated)
- Committer:
- fabiombed
- Date:
- Thu Mar 03 16:32:10 2016 +0000
- Revision:
- 11:b5e23d64d366
- Parent:
- 10:d154094297ce
- Child:
- 12:1e6a1d3a66b5
Temporary issued fixed avoiding to use NC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabiombed | 0:becf0d313663 | 1 | /** |
fabiombed | 0:becf0d313663 | 2 | ****************************************************************************** |
fabiombed | 0:becf0d313663 | 3 | * @file main.cpp |
fabiombed | 1:8f312c1686b6 | 4 | * @author Fabio Brembilla |
fabiombed | 4:1d3d071a4c2c | 5 | * @version V2.0.0 |
fabiombed | 6:4cbf7303b496 | 6 | * @date February, 2016 |
fabiombed | 1:8f312c1686b6 | 7 | * @brief SunTracker + RemoteControl Vertical Application |
fabiombed | 1:8f312c1686b6 | 8 | * This application use IHM01A1, 6180XA1, IKS01A1, IDB0XA1 expansion boards |
fabiombed | 0:becf0d313663 | 9 | ****************************************************************************** |
fabiombed | 0:becf0d313663 | 10 | * @attention |
fabiombed | 4:1d3d071a4c2c | 11 | * |
fabiombed | 4:1d3d071a4c2c | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
fabiombed | 0:becf0d313663 | 13 | * |
fabiombed | 0:becf0d313663 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
fabiombed | 0:becf0d313663 | 15 | * are permitted provided that the following conditions are met: |
fabiombed | 0:becf0d313663 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
fabiombed | 0:becf0d313663 | 17 | * this list of conditions and the following disclaimer. |
fabiombed | 0:becf0d313663 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fabiombed | 0:becf0d313663 | 19 | * this list of conditions and the following disclaimer in the documentation |
fabiombed | 0:becf0d313663 | 20 | * and/or other materials provided with the distribution. |
fabiombed | 0:becf0d313663 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
fabiombed | 0:becf0d313663 | 22 | * may be used to endorse or promote products derived from this software |
fabiombed | 0:becf0d313663 | 23 | * without specific prior written permission. |
fabiombed | 0:becf0d313663 | 24 | * |
fabiombed | 0:becf0d313663 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
fabiombed | 0:becf0d313663 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fabiombed | 0:becf0d313663 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
fabiombed | 0:becf0d313663 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
fabiombed | 0:becf0d313663 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fabiombed | 0:becf0d313663 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
fabiombed | 0:becf0d313663 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
fabiombed | 0:becf0d313663 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
fabiombed | 0:becf0d313663 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
fabiombed | 0:becf0d313663 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fabiombed | 0:becf0d313663 | 35 | * |
fabiombed | 0:becf0d313663 | 36 | ****************************************************************************** |
fabiombed | 0:becf0d313663 | 37 | */ |
fabiombed | 4:1d3d071a4c2c | 38 | |
fabiombed | 0:becf0d313663 | 39 | /* Includes ------------------------------------------------------------------*/ |
fabiombed | 4:1d3d071a4c2c | 40 | |
fabiombed | 6:4cbf7303b496 | 41 | // Mbed specific header files |
fabiombed | 0:becf0d313663 | 42 | #include "mbed.h" |
fabiombed | 4:1d3d071a4c2c | 43 | |
fabiombed | 6:4cbf7303b496 | 44 | // Helper header files |
fabiombed | 0:becf0d313663 | 45 | #include "DevSPI.h" |
fabiombed | 1:8f312c1686b6 | 46 | #include "DevI2C.h" |
fabiombed | 0:becf0d313663 | 47 | |
fabiombed | 6:4cbf7303b496 | 48 | // Component specific header files |
fabiombed | 1:8f312c1686b6 | 49 | #include "l6474_class.h" |
fabiombed | 1:8f312c1686b6 | 50 | #include "x_nucleo_6180xa1.h" |
fabiombed | 0:becf0d313663 | 51 | #include "x_nucleo_iks01a1.h" |
fabiombed | 0:becf0d313663 | 52 | |
fabiombed | 6:4cbf7303b496 | 53 | // C header files |
fabiombed | 1:8f312c1686b6 | 54 | #include <string.h> |
fabiombed | 1:8f312c1686b6 | 55 | #include <stdlib.h> |
fabiombed | 1:8f312c1686b6 | 56 | #include <stdio.h> |
fabiombed | 1:8f312c1686b6 | 57 | #include <assert.h> |
fabiombed | 0:becf0d313663 | 58 | |
fabiombed | 5:76fb6b783487 | 59 | /* BlueTooth -----------------------------------------------------------------*/ |
fabiombed | 5:76fb6b783487 | 60 | |
fabiombed | 5:76fb6b783487 | 61 | #include "debug.h" // Need for PRINTF |
fabiombed | 5:76fb6b783487 | 62 | #include "Utils.h" // Need for STORE_LE_16 and _32 |
fabiombed | 5:76fb6b783487 | 63 | |
fabiombed | 8:144855fe02bd | 64 | const unsigned LENGTH_OF_LONG_UUID = 16; |
fabiombed | 8:144855fe02bd | 65 | typedef uint16_t ShortUUIDBytes_t; |
fabiombed | 8:144855fe02bd | 66 | typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; |
fabiombed | 8:144855fe02bd | 67 | |
fabiombed | 5:76fb6b783487 | 68 | typedef struct { |
fabiombed | 5:76fb6b783487 | 69 | int32_t AXIS_X; |
fabiombed | 5:76fb6b783487 | 70 | int32_t AXIS_Y; |
fabiombed | 5:76fb6b783487 | 71 | int32_t AXIS_Z; |
fabiombed | 5:76fb6b783487 | 72 | } AxesRaw_TypeDef; |
fabiombed | 5:76fb6b783487 | 73 | |
fabiombed | 5:76fb6b783487 | 74 | typedef enum ConnectionStatus_t { |
fabiombed | 5:76fb6b783487 | 75 | DISCONNECTED =0, |
fabiombed | 5:76fb6b783487 | 76 | CONNECTED =1 |
fabiombed | 8:144855fe02bd | 77 | } cns_t; |
fabiombed | 5:76fb6b783487 | 78 | |
fabiombed | 8:144855fe02bd | 79 | #define BLE_DEV_NAME "SunTracker" |
fabiombed | 8:144855fe02bd | 80 | #define BLE_DEV_MAC 0xAA,0xBB,0xCC,0xDD,0xEE,0xFF |
fabiombed | 8:144855fe02bd | 81 | #define BLE_ADVERTISING_INTERVAL 1000 |
fabiombed | 5:76fb6b783487 | 82 | |
fabiombed | 5:76fb6b783487 | 83 | #include "CustomControlService.h" |
fabiombed | 5:76fb6b783487 | 84 | #include "CustomSensorsService.h" |
fabiombed | 5:76fb6b783487 | 85 | |
fabiombed | 6:4cbf7303b496 | 86 | static BLE *p_BLEdev = NULL; |
fabiombed | 7:54984d031243 | 87 | static CustomControlService *p_customcontrolservice = NULL; |
fabiombed | 7:54984d031243 | 88 | static CustomSensorService *p_customsensorservice = NULL; |
fabiombed | 7:54984d031243 | 89 | |
fabiombed | 0:becf0d313663 | 90 | /* Definitions ---------------------------------------------------------------*/ |
fabiombed | 0:becf0d313663 | 91 | |
fabiombed | 1:8f312c1686b6 | 92 | #define SET_ACC 400 // Set Motor Acceleration |
fabiombed | 1:8f312c1686b6 | 93 | #define SET_DEC 400 // Set Motor Deceleration |
fabiombed | 1:8f312c1686b6 | 94 | #define SET_MAX 200 // Set Motor MaxSpeed |
fabiombed | 1:8f312c1686b6 | 95 | #define SET_MIN 100 // Set Motor MinSpeed |
fabiombed | 1:8f312c1686b6 | 96 | #define STOP 1000 // Set Motor Stop Position |
fabiombed | 1:8f312c1686b6 | 97 | #define TOLLERANCE 100 // Tollerance between Left and Right before Start Movement |
fabiombed | 1:8f312c1686b6 | 98 | #define RANGE_1 200 // Range 1 for Motor Speed |
fabiombed | 1:8f312c1686b6 | 99 | #define RANGE_2 500 // Range 2 for Motor Speed |
fabiombed | 0:becf0d313663 | 100 | |
fabiombed | 0:becf0d313663 | 101 | /* Variables -----------------------------------------------------------------*/ |
fabiombed | 0:becf0d313663 | 102 | |
fabiombed | 1:8f312c1686b6 | 103 | int16_t dir=0; // Motor Rotation Direction: 0 = Stop, 1 = Anticlockwise, 2 = Clockwise |
fabiombed | 1:8f312c1686b6 | 104 | int16_t changedir=0; // Change Direction: 0 = No, 1 = Yes |
fabiombed | 8:144855fe02bd | 105 | int16_t arrest=0; // Arrest: 0 = No, 1 = Yes |
fabiombed | 1:8f312c1686b6 | 106 | int16_t babybear=0; // Difference (in Lux) between Left and Right |
fabiombed | 1:8f312c1686b6 | 107 | int acc_data[3]; // Difference of Accelerometer |
fabiombed | 1:8f312c1686b6 | 108 | int16_t diff=0; // Abs of Babybear or Accelerometer difference |
fabiombed | 1:8f312c1686b6 | 109 | int16_t left=0; // Left Command for Rotate Direction |
fabiombed | 1:8f312c1686b6 | 110 | int16_t right=0; // Right Command for Rotate Direction |
fabiombed | 1:8f312c1686b6 | 111 | int16_t start=0; // Waiting User Button Push |
fabiombed | 1:8f312c1686b6 | 112 | int32_t pos=0; // Motor Position |
fabiombed | 1:8f312c1686b6 | 113 | char DisplayStr[5]; // Status Display |
fabiombed | 1:8f312c1686b6 | 114 | int16_t Display=0; // Shown on Display: 0 = Motor Speed, 1 = Solar Panel Value, 2 = Manual Control |
fabiombed | 1:8f312c1686b6 | 115 | int16_t status, status_t, status_b, status_l, status_r; // Babybear Status |
fabiombed | 1:8f312c1686b6 | 116 | |
fabiombed | 8:144855fe02bd | 117 | /* Initializations ------------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 118 | |
fabiombed | 8:144855fe02bd | 119 | // Initializing SPI bus |
fabiombed | 8:144855fe02bd | 120 | DevSPI dev_spi(D11, D12, D13); |
fabiombed | 8:144855fe02bd | 121 | |
fabiombed | 8:144855fe02bd | 122 | // Initializing I2C bus |
fabiombed | 8:144855fe02bd | 123 | DevI2C dev_i2c(D14, D15); |
fabiombed | 8:144855fe02bd | 124 | |
fabiombed | 8:144855fe02bd | 125 | // Initializing Motor Component IHM01A1 |
fabiombed | 8:144855fe02bd | 126 | static L6474 *motor; |
fabiombed | 8:144855fe02bd | 127 | |
fabiombed | 8:144855fe02bd | 128 | // Initializing Babybear Component 6180XA1 |
fabiombed | 8:144855fe02bd | 129 | static X_NUCLEO_6180XA1 *board; |
fabiombed | 8:144855fe02bd | 130 | MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right; |
fabiombed | 8:144855fe02bd | 131 | |
fabiombed | 8:144855fe02bd | 132 | // Initializing MEMS Component IKS01A1 |
fabiombed | 8:144855fe02bd | 133 | static X_NUCLEO_IKS01A1 *mems; |
fabiombed | 8:144855fe02bd | 134 | MotionSensor *accelerometer; |
fabiombed | 8:144855fe02bd | 135 | |
fabiombed | 8:144855fe02bd | 136 | InterruptIn mybutton(USER_BUTTON); |
fabiombed | 8:144855fe02bd | 137 | |
fabiombed | 9:ca289bf57f52 | 138 | AnalogIn analog_read(PC_3); // A1 Conflict with BLE SPI_CS del BLE --> Changed in Morpho PC_3 |
fabiombed | 8:144855fe02bd | 139 | |
fabiombed | 8:144855fe02bd | 140 | /* User_Button_Pressed -------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 141 | |
fabiombed | 8:144855fe02bd | 142 | void User_Button_Pressed() |
fabiombed | 8:144855fe02bd | 143 | { |
fabiombed | 8:144855fe02bd | 144 | |
fabiombed | 8:144855fe02bd | 145 | if (start>0) { Display++; } |
fabiombed | 8:144855fe02bd | 146 | if (Display>2) { Display=0; } |
fabiombed | 8:144855fe02bd | 147 | if (start==0) { start=1; } |
fabiombed | 8:144855fe02bd | 148 | |
fabiombed | 11:b5e23d64d366 | 149 | printf("PUSH Display %d\r\n", Display+1); |
fabiombed | 8:144855fe02bd | 150 | |
fabiombed | 8:144855fe02bd | 151 | } |
fabiombed | 8:144855fe02bd | 152 | |
fabiombed | 9:ca289bf57f52 | 153 | /* Bluetooth CallBack ---------------------------------------------------------*/ |
fabiombed | 9:ca289bf57f52 | 154 | |
fabiombed | 9:ca289bf57f52 | 155 | static void onUpdatesEnabledCallback(GattAttribute::Handle_t handle) |
fabiombed | 9:ca289bf57f52 | 156 | { |
fabiombed | 9:ca289bf57f52 | 157 | |
fabiombed | 9:ca289bf57f52 | 158 | } |
fabiombed | 9:ca289bf57f52 | 159 | |
fabiombed | 9:ca289bf57f52 | 160 | static void onUpdatesDisabledCallback(Gap::Handle_t handle) |
fabiombed | 9:ca289bf57f52 | 161 | { |
fabiombed | 9:ca289bf57f52 | 162 | |
fabiombed | 9:ca289bf57f52 | 163 | } |
fabiombed | 9:ca289bf57f52 | 164 | |
fabiombed | 9:ca289bf57f52 | 165 | static void onDataReadCallback(const GattReadCallbackParams *eventDataP) |
fabiombed | 9:ca289bf57f52 | 166 | { |
fabiombed | 9:ca289bf57f52 | 167 | |
fabiombed | 9:ca289bf57f52 | 168 | } |
fabiombed | 9:ca289bf57f52 | 169 | |
fabiombed | 9:ca289bf57f52 | 170 | static void myonDataWriteCallback(const GattWriteCallbackParams *eventDataP) |
fabiombed | 9:ca289bf57f52 | 171 | { |
fabiombed | 9:ca289bf57f52 | 172 | printf ("myonDataWriteCallback attr_handle: %x att_data[3]: %x data_length: %d\n\r", eventDataP->handle, eventDataP->data[3], eventDataP->len ); |
fabiombed | 9:ca289bf57f52 | 173 | |
fabiombed | 9:ca289bf57f52 | 174 | if (p_customcontrolservice->isContHandle(eventDataP->handle)) // Only if write come from this Service |
fabiombed | 9:ca289bf57f52 | 175 | { |
fabiombed | 9:ca289bf57f52 | 176 | uint8_t Command = eventDataP->data[3]; // Riceve 8 byte: data[0] + data[1] + data[2] + data[3] |
fabiombed | 9:ca289bf57f52 | 177 | printf("myonDataWriteCallback (Command %x)\r\n", Command); |
fabiombed | 9:ca289bf57f52 | 178 | } |
fabiombed | 9:ca289bf57f52 | 179 | } |
fabiombed | 9:ca289bf57f52 | 180 | |
fabiombed | 9:ca289bf57f52 | 181 | static void onConnectionCallback(const Gap::ConnectionCallbackParams_t * connectionParams) |
fabiombed | 9:ca289bf57f52 | 182 | { |
fabiombed | 9:ca289bf57f52 | 183 | printf("onConnectionCallback (Line %d)\r\n", __LINE__); |
fabiombed | 9:ca289bf57f52 | 184 | } |
fabiombed | 9:ca289bf57f52 | 185 | |
fabiombed | 9:ca289bf57f52 | 186 | static void onDisconnectionCallback(const Gap::DisconnectionCallbackParams_t * disConnectionReason) |
fabiombed | 9:ca289bf57f52 | 187 | { |
fabiombed | 9:ca289bf57f52 | 188 | printf("onDisconnectionCallback (Line %d)\r\n", __LINE__); |
fabiombed | 9:ca289bf57f52 | 189 | p_BLEdev->gap().startAdvertising(); |
fabiombed | 9:ca289bf57f52 | 190 | } |
fabiombed | 9:ca289bf57f52 | 191 | |
fabiombed | 9:ca289bf57f52 | 192 | /* Bluetooth Initialization ---------------------------------------------------*/ |
fabiombed | 9:ca289bf57f52 | 193 | |
fabiombed | 9:ca289bf57f52 | 194 | bool BLE_Initialization(void) |
fabiombed | 9:ca289bf57f52 | 195 | { |
fabiombed | 9:ca289bf57f52 | 196 | |
fabiombed | 9:ca289bf57f52 | 197 | p_BLEdev = new BLE; |
fabiombed | 9:ca289bf57f52 | 198 | if (!p_BLEdev) { printf("\r\nBLE Device creation failed\r\n"); } |
fabiombed | 9:ca289bf57f52 | 199 | const Gap::Address_t BLE_address_BE = {BLE_DEV_MAC}; |
fabiombed | 9:ca289bf57f52 | 200 | p_BLEdev->gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE); |
fabiombed | 9:ca289bf57f52 | 201 | |
fabiombed | 9:ca289bf57f52 | 202 | p_BLEdev->init(); |
fabiombed | 9:ca289bf57f52 | 203 | |
fabiombed | 9:ca289bf57f52 | 204 | // Set BLE CallBack Functions |
fabiombed | 9:ca289bf57f52 | 205 | p_BLEdev->gattServer().onUpdatesEnabled(onUpdatesEnabledCallback); |
fabiombed | 9:ca289bf57f52 | 206 | p_BLEdev->gattServer().onUpdatesDisabled(onUpdatesDisabledCallback); |
fabiombed | 9:ca289bf57f52 | 207 | p_BLEdev->gattServer().onDataRead(onDataReadCallback); |
fabiombed | 9:ca289bf57f52 | 208 | p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback); |
fabiombed | 9:ca289bf57f52 | 209 | p_BLEdev->gap().onConnection(onConnectionCallback); |
fabiombed | 9:ca289bf57f52 | 210 | p_BLEdev->gap().onDisconnection(onDisconnectionCallback); |
fabiombed | 9:ca289bf57f52 | 211 | //p_BLEdev->gattServer().onConfirmationReceived(onConfirmationReceivedCallback); |
fabiombed | 9:ca289bf57f52 | 212 | //p_BLEdev->gattServer().onDataSent(onDataSentCallback); |
fabiombed | 9:ca289bf57f52 | 213 | //p_BLEdev->gap().onTimeout(onTimeoutCallback); |
fabiombed | 9:ca289bf57f52 | 214 | |
fabiombed | 9:ca289bf57f52 | 215 | // BLE Services |
fabiombed | 9:ca289bf57f52 | 216 | p_customcontrolservice = new CustomControlService(*p_BLEdev); |
fabiombed | 9:ca289bf57f52 | 217 | p_customsensorservice = new CustomSensorService(*p_BLEdev); |
fabiombed | 9:ca289bf57f52 | 218 | |
fabiombed | 9:ca289bf57f52 | 219 | // Setup BLE Advertising |
fabiombed | 9:ca289bf57f52 | 220 | const static char DEVICE_NAME[] = BLE_DEV_NAME; |
fabiombed | 9:ca289bf57f52 | 221 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
fabiombed | 9:ca289bf57f52 | 222 | uint8_t dat[] = {0x01,0x80,0x00,0xFC,0x00,0x00}; |
fabiombed | 9:ca289bf57f52 | 223 | p_BLEdev->gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,dat,6); |
fabiombed | 9:ca289bf57f52 | 224 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); |
fabiombed | 9:ca289bf57f52 | 225 | p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
fabiombed | 9:ca289bf57f52 | 226 | p_BLEdev->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
fabiombed | 9:ca289bf57f52 | 227 | p_BLEdev->gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL); |
fabiombed | 9:ca289bf57f52 | 228 | p_BLEdev->gap().startAdvertising(); |
fabiombed | 9:ca289bf57f52 | 229 | |
fabiombed | 9:ca289bf57f52 | 230 | return true; |
fabiombed | 9:ca289bf57f52 | 231 | |
fabiombed | 9:ca289bf57f52 | 232 | } |
fabiombed | 9:ca289bf57f52 | 233 | |
fabiombed | 8:144855fe02bd | 234 | /* Initialization ------------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 235 | |
fabiombed | 8:144855fe02bd | 236 | bool Initialization(void) |
fabiombed | 8:144855fe02bd | 237 | { |
fabiombed | 11:b5e23d64d366 | 238 | |
fabiombed | 11:b5e23d64d366 | 239 | dev_i2c.frequency(100000); // at 200KHz or 400KHz doesn't work |
fabiombed | 11:b5e23d64d366 | 240 | |
fabiombed | 11:b5e23d64d366 | 241 | //---- |
fabiombed | 11:b5e23d64d366 | 242 | |
fabiombed | 8:144855fe02bd | 243 | // Initializing Babybear Component |
fabiombed | 11:b5e23d64d366 | 244 | board=X_NUCLEO_6180XA1::Instance(&dev_i2c, PA_4, PA_4, PA_4, PA_4); // Don't use NC because it doesn't set InterruptIn correctly |
fabiombed | 8:144855fe02bd | 245 | |
fabiombed | 9:ca289bf57f52 | 246 | status=board->InitBoard(); |
fabiombed | 9:ca289bf57f52 | 247 | if(status) VL6180x_ErrLog("Failed to init the board!\n\r"); |
fabiombed | 8:144855fe02bd | 248 | |
fabiombed | 8:144855fe02bd | 249 | // Put GPIO not used as Interrupt in Hi-Z |
fabiombed | 8:144855fe02bd | 250 | status_t=board->sensor_top->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 8:144855fe02bd | 251 | //status_b=board->sensor_botton->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); No Present |
fabiombed | 8:144855fe02bd | 252 | status_l=board->sensor_left->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 8:144855fe02bd | 253 | status_r=board->sensor_right->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); |
fabiombed | 8:144855fe02bd | 254 | |
fabiombed | 8:144855fe02bd | 255 | // Set Babybears |
fabiombed | 8:144855fe02bd | 256 | status_l=board->sensor_left->AlsSetAnalogueGain(3); |
fabiombed | 8:144855fe02bd | 257 | status_r=board->sensor_right->AlsSetAnalogueGain(3); |
fabiombed | 8:144855fe02bd | 258 | status_l=board->sensor_left->StartMeasurement(als_continuous_polling, NULL, NULL, NULL); |
fabiombed | 8:144855fe02bd | 259 | status_r=board->sensor_right->StartMeasurement(als_continuous_polling, NULL, NULL, NULL); |
fabiombed | 8:144855fe02bd | 260 | |
fabiombed | 11:b5e23d64d366 | 261 | printf("Init Babybear OK\r\n"); |
fabiombed | 11:b5e23d64d366 | 262 | |
fabiombed | 8:144855fe02bd | 263 | //---- |
fabiombed | 8:144855fe02bd | 264 | |
fabiombed | 11:b5e23d64d366 | 265 | // Initializing MEMS Component |
fabiombed | 11:b5e23d64d366 | 266 | mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
fabiombed | 11:b5e23d64d366 | 267 | accelerometer = mems->GetAccelerometer(); |
fabiombed | 11:b5e23d64d366 | 268 | |
fabiombed | 11:b5e23d64d366 | 269 | printf("Init MEMS OK\r\n"); |
fabiombed | 11:b5e23d64d366 | 270 | |
fabiombed | 11:b5e23d64d366 | 271 | //---- |
fabiombed | 11:b5e23d64d366 | 272 | |
fabiombed | 8:144855fe02bd | 273 | // Initializing Motor Component |
fabiombed | 9:ca289bf57f52 | 274 | motor = new L6474(D2, D8, PB_2, D9, D10, dev_spi); // D7 conflict with BNRG_RST (when motor change direction 0-1 reset BLE) |
fabiombed | 9:ca289bf57f52 | 275 | // Used Morpho PB_2 and made HW wiring |
fabiombed | 8:144855fe02bd | 276 | if (motor->Init(NULL) != COMPONENT_OK) |
fabiombed | 8:144855fe02bd | 277 | return false; |
fabiombed | 8:144855fe02bd | 278 | |
fabiombed | 8:144855fe02bd | 279 | motor->SetStepMode(STEP_MODE_1_8); // Default is STEP_MODE_1_16 |
fabiombed | 8:144855fe02bd | 280 | |
fabiombed | 8:144855fe02bd | 281 | // Set defaults Motor Speed |
fabiombed | 8:144855fe02bd | 282 | motor->SetAcceleration(SET_ACC); |
fabiombed | 8:144855fe02bd | 283 | motor->SetDeceleration(SET_DEC); |
fabiombed | 8:144855fe02bd | 284 | motor->SetMaxSpeed(SET_MAX); // Variable by Light/Mems Sensors |
fabiombed | 8:144855fe02bd | 285 | motor->SetMinSpeed(SET_MIN); |
fabiombed | 8:144855fe02bd | 286 | |
fabiombed | 11:b5e23d64d366 | 287 | printf("Init Motor OK\r\n"); |
fabiombed | 11:b5e23d64d366 | 288 | |
fabiombed | 8:144855fe02bd | 289 | return true; |
fabiombed | 8:144855fe02bd | 290 | |
fabiombed | 8:144855fe02bd | 291 | } |
fabiombed | 8:144855fe02bd | 292 | |
fabiombed | 8:144855fe02bd | 293 | /* Measure_Babybear ----------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 294 | |
fabiombed | 8:144855fe02bd | 295 | void Measure_Babybear(void) |
fabiombed | 8:144855fe02bd | 296 | { |
fabiombed | 8:144855fe02bd | 297 | |
fabiombed | 8:144855fe02bd | 298 | status_l=board->sensor_left->GetMeasurement(als_continuous_polling, &data_sensor_left); |
fabiombed | 8:144855fe02bd | 299 | status_r=board->sensor_right->GetMeasurement(als_continuous_polling, &data_sensor_right); |
fabiombed | 8:144855fe02bd | 300 | |
fabiombed | 8:144855fe02bd | 301 | babybear = data_sensor_right.lux - data_sensor_left.lux; |
fabiombed | 8:144855fe02bd | 302 | |
fabiombed | 8:144855fe02bd | 303 | diff = abs(babybear); |
fabiombed | 1:8f312c1686b6 | 304 | |
fabiombed | 8:144855fe02bd | 305 | if (babybear>0) { left=0; right=1; } |
fabiombed | 8:144855fe02bd | 306 | if (babybear<0) { left=1; right=0; } |
fabiombed | 8:144855fe02bd | 307 | |
fabiombed | 8:144855fe02bd | 308 | } |
fabiombed | 8:144855fe02bd | 309 | |
fabiombed | 8:144855fe02bd | 310 | /* Measure_Accelerometer -----------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 311 | |
fabiombed | 8:144855fe02bd | 312 | void Measure_Accelerometer(void) |
fabiombed | 8:144855fe02bd | 313 | { |
fabiombed | 8:144855fe02bd | 314 | |
fabiombed | 8:144855fe02bd | 315 | accelerometer->Get_X_Axes(acc_data); |
fabiombed | 8:144855fe02bd | 316 | |
fabiombed | 8:144855fe02bd | 317 | diff = abs(acc_data[0]); |
fabiombed | 8:144855fe02bd | 318 | |
fabiombed | 8:144855fe02bd | 319 | if (acc_data[0]>0) { left=0; right=1; } |
fabiombed | 8:144855fe02bd | 320 | if (acc_data[0]<0) { left=1; right=0; } |
fabiombed | 8:144855fe02bd | 321 | |
fabiombed | 8:144855fe02bd | 322 | } |
fabiombed | 8:144855fe02bd | 323 | |
fabiombed | 8:144855fe02bd | 324 | /* Control_Motor -------------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 325 | |
fabiombed | 8:144855fe02bd | 326 | void Control_Motor(void) |
fabiombed | 8:144855fe02bd | 327 | { |
fabiombed | 8:144855fe02bd | 328 | |
fabiombed | 8:144855fe02bd | 329 | //printf("Diff: %d lux/mems\n\r", diff); |
fabiombed | 8:144855fe02bd | 330 | motor->SetMaxSpeed(diff); |
fabiombed | 8:144855fe02bd | 331 | |
fabiombed | 8:144855fe02bd | 332 | if (diff>TOLLERANCE) { |
fabiombed | 8:144855fe02bd | 333 | if (diff <=RANGE_1) |
fabiombed | 8:144855fe02bd | 334 | { |
fabiombed | 8:144855fe02bd | 335 | if (left) { strcpy(DisplayStr,"E___"); } |
fabiombed | 8:144855fe02bd | 336 | if (right) { strcpy(DisplayStr,"___3"); } |
fabiombed | 8:144855fe02bd | 337 | } |
fabiombed | 8:144855fe02bd | 338 | else if (diff >RANGE_1 & diff <=RANGE_2) |
fabiombed | 8:144855fe02bd | 339 | { |
fabiombed | 8:144855fe02bd | 340 | if (left) { strcpy(DisplayStr,"E==="); } |
fabiombed | 8:144855fe02bd | 341 | if (right) { strcpy(DisplayStr,"===3"); } |
fabiombed | 8:144855fe02bd | 342 | } |
fabiombed | 8:144855fe02bd | 343 | else if (diff >RANGE_2) |
fabiombed | 8:144855fe02bd | 344 | { |
fabiombed | 8:144855fe02bd | 345 | if (left) { strcpy(DisplayStr,"E~~~"); } |
fabiombed | 8:144855fe02bd | 346 | if (right) { strcpy(DisplayStr,"~~~3"); } |
fabiombed | 8:144855fe02bd | 347 | } |
fabiombed | 8:144855fe02bd | 348 | |
fabiombed | 8:144855fe02bd | 349 | // In Case of Change Direction |
fabiombed | 8:144855fe02bd | 350 | if (left & dir==2) { changedir=1; } |
fabiombed | 8:144855fe02bd | 351 | if (right & dir==1) { changedir=1; } |
fabiombed | 0:becf0d313663 | 352 | |
fabiombed | 8:144855fe02bd | 353 | // Run only if Stop or Change Direction |
fabiombed | 8:144855fe02bd | 354 | if (diff>TOLLERANCE & (dir==0 | changedir==1)) { |
fabiombed | 8:144855fe02bd | 355 | if (left) { motor->Run(StepperMotor::FWD); dir=1; changedir=0; } |
fabiombed | 8:144855fe02bd | 356 | if (right) { motor->Run(StepperMotor::BWD); dir=2; changedir=0; } |
fabiombed | 8:144855fe02bd | 357 | printf("RUN\n\r"); |
fabiombed | 8:144855fe02bd | 358 | } |
fabiombed | 8:144855fe02bd | 359 | } |
fabiombed | 8:144855fe02bd | 360 | |
fabiombed | 8:144855fe02bd | 361 | // Get Motor Position and Control Rotation Block |
fabiombed | 8:144855fe02bd | 362 | pos = motor->GetPosition(); |
fabiombed | 8:144855fe02bd | 363 | if (pos>STOP | pos<-STOP) { |
fabiombed | 8:144855fe02bd | 364 | if (pos>0) { motor->GoTo(STOP); } |
fabiombed | 8:144855fe02bd | 365 | if (pos<0) { motor->GoTo(-STOP); } |
fabiombed | 8:144855fe02bd | 366 | printf("GOTO\n\r"); |
fabiombed | 8:144855fe02bd | 367 | } |
fabiombed | 1:8f312c1686b6 | 368 | |
fabiombed | 8:144855fe02bd | 369 | // Stop Motor |
fabiombed | 8:144855fe02bd | 370 | if (diff<=TOLLERANCE) { |
fabiombed | 8:144855fe02bd | 371 | arrest=1; |
fabiombed | 8:144855fe02bd | 372 | if (Display==0) { strcpy(DisplayStr,"----"); } |
fabiombed | 8:144855fe02bd | 373 | if (Display==2) { strcpy(DisplayStr,"E 3"); } |
fabiombed | 8:144855fe02bd | 374 | } |
fabiombed | 8:144855fe02bd | 375 | |
fabiombed | 8:144855fe02bd | 376 | if (arrest==1 && dir!=0 ) { // Stop just one time |
fabiombed | 8:144855fe02bd | 377 | motor->HardStop(); |
fabiombed | 8:144855fe02bd | 378 | dir=0; |
fabiombed | 8:144855fe02bd | 379 | changedir=0; |
fabiombed | 8:144855fe02bd | 380 | arrest=0; |
fabiombed | 8:144855fe02bd | 381 | printf("STOP\n\r"); |
fabiombed | 8:144855fe02bd | 382 | } |
fabiombed | 8:144855fe02bd | 383 | |
fabiombed | 8:144855fe02bd | 384 | } |
fabiombed | 8:144855fe02bd | 385 | |
fabiombed | 8:144855fe02bd | 386 | /* Measure_SolarPanel --------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 387 | |
fabiombed | 8:144855fe02bd | 388 | void Measure_SolarPanel(void) |
fabiombed | 8:144855fe02bd | 389 | { |
fabiombed | 8:144855fe02bd | 390 | |
fabiombed | 8:144855fe02bd | 391 | // AnalogIn: 0V return 0.0 , 3.3V return 1.0 |
fabiombed | 9:ca289bf57f52 | 392 | float measure = analog_read.read() * 3300; |
fabiombed | 8:144855fe02bd | 393 | //printf("Measure = %.0f mV\r\n", measure); |
fabiombed | 8:144855fe02bd | 394 | //board->display->DisplayDigit("A", 0); |
fabiombed | 8:144855fe02bd | 395 | |
fabiombed | 8:144855fe02bd | 396 | if (Display==1) { sprintf(DisplayStr, "%.0f", measure); } |
fabiombed | 8:144855fe02bd | 397 | |
fabiombed | 8:144855fe02bd | 398 | board->display->DisplayString(DisplayStr, 4); |
fabiombed | 8:144855fe02bd | 399 | //printf("%s\n\r", DisplayStr); |
fabiombed | 8:144855fe02bd | 400 | |
fabiombed | 8:144855fe02bd | 401 | } |
fabiombed | 1:8f312c1686b6 | 402 | |
fabiombed | 8:144855fe02bd | 403 | /* Main ----------------------------------------------------------------------*/ |
fabiombed | 8:144855fe02bd | 404 | |
fabiombed | 8:144855fe02bd | 405 | int main() |
fabiombed | 8:144855fe02bd | 406 | { |
fabiombed | 8:144855fe02bd | 407 | |
fabiombed | 8:144855fe02bd | 408 | // Printing to the console |
fabiombed | 8:144855fe02bd | 409 | printf("SunTracker by Fabio Brembilla\r\n\n"); |
fabiombed | 11:b5e23d64d366 | 410 | |
fabiombed | 8:144855fe02bd | 411 | Initialization(); |
fabiombed | 8:144855fe02bd | 412 | printf("Initialization OK (Line %d)\r\n", __LINE__); |
fabiombed | 7:54984d031243 | 413 | |
fabiombed | 11:b5e23d64d366 | 414 | BLE_Initialization(); |
fabiombed | 11:b5e23d64d366 | 415 | printf("BLE_Initialization OK (Line %d)\r\n", __LINE__); |
fabiombed | 8:144855fe02bd | 416 | |
fabiombed | 8:144855fe02bd | 417 | mybutton.fall(&User_Button_Pressed); |
fabiombed | 8:144855fe02bd | 418 | printf("Wait Push Button\r\n"); |
fabiombed | 7:54984d031243 | 419 | |
fabiombed | 8:144855fe02bd | 420 | // Loop until push User Button to Set 0 Point |
fabiombed | 8:144855fe02bd | 421 | strcpy(DisplayStr,"pusH"); |
fabiombed | 8:144855fe02bd | 422 | while(start<1) { |
fabiombed | 8:144855fe02bd | 423 | board->display->DisplayString(DisplayStr, 4); |
fabiombed | 9:ca289bf57f52 | 424 | //p_BLEdev->waitForEvent(); |
fabiombed | 8:144855fe02bd | 425 | //printf("%s\n\r", DisplayStr); |
fabiombed | 8:144855fe02bd | 426 | } |
fabiombed | 4:1d3d071a4c2c | 427 | |
fabiombed | 6:4cbf7303b496 | 428 | printf("Start Main Loop\r\n"); |
fabiombed | 6:4cbf7303b496 | 429 | |
fabiombed | 10:d154094297ce | 430 | motor->Enable(); // To put the motor on hold execute CmdEnable |
fabiombed | 10:d154094297ce | 431 | |
fabiombed | 11:b5e23d64d366 | 432 | static int INTLOOP=0; |
fabiombed | 7:54984d031243 | 433 | |
fabiombed | 6:4cbf7303b496 | 434 | // Main Loop |
fabiombed | 8:144855fe02bd | 435 | while(true) |
fabiombed | 8:144855fe02bd | 436 | { |
fabiombed | 8:144855fe02bd | 437 | if (Display==0 | Display==1) { Measure_Babybear(); } |
fabiombed | 8:144855fe02bd | 438 | if (Display==2) { Measure_Accelerometer(); } |
fabiombed | 8:144855fe02bd | 439 | |
fabiombed | 8:144855fe02bd | 440 | Control_Motor(); |
fabiombed | 8:144855fe02bd | 441 | Measure_SolarPanel(); |
fabiombed | 11:b5e23d64d366 | 442 | |
fabiombed | 11:b5e23d64d366 | 443 | INTLOOP++; |
fabiombed | 11:b5e23d64d366 | 444 | if (INTLOOP==100) { p_customsensorservice->sendEnvPosition(rand(),0); printf("Read BLE\n\r"); INTLOOP=0; } |
fabiombed | 7:54984d031243 | 445 | |
fabiombed | 11:b5e23d64d366 | 446 | p_BLEdev->waitForEvent(); |
fabiombed | 8:144855fe02bd | 447 | } |
fabiombed | 4:1d3d071a4c2c | 448 | |
fabiombed | 8:144855fe02bd | 449 | //status_l=board->sensor_left->StopMeasurement(als_continuous_polling); |
fabiombed | 8:144855fe02bd | 450 | //status_r=board->sensor_right->StopMeasurement(als_continuous_polling); |
fabiombed | 4:1d3d071a4c2c | 451 | |
fabiombed | 8:144855fe02bd | 452 | } |