SunTracker_BLE

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 X_NUCLEO_IHM01A1 X_NUCLEO_IKS01A1 mbed

Fork of SunTracker_BLE by ST Expansion SW Team

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.

/media/uploads/fabiombed/suntracker_server-client.png

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 structure is 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 efficiency with 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

  • 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

MECHANICAL STRUCTURE

Find here the STL files to print with a 3D printer.

/media/uploads/fabiombed/assembly.png

/media/uploads/fabiombed/mechanical_structure_and_motor_legs.png

FLAT CABLE ASSEMBLY

/media/uploads/fabiombed/flat_cable.png

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

/media/uploads/fabiombed/arduino_pinout.png /media/uploads/fabiombed/morpho_pinout.png

Committer:
mapellil
Date:
Mon May 23 10:05:08 2016 +0000
Revision:
25:00220cf2e628
Parent:
21:6948443b29e6
Update link to X_NUCLEO_6180XA1 library (inherits from Component class)

Who changed what in which revision?

UserRevisionLine numberNew 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 19:b2c04428ffed 6 * @date May, 2016
fabiombed 12:1e6a1d3a66b5 7 * @brief SunTracker + BLE (Server) 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>&copy; 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 14:644f9e7278e9 39 /* Define --------------------------------------------------------------------*/
fabiombed 14:644f9e7278e9 40
fabiombed 14:644f9e7278e9 41 #define FlightSense //6180XA1 Main (remove only for debug)
fabiombed 14:644f9e7278e9 42 #define EasySpin //IHM01A1 Main (remove only for debug)
fabiombed 14:644f9e7278e9 43 #define Sensors //IKS01A1 Option
fabiombed 14:644f9e7278e9 44 #define Ble //IDB0XA1 Option
fabiombed 21:6948443b29e6 45 //#define Debug //Verbose about Printf
fabiombed 14:644f9e7278e9 46
fabiombed 0:becf0d313663 47 /* Includes ------------------------------------------------------------------*/
fabiombed 4:1d3d071a4c2c 48
fabiombed 6:4cbf7303b496 49 // Mbed specific header files
fabiombed 0:becf0d313663 50 #include "mbed.h"
fabiombed 4:1d3d071a4c2c 51
fabiombed 6:4cbf7303b496 52 // Helper header files
fabiombed 0:becf0d313663 53 #include "DevSPI.h"
fabiombed 1:8f312c1686b6 54 #include "DevI2C.h"
fabiombed 0:becf0d313663 55
fabiombed 6:4cbf7303b496 56 // Component specific header files
fabiombed 14:644f9e7278e9 57 #ifdef FlightSense
fabiombed 14:644f9e7278e9 58 #include "x_nucleo_6180xa1.h"
fabiombed 14:644f9e7278e9 59 #endif
fabiombed 14:644f9e7278e9 60
fabiombed 14:644f9e7278e9 61 #ifdef EasySpin
fabiombed 14:644f9e7278e9 62 #include "l6474_class.h"
fabiombed 14:644f9e7278e9 63 #endif
fabiombed 14:644f9e7278e9 64
fabiombed 14:644f9e7278e9 65 #ifdef Sensors
fabiombed 14:644f9e7278e9 66 #include "x_nucleo_iks01a1.h"
fabiombed 14:644f9e7278e9 67 #endif
fabiombed 0:becf0d313663 68
fabiombed 6:4cbf7303b496 69 // C header files
fabiombed 1:8f312c1686b6 70 #include <string.h>
fabiombed 1:8f312c1686b6 71 #include <stdlib.h>
fabiombed 1:8f312c1686b6 72 #include <stdio.h>
fabiombed 1:8f312c1686b6 73 #include <assert.h>
fabiombed 0:becf0d313663 74
fabiombed 5:76fb6b783487 75 /* BlueTooth -----------------------------------------------------------------*/
fabiombed 5:76fb6b783487 76
fabiombed 14:644f9e7278e9 77 #ifdef Ble
fabiombed 14:644f9e7278e9 78
fabiombed 5:76fb6b783487 79 #include "debug.h" // Need for PRINTF
fabiombed 14:644f9e7278e9 80 #include "Utils.h" // Need for STORE_LE_16 and STORE_LE_32
fabiombed 5:76fb6b783487 81
fabiombed 17:582eba752042 82 // Not need to re-define it, it is already defined into UUID.h, just use UUID:: when you call it
fabiombed 17:582eba752042 83 //const unsigned LENGTH_OF_LONG_UUID = 16;
fabiombed 17:582eba752042 84 //typedef uint16_t ShortUUIDBytes_t;
fabiombed 17:582eba752042 85 //typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
fabiombed 8:144855fe02bd 86
fabiombed 5:76fb6b783487 87 typedef enum ConnectionStatus_t {
fabiombed 5:76fb6b783487 88 DISCONNECTED =0,
fabiombed 5:76fb6b783487 89 CONNECTED =1
fabiombed 8:144855fe02bd 90 } cns_t;
fabiombed 5:76fb6b783487 91
fabiombed 8:144855fe02bd 92 #define BLE_DEV_NAME "SunTracker"
fabiombed 19:b2c04428ffed 93 #define BLE_DEV_MAC 0xFC,0x03,0x02,0x01,0xFB,0xFA // SERVER address (must be set into CLIENT BLE_peer_address_BE)
fabiombed 19:b2c04428ffed 94 #define BLE_ADVERTISING_INTERVAL 100
fabiombed 19:b2c04428ffed 95 uint8_t dat[] = {0x01,0x80,0x00,0x00,0x78,0x00}; // Data Sent during Advertising, must respect a Standard ST
fabiombed 19:b2c04428ffed 96
fabiombed 5:76fb6b783487 97 #include "CustomControlService.h"
fabiombed 5:76fb6b783487 98 #include "CustomSensorsService.h"
fabiombed 5:76fb6b783487 99
fabiombed 6:4cbf7303b496 100 static BLE *p_BLEdev = NULL;
fabiombed 7:54984d031243 101 static CustomControlService *p_customcontrolservice = NULL;
fabiombed 7:54984d031243 102 static CustomSensorService *p_customsensorservice = NULL;
fabiombed 7:54984d031243 103
fabiombed 18:319a1bb8f837 104 #define FeatureStatus 0x00000800
fabiombed 18:319a1bb8f837 105 #define FeatureDifference 0x00001000
fabiombed 18:319a1bb8f837 106 #define FeaturePosition 0x00002000
fabiombed 18:319a1bb8f837 107 #define FeatureSunPanel 0x00004000
fabiombed 15:019b8d60c89d 108
fabiombed 14:644f9e7278e9 109 #endif
fabiombed 14:644f9e7278e9 110
fabiombed 0:becf0d313663 111 /* Definitions ---------------------------------------------------------------*/
fabiombed 0:becf0d313663 112
fabiombed 1:8f312c1686b6 113 #define SET_ACC 400 // Set Motor Acceleration
fabiombed 1:8f312c1686b6 114 #define SET_DEC 400 // Set Motor Deceleration
fabiombed 1:8f312c1686b6 115 #define SET_MAX 200 // Set Motor MaxSpeed
fabiombed 1:8f312c1686b6 116 #define SET_MIN 100 // Set Motor MinSpeed
fabiombed 1:8f312c1686b6 117 #define STOP 1000 // Set Motor Stop Position
fabiombed 18:319a1bb8f837 118 #define TOLLERANCE 80 // Tollerance between Left and Right before Start Movement
fabiombed 18:319a1bb8f837 119 #define RANGE_1 250 // Range 1 for Motor Speed
fabiombed 1:8f312c1686b6 120 #define RANGE_2 500 // Range 2 for Motor Speed
fabiombed 19:b2c04428ffed 121 #define TIMEOUT_STA 0.5 // Timeout for Ticker Status (in second)
fabiombed 19:b2c04428ffed 122 #define TIMEOUT_DIF 0.1 // Timeout for Ticker Difference (in second)
fabiombed 19:b2c04428ffed 123 #define TIMEOUT_POS 0.1 // Timeout for Ticker Position (in second)
fabiombed 18:319a1bb8f837 124 #define TIMEOUT_SUN 0.5 // Timeout for Ticker SunPanel (in second)
fabiombed 0:becf0d313663 125
fabiombed 0:becf0d313663 126 /* Variables -----------------------------------------------------------------*/
fabiombed 0:becf0d313663 127
fabiombed 14:644f9e7278e9 128 int8_t dir=0; // Motor Rotation Direction: 0 = Stop, 1 = Anticlockwise, 2 = Clockwise
fabiombed 14:644f9e7278e9 129 int8_t changedir=0; // Change Direction: 0 = No, 1 = Yes
fabiombed 14:644f9e7278e9 130 int8_t arrest=0; // Arrest: 0 = No, 1 = Yes
fabiombed 15:019b8d60c89d 131 int16_t babybear=0; // Difference (in Lux) between Left and Right
fabiombed 14:644f9e7278e9 132 int32_t acc_data[3]; // Difference of Accelerometer
fabiombed 15:019b8d60c89d 133 int16_t diff=0; // Babybear or Accelerometer difference [--> Send BLE]
fabiombed 15:019b8d60c89d 134 int16_t diff_abs=0; // Abs of Babybear or Accelerometer difference
fabiombed 14:644f9e7278e9 135 int8_t left=0; // Left Command for Rotate Direction
fabiombed 14:644f9e7278e9 136 int8_t right=0; // Right Command for Rotate Direction
fabiombed 15:019b8d60c89d 137 int16_t pos=0; // Motor Position [--> Send BLE]
fabiombed 15:019b8d60c89d 138 int16_t measure=0; // ADC Value from SunPanel [--> Send BLE]
fabiombed 17:582eba752042 139 char DisplayStr[5]; // Display String
fabiombed 17:582eba752042 140 int16_t Status=0; // Status Shown on Display: 0 = Idle, 1 = Motor Speed, 2 = Solar Panel Value, 3 = Manual Control [--> Send BLE]
fabiombed 17:582eba752042 141 int16_t status_bb, status_t, status_b, status_l, status_r; // Babybear Status
fabiombed 17:582eba752042 142 uint16_t TimeStamp=0; // TimeStamp for BLE
fabiombed 18:319a1bb8f837 143 bool tickerSta=false;
fabiombed 18:319a1bb8f837 144 bool tickerDif=false;
fabiombed 18:319a1bb8f837 145 bool tickerPos=false;
fabiombed 18:319a1bb8f837 146 bool tickerSun=false;
fabiombed 19:b2c04428ffed 147 bool BLEConnected=false;// BLE Connection Status
fabiombed 1:8f312c1686b6 148
fabiombed 8:144855fe02bd 149 /* Initializations ------------------------------------------------------------*/
fabiombed 8:144855fe02bd 150
fabiombed 8:144855fe02bd 151 // Initializing SPI bus
fabiombed 8:144855fe02bd 152 DevSPI dev_spi(D11, D12, D13);
fabiombed 8:144855fe02bd 153
fabiombed 8:144855fe02bd 154 // Initializing I2C bus
fabiombed 8:144855fe02bd 155 DevI2C dev_i2c(D14, D15);
fabiombed 8:144855fe02bd 156
fabiombed 14:644f9e7278e9 157 #ifdef FlightSense
fabiombed 14:644f9e7278e9 158 // Initializing FlightSense Component 6180XA1
fabiombed 8:144855fe02bd 159 static X_NUCLEO_6180XA1 *board;
fabiombed 8:144855fe02bd 160 MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right;
fabiombed 14:644f9e7278e9 161 #endif
fabiombed 14:644f9e7278e9 162
fabiombed 14:644f9e7278e9 163 #ifdef EasySpin
fabiombed 14:644f9e7278e9 164 // Initializing EasySpin Component IHM01A1
fabiombed 14:644f9e7278e9 165 static L6474 *motor;
fabiombed 14:644f9e7278e9 166 #endif
fabiombed 14:644f9e7278e9 167
fabiombed 14:644f9e7278e9 168 #ifdef Sensors
fabiombed 14:644f9e7278e9 169 // Initializing Sensors Component IKS01A1
fabiombed 8:144855fe02bd 170 static X_NUCLEO_IKS01A1 *mems;
fabiombed 8:144855fe02bd 171 MotionSensor *accelerometer;
fabiombed 14:644f9e7278e9 172 #endif
fabiombed 8:144855fe02bd 173
fabiombed 8:144855fe02bd 174 InterruptIn mybutton(USER_BUTTON);
fabiombed 8:144855fe02bd 175
fabiombed 14:644f9e7278e9 176 AnalogIn analog_read(PC_3); // A1 Conflict with BLE SPI_CS --> Changed in Morpho PC_3
fabiombed 8:144855fe02bd 177
fabiombed 17:582eba752042 178 /* Update_Status ------------------------------------------------------------*/
fabiombed 17:582eba752042 179
fabiombed 17:582eba752042 180 void Update_Status(int16_t Stat)
fabiombed 17:582eba752042 181 {
fabiombed 17:582eba752042 182
fabiombed 17:582eba752042 183 char State[12]; // Characters into State must be <12 and never =>12 or it crash!!!
fabiombed 17:582eba752042 184 if (Stat==1) strcpy(State,"FlightSense");
fabiombed 17:582eba752042 185 if (Stat==2) strcpy(State,"ADCSunPanel");
fabiombed 17:582eba752042 186 if (Stat==3) strcpy(State,"Sensors");
fabiombed 17:582eba752042 187 printf("\r\n\r\nPUSH Status: %s", State);
fabiombed 17:582eba752042 188
fabiombed 17:582eba752042 189 }
fabiombed 17:582eba752042 190
fabiombed 8:144855fe02bd 191 /* User_Button_Pressed -------------------------------------------------------*/
fabiombed 8:144855fe02bd 192
fabiombed 14:644f9e7278e9 193 void User_Button_Pressed(void)
fabiombed 8:144855fe02bd 194 {
fabiombed 8:144855fe02bd 195
fabiombed 17:582eba752042 196 Status++;
fabiombed 17:582eba752042 197
fabiombed 19:b2c04428ffed 198 #ifdef Sensors //#ifdef Ble
fabiombed 17:582eba752042 199 if (Status>3) { Status=1; }
fabiombed 14:644f9e7278e9 200 #else
fabiombed 17:582eba752042 201 if (Status>2) { Status=1; }
fabiombed 14:644f9e7278e9 202 #endif
fabiombed 14:644f9e7278e9 203
fabiombed 17:582eba752042 204 Update_Status(Status);
fabiombed 8:144855fe02bd 205
fabiombed 8:144855fe02bd 206 }
fabiombed 8:144855fe02bd 207
fabiombed 14:644f9e7278e9 208 #ifdef Ble
fabiombed 14:644f9e7278e9 209
fabiombed 9:ca289bf57f52 210 /* Bluetooth CallBack ---------------------------------------------------------*/
fabiombed 9:ca289bf57f52 211
fabiombed 9:ca289bf57f52 212 static void onUpdatesEnabledCallback(GattAttribute::Handle_t handle)
fabiombed 9:ca289bf57f52 213 {
fabiombed 17:582eba752042 214 if (p_customcontrolservice->isCommandHandle(handle)) p_customcontrolservice->enNotify(handle);
fabiombed 17:582eba752042 215
fabiombed 17:582eba752042 216 if (p_customsensorservice->isStatusHandle(handle)) p_customsensorservice->enNotify(handle);
fabiombed 15:019b8d60c89d 217 if (p_customsensorservice->isDifferenceHandle(handle)) p_customsensorservice->enNotify(handle);
fabiombed 15:019b8d60c89d 218 if (p_customsensorservice->isPositionHandle(handle)) p_customsensorservice->enNotify(handle);
fabiombed 15:019b8d60c89d 219 if (p_customsensorservice->isSunpanelHandle(handle)) p_customsensorservice->enNotify(handle);
fabiombed 9:ca289bf57f52 220 }
fabiombed 9:ca289bf57f52 221
fabiombed 9:ca289bf57f52 222 static void onUpdatesDisabledCallback(Gap::Handle_t handle)
fabiombed 9:ca289bf57f52 223 {
fabiombed 17:582eba752042 224 if (p_customcontrolservice->isCommandHandle(handle)) p_customcontrolservice->disNotify(handle);
fabiombed 17:582eba752042 225
fabiombed 17:582eba752042 226 if (p_customsensorservice->isStatusHandle(handle)) p_customsensorservice->disNotify(handle);
fabiombed 15:019b8d60c89d 227 if (p_customsensorservice->isDifferenceHandle(handle)) p_customsensorservice->disNotify(handle);
fabiombed 15:019b8d60c89d 228 if (p_customsensorservice->isPositionHandle(handle)) p_customsensorservice->disNotify(handle);
fabiombed 15:019b8d60c89d 229 if (p_customsensorservice->isSunpanelHandle(handle)) p_customsensorservice->disNotify(handle);
fabiombed 9:ca289bf57f52 230 }
fabiombed 9:ca289bf57f52 231
fabiombed 9:ca289bf57f52 232 static void onDataReadCallback(const GattReadCallbackParams *eventDataP)
fabiombed 9:ca289bf57f52 233 {
fabiombed 18:319a1bb8f837 234
fabiombed 17:582eba752042 235 // if receive a manual Read request
fabiombed 17:582eba752042 236
fabiombed 17:582eba752042 237 if (p_customsensorservice->isStatusHandle(eventDataP->handle))
fabiombed 17:582eba752042 238 {
fabiombed 19:b2c04428ffed 239 #ifdef Debug
fabiombed 19:b2c04428ffed 240 printf("\n\r\n\rSend BLE Display Status %d", Status);
fabiombed 19:b2c04428ffed 241 #endif
fabiombed 17:582eba752042 242 p_customsensorservice->sendEnvStatus(Status, TimeStamp);
fabiombed 17:582eba752042 243
fabiombed 19:b2c04428ffed 244 } else if (p_customsensorservice->isDifferenceHandle(eventDataP->handle))
fabiombed 19:b2c04428ffed 245 {
fabiombed 19:b2c04428ffed 246 #ifdef Debug
fabiombed 19:b2c04428ffed 247 printf("\n\rSend BLE Difference %d lux/mems", diff); // Send BLE diff, no diff_abs
fabiombed 19:b2c04428ffed 248 #endif
fabiombed 17:582eba752042 249 p_customsensorservice->sendEnvDifference(diff, TimeStamp);
fabiombed 17:582eba752042 250
fabiombed 19:b2c04428ffed 251 } else if (p_customsensorservice->isPositionHandle(eventDataP->handle))
fabiombed 19:b2c04428ffed 252 {
fabiombed 19:b2c04428ffed 253 #ifdef Debug
fabiombed 19:b2c04428ffed 254 printf("\n\rSend BLE Position %d", pos);
fabiombed 19:b2c04428ffed 255 #endif
fabiombed 17:582eba752042 256 p_customsensorservice->sendEnvPosition(pos, TimeStamp);
fabiombed 17:582eba752042 257
fabiombed 19:b2c04428ffed 258 } else if (p_customsensorservice->isSunpanelHandle(eventDataP->handle))
fabiombed 19:b2c04428ffed 259 {
fabiombed 19:b2c04428ffed 260 #ifdef Debug
fabiombed 19:b2c04428ffed 261 printf("\n\rSend BLE Sunpanel %d mV", measure);
fabiombed 19:b2c04428ffed 262 #endif
fabiombed 17:582eba752042 263 p_customsensorservice->sendEnvSunpanel(measure, TimeStamp);
fabiombed 17:582eba752042 264
fabiombed 17:582eba752042 265 }
fabiombed 18:319a1bb8f837 266
fabiombed 9:ca289bf57f52 267 }
fabiombed 9:ca289bf57f52 268
fabiombed 14:644f9e7278e9 269 // This Callback happen when it RECEIVE a WRITE
fabiombed 9:ca289bf57f52 270 static void myonDataWriteCallback(const GattWriteCallbackParams *eventDataP)
fabiombed 14:644f9e7278e9 271 {
fabiombed 15:019b8d60c89d 272
fabiombed 17:582eba752042 273 if (p_customcontrolservice->isCommandHandle(eventDataP->handle))
fabiombed 17:582eba752042 274 {
fabiombed 17:582eba752042 275 // From CLIENT it receives a command in this format: Feature (4) + Type (1) + Data (2)
fabiombed 17:582eba752042 276 uint32_t Feature = eventDataP->data[0]<<24 | eventDataP->data[1]<<16 | eventDataP->data[2]<<8 | eventDataP->data[3];
fabiombed 19:b2c04428ffed 277 //uint8_t Type = eventDataP->data[4];
fabiombed 17:582eba752042 278 int16_t Data = eventDataP->data[5]<<8 | eventDataP->data[6];
fabiombed 14:644f9e7278e9 279
fabiombed 17:582eba752042 280 //printf("\r\n\r\nmyonDataWriteCallback (Feature %x)", Feature);
fabiombed 17:582eba752042 281 //printf("\r\nmyonDataWriteCallback (Type %x)", Type); // Not Used
fabiombed 17:582eba752042 282 //printf("\r\nmyonDataWriteCallback (Data Hex %x)", Data);
fabiombed 17:582eba752042 283 //printf("\r\nmyonDataWriteCallback (Data Dec %d)", Data);
fabiombed 17:582eba752042 284
fabiombed 17:582eba752042 285 switch(Feature)
fabiombed 17:582eba752042 286 {
fabiombed 17:582eba752042 287 case FeatureStatus:
fabiombed 17:582eba752042 288 printf("\r\n\r\nReceived STATUS %d", Data);
fabiombed 17:582eba752042 289 Status = Data;
fabiombed 17:582eba752042 290 Update_Status(Data);
fabiombed 17:582eba752042 291 break;
fabiombed 17:582eba752042 292
fabiombed 17:582eba752042 293 case FeatureDifference:
fabiombed 19:b2c04428ffed 294 #ifdef Debug
fabiombed 19:b2c04428ffed 295 printf("\r\n\r\nReceived DIFF %d", Data);
fabiombed 19:b2c04428ffed 296 #endif
fabiombed 17:582eba752042 297 diff = Data;
fabiombed 17:582eba752042 298 break;
fabiombed 17:582eba752042 299
fabiombed 17:582eba752042 300 case FeaturePosition:
fabiombed 17:582eba752042 301 break;
fabiombed 17:582eba752042 302
fabiombed 17:582eba752042 303 case FeatureSunPanel:
fabiombed 17:582eba752042 304 break;
fabiombed 17:582eba752042 305
fabiombed 17:582eba752042 306 default:
fabiombed 17:582eba752042 307 break;
fabiombed 17:582eba752042 308 }
fabiombed 15:019b8d60c89d 309 }
fabiombed 15:019b8d60c89d 310
fabiombed 9:ca289bf57f52 311 }
fabiombed 9:ca289bf57f52 312
fabiombed 9:ca289bf57f52 313 static void onConnectionCallback(const Gap::ConnectionCallbackParams_t * connectionParams)
fabiombed 9:ca289bf57f52 314 {
fabiombed 14:644f9e7278e9 315 printf("\r\n\r\nonConnectionCallback (Line %d)", __LINE__);
fabiombed 19:b2c04428ffed 316 BLEConnected=true;
fabiombed 9:ca289bf57f52 317 }
fabiombed 9:ca289bf57f52 318
fabiombed 9:ca289bf57f52 319 static void onDisconnectionCallback(const Gap::DisconnectionCallbackParams_t * disConnectionReason)
fabiombed 9:ca289bf57f52 320 {
fabiombed 14:644f9e7278e9 321 printf("\r\n\r\nonDisconnectionCallback (Line %d)", __LINE__);
fabiombed 9:ca289bf57f52 322 p_BLEdev->gap().startAdvertising();
fabiombed 19:b2c04428ffed 323 BLEConnected=false;
fabiombed 9:ca289bf57f52 324 }
fabiombed 9:ca289bf57f52 325
fabiombed 14:644f9e7278e9 326 #endif
fabiombed 14:644f9e7278e9 327
fabiombed 9:ca289bf57f52 328 /* Bluetooth Initialization ---------------------------------------------------*/
fabiombed 9:ca289bf57f52 329
fabiombed 9:ca289bf57f52 330 bool BLE_Initialization(void)
fabiombed 9:ca289bf57f52 331 {
fabiombed 9:ca289bf57f52 332
fabiombed 14:644f9e7278e9 333 #ifdef Ble
fabiombed 14:644f9e7278e9 334
fabiombed 9:ca289bf57f52 335 p_BLEdev = new BLE;
fabiombed 9:ca289bf57f52 336 if (!p_BLEdev) { printf("\r\nBLE Device creation failed\r\n"); }
fabiombed 9:ca289bf57f52 337 const Gap::Address_t BLE_address_BE = {BLE_DEV_MAC};
fabiombed 9:ca289bf57f52 338 p_BLEdev->gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE);
fabiombed 9:ca289bf57f52 339
fabiombed 9:ca289bf57f52 340 p_BLEdev->init();
fabiombed 9:ca289bf57f52 341
fabiombed 9:ca289bf57f52 342 // Set BLE CallBack Functions
fabiombed 9:ca289bf57f52 343 p_BLEdev->gattServer().onUpdatesEnabled(onUpdatesEnabledCallback);
fabiombed 9:ca289bf57f52 344 p_BLEdev->gattServer().onUpdatesDisabled(onUpdatesDisabledCallback);
fabiombed 9:ca289bf57f52 345 p_BLEdev->gattServer().onDataRead(onDataReadCallback);
fabiombed 9:ca289bf57f52 346 p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback);
fabiombed 9:ca289bf57f52 347 p_BLEdev->gap().onConnection(onConnectionCallback);
fabiombed 9:ca289bf57f52 348 p_BLEdev->gap().onDisconnection(onDisconnectionCallback);
fabiombed 9:ca289bf57f52 349 //p_BLEdev->gattServer().onConfirmationReceived(onConfirmationReceivedCallback);
fabiombed 9:ca289bf57f52 350 //p_BLEdev->gattServer().onDataSent(onDataSentCallback);
fabiombed 9:ca289bf57f52 351 //p_BLEdev->gap().onTimeout(onTimeoutCallback);
fabiombed 9:ca289bf57f52 352
fabiombed 9:ca289bf57f52 353 // BLE Services
fabiombed 14:644f9e7278e9 354 p_customcontrolservice = new CustomControlService(*p_BLEdev);
fabiombed 9:ca289bf57f52 355 p_customsensorservice = new CustomSensorService(*p_BLEdev);
fabiombed 9:ca289bf57f52 356
fabiombed 9:ca289bf57f52 357 // Setup BLE Advertising
fabiombed 9:ca289bf57f52 358 const static char DEVICE_NAME[] = BLE_DEV_NAME;
fabiombed 9:ca289bf57f52 359 p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
fabiombed 9:ca289bf57f52 360 p_BLEdev->gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,dat,6);
fabiombed 9:ca289bf57f52 361 p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
fabiombed 9:ca289bf57f52 362 p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
fabiombed 9:ca289bf57f52 363 p_BLEdev->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
fabiombed 9:ca289bf57f52 364 p_BLEdev->gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL);
fabiombed 9:ca289bf57f52 365 p_BLEdev->gap().startAdvertising();
fabiombed 9:ca289bf57f52 366
fabiombed 14:644f9e7278e9 367 printf("Initialization Ble OK (Line %d)\r\n", __LINE__);
fabiombed 14:644f9e7278e9 368
fabiombed 14:644f9e7278e9 369 #endif
fabiombed 14:644f9e7278e9 370
fabiombed 9:ca289bf57f52 371 return true;
fabiombed 9:ca289bf57f52 372
fabiombed 9:ca289bf57f52 373 }
fabiombed 9:ca289bf57f52 374
fabiombed 8:144855fe02bd 375 /* Initialization ------------------------------------------------------------*/
fabiombed 8:144855fe02bd 376
fabiombed 8:144855fe02bd 377 bool Initialization(void)
fabiombed 8:144855fe02bd 378 {
fabiombed 11:b5e23d64d366 379
fabiombed 14:644f9e7278e9 380 dev_i2c.frequency(100000); // Default 100KHz. At 10KHz, 20KHz, 200KHz and 400KHz doesn't work
fabiombed 11:b5e23d64d366 381
fabiombed 11:b5e23d64d366 382 //----
fabiombed 14:644f9e7278e9 383
fabiombed 14:644f9e7278e9 384 #ifdef FlightSense
fabiombed 14:644f9e7278e9 385
fabiombed 14:644f9e7278e9 386 // Initializing FlightSense Component
fabiombed 14:644f9e7278e9 387 board=X_NUCLEO_6180XA1::Instance(&dev_i2c, NC, NC, NC, NC); // NC as Interrupt fixed from FlightSense library 42
fabiombed 14:644f9e7278e9 388
fabiombed 17:582eba752042 389 status_bb=board->InitBoard();
fabiombed 17:582eba752042 390 if(status_bb) VL6180x_ErrLog("Failed to init the board!\n\r");
fabiombed 8:144855fe02bd 391
fabiombed 8:144855fe02bd 392 // Put GPIO not used as Interrupt in Hi-Z
fabiombed 8:144855fe02bd 393 status_t=board->sensor_top->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 14:644f9e7278e9 394 //status_b=board->sensor_botton->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); Not Present
fabiombed 8:144855fe02bd 395 status_l=board->sensor_left->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 8:144855fe02bd 396 status_r=board->sensor_right->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 8:144855fe02bd 397
fabiombed 8:144855fe02bd 398 // Set Babybears
fabiombed 8:144855fe02bd 399 status_l=board->sensor_left->AlsSetAnalogueGain(3);
fabiombed 8:144855fe02bd 400 status_r=board->sensor_right->AlsSetAnalogueGain(3);
fabiombed 8:144855fe02bd 401 status_l=board->sensor_left->StartMeasurement(als_continuous_polling, NULL, NULL, NULL);
fabiombed 8:144855fe02bd 402 status_r=board->sensor_right->StartMeasurement(als_continuous_polling, NULL, NULL, NULL);
fabiombed 8:144855fe02bd 403
fabiombed 14:644f9e7278e9 404 printf("Init FlightSense OK\r\n");
fabiombed 14:644f9e7278e9 405
fabiombed 14:644f9e7278e9 406 #endif
fabiombed 11:b5e23d64d366 407
fabiombed 8:144855fe02bd 408 //----
fabiombed 8:144855fe02bd 409
fabiombed 14:644f9e7278e9 410 #ifdef EasySpin
fabiombed 11:b5e23d64d366 411
fabiombed 14:644f9e7278e9 412 // Initializing EasySpin Component
fabiombed 9:ca289bf57f52 413 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 414 // Used Morpho PB_2 and made HW wiring
fabiombed 8:144855fe02bd 415 if (motor->Init(NULL) != COMPONENT_OK)
fabiombed 8:144855fe02bd 416 return false;
fabiombed 8:144855fe02bd 417
fabiombed 16:d69c0d5d5ab2 418 motor->SetStepMode(StepperMotor::STEP_MODE_1_8); // Default is STEP_MODE_1_16
fabiombed 8:144855fe02bd 419
fabiombed 8:144855fe02bd 420 // Set defaults Motor Speed
fabiombed 8:144855fe02bd 421 motor->SetAcceleration(SET_ACC);
fabiombed 8:144855fe02bd 422 motor->SetDeceleration(SET_DEC);
fabiombed 8:144855fe02bd 423 motor->SetMaxSpeed(SET_MAX); // Variable by Light/Mems Sensors
fabiombed 8:144855fe02bd 424 motor->SetMinSpeed(SET_MIN);
fabiombed 8:144855fe02bd 425
fabiombed 14:644f9e7278e9 426 printf("Init EasySpin OK\r\n");
fabiombed 14:644f9e7278e9 427
fabiombed 14:644f9e7278e9 428 #endif
fabiombed 14:644f9e7278e9 429
fabiombed 14:644f9e7278e9 430 //----
fabiombed 14:644f9e7278e9 431
fabiombed 14:644f9e7278e9 432 #ifdef Sensors
fabiombed 11:b5e23d64d366 433
fabiombed 14:644f9e7278e9 434 // Initializing Sensors Component
fabiombed 14:644f9e7278e9 435 mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c);
fabiombed 14:644f9e7278e9 436 accelerometer = mems->GetAccelerometer();
fabiombed 14:644f9e7278e9 437
fabiombed 14:644f9e7278e9 438 printf("Init Sensors OK\r\n");
fabiombed 14:644f9e7278e9 439
fabiombed 14:644f9e7278e9 440 #endif
fabiombed 14:644f9e7278e9 441
fabiombed 14:644f9e7278e9 442 printf("Initialization OK (Line %d)\r\n", __LINE__);
fabiombed 8:144855fe02bd 443 return true;
fabiombed 8:144855fe02bd 444
fabiombed 8:144855fe02bd 445 }
fabiombed 8:144855fe02bd 446
fabiombed 8:144855fe02bd 447 /* Measure_Babybear ----------------------------------------------------------*/
fabiombed 8:144855fe02bd 448
fabiombed 8:144855fe02bd 449 void Measure_Babybear(void)
fabiombed 8:144855fe02bd 450 {
fabiombed 8:144855fe02bd 451
fabiombed 14:644f9e7278e9 452 #ifdef FlightSense
fabiombed 14:644f9e7278e9 453
fabiombed 8:144855fe02bd 454 status_l=board->sensor_left->GetMeasurement(als_continuous_polling, &data_sensor_left);
fabiombed 8:144855fe02bd 455 status_r=board->sensor_right->GetMeasurement(als_continuous_polling, &data_sensor_right);
fabiombed 8:144855fe02bd 456
fabiombed 8:144855fe02bd 457 babybear = data_sensor_right.lux - data_sensor_left.lux;
fabiombed 8:144855fe02bd 458
fabiombed 14:644f9e7278e9 459 diff = babybear;
fabiombed 1:8f312c1686b6 460
fabiombed 14:644f9e7278e9 461 #endif
fabiombed 8:144855fe02bd 462
fabiombed 8:144855fe02bd 463 }
fabiombed 8:144855fe02bd 464
fabiombed 8:144855fe02bd 465 /* Measure_Accelerometer -----------------------------------------------------*/
fabiombed 8:144855fe02bd 466
fabiombed 8:144855fe02bd 467 void Measure_Accelerometer(void)
fabiombed 8:144855fe02bd 468 {
fabiombed 8:144855fe02bd 469
fabiombed 14:644f9e7278e9 470 #ifdef Sensors
fabiombed 14:644f9e7278e9 471
fabiombed 8:144855fe02bd 472 accelerometer->Get_X_Axes(acc_data);
fabiombed 8:144855fe02bd 473
fabiombed 14:644f9e7278e9 474 diff = acc_data[0];
fabiombed 8:144855fe02bd 475
fabiombed 14:644f9e7278e9 476 #endif
fabiombed 8:144855fe02bd 477
fabiombed 8:144855fe02bd 478 }
fabiombed 8:144855fe02bd 479
fabiombed 8:144855fe02bd 480 /* Control_Motor -------------------------------------------------------------*/
fabiombed 8:144855fe02bd 481
fabiombed 8:144855fe02bd 482 void Control_Motor(void)
fabiombed 8:144855fe02bd 483 {
fabiombed 8:144855fe02bd 484
fabiombed 14:644f9e7278e9 485 #ifdef EasySpin
fabiombed 14:644f9e7278e9 486
fabiombed 14:644f9e7278e9 487 diff_abs = abs(diff);
fabiombed 8:144855fe02bd 488
fabiombed 14:644f9e7278e9 489 //printf("Diff: %d lux/mems\n\r", diff_abs);
fabiombed 14:644f9e7278e9 490 motor->SetMaxSpeed(diff_abs);
fabiombed 14:644f9e7278e9 491
fabiombed 14:644f9e7278e9 492 if (diff>0) { left=0; right=1; }
fabiombed 14:644f9e7278e9 493 if (diff<0) { left=1; right=0; }
fabiombed 14:644f9e7278e9 494
fabiombed 14:644f9e7278e9 495 if (diff_abs>TOLLERANCE) {
fabiombed 14:644f9e7278e9 496 if (diff_abs <=RANGE_1)
fabiombed 8:144855fe02bd 497 {
fabiombed 8:144855fe02bd 498 if (left) { strcpy(DisplayStr,"E___"); }
fabiombed 8:144855fe02bd 499 if (right) { strcpy(DisplayStr,"___3"); }
fabiombed 8:144855fe02bd 500 }
fabiombed 14:644f9e7278e9 501 else if (diff_abs >RANGE_1 & diff_abs <=RANGE_2)
fabiombed 8:144855fe02bd 502 {
fabiombed 8:144855fe02bd 503 if (left) { strcpy(DisplayStr,"E==="); }
fabiombed 8:144855fe02bd 504 if (right) { strcpy(DisplayStr,"===3"); }
fabiombed 8:144855fe02bd 505 }
fabiombed 14:644f9e7278e9 506 else if (diff_abs >RANGE_2)
fabiombed 8:144855fe02bd 507 {
fabiombed 8:144855fe02bd 508 if (left) { strcpy(DisplayStr,"E~~~"); }
fabiombed 8:144855fe02bd 509 if (right) { strcpy(DisplayStr,"~~~3"); }
fabiombed 8:144855fe02bd 510 }
fabiombed 8:144855fe02bd 511
fabiombed 8:144855fe02bd 512 // In Case of Change Direction
fabiombed 8:144855fe02bd 513 if (left & dir==2) { changedir=1; }
fabiombed 8:144855fe02bd 514 if (right & dir==1) { changedir=1; }
fabiombed 0:becf0d313663 515
fabiombed 8:144855fe02bd 516 // Run only if Stop or Change Direction
fabiombed 14:644f9e7278e9 517 if (diff_abs>TOLLERANCE & (dir==0 | changedir==1)) {
fabiombed 8:144855fe02bd 518 if (left) { motor->Run(StepperMotor::FWD); dir=1; changedir=0; }
fabiombed 8:144855fe02bd 519 if (right) { motor->Run(StepperMotor::BWD); dir=2; changedir=0; }
fabiombed 14:644f9e7278e9 520 printf("\n\rRUN");
fabiombed 8:144855fe02bd 521 }
fabiombed 8:144855fe02bd 522 }
fabiombed 8:144855fe02bd 523
fabiombed 8:144855fe02bd 524 // Get Motor Position and Control Rotation Block
fabiombed 8:144855fe02bd 525 pos = motor->GetPosition();
fabiombed 8:144855fe02bd 526 if (pos>STOP | pos<-STOP) {
fabiombed 8:144855fe02bd 527 if (pos>0) { motor->GoTo(STOP); }
fabiombed 8:144855fe02bd 528 if (pos<0) { motor->GoTo(-STOP); }
fabiombed 14:644f9e7278e9 529 printf("\n\rGOTO");
fabiombed 8:144855fe02bd 530 }
fabiombed 1:8f312c1686b6 531
fabiombed 8:144855fe02bd 532 // Stop Motor
fabiombed 14:644f9e7278e9 533 if (diff_abs<=TOLLERANCE) {
fabiombed 8:144855fe02bd 534 arrest=1;
fabiombed 17:582eba752042 535 if (Status==1) { strcpy(DisplayStr,"----"); }
fabiombed 17:582eba752042 536 if (Status==3) { strcpy(DisplayStr,"E 3"); }
fabiombed 8:144855fe02bd 537 }
fabiombed 8:144855fe02bd 538
fabiombed 8:144855fe02bd 539 if (arrest==1 && dir!=0 ) { // Stop just one time
fabiombed 8:144855fe02bd 540 motor->HardStop();
fabiombed 8:144855fe02bd 541 dir=0;
fabiombed 8:144855fe02bd 542 changedir=0;
fabiombed 8:144855fe02bd 543 arrest=0;
fabiombed 14:644f9e7278e9 544 printf("\n\rSTOP");
fabiombed 8:144855fe02bd 545 }
fabiombed 8:144855fe02bd 546
fabiombed 14:644f9e7278e9 547 #endif
fabiombed 14:644f9e7278e9 548
fabiombed 8:144855fe02bd 549 }
fabiombed 8:144855fe02bd 550
fabiombed 8:144855fe02bd 551 /* Measure_SolarPanel --------------------------------------------------------*/
fabiombed 8:144855fe02bd 552
fabiombed 8:144855fe02bd 553 void Measure_SolarPanel(void)
fabiombed 8:144855fe02bd 554 {
fabiombed 8:144855fe02bd 555
fabiombed 8:144855fe02bd 556 // AnalogIn: 0V return 0.0 , 3.3V return 1.0
fabiombed 14:644f9e7278e9 557 measure = analog_read.read() * 3300;
fabiombed 8:144855fe02bd 558 //printf("Measure = %.0f mV\r\n", measure);
fabiombed 8:144855fe02bd 559 //board->display->DisplayDigit("A", 0);
fabiombed 8:144855fe02bd 560
fabiombed 19:b2c04428ffed 561 //if (abs(measure-prev_meas)>(prev_meas*0.1)) { prev_meas = measure; }
fabiombed 19:b2c04428ffed 562 //if (Status==2) { sprintf(DisplayStr, "%d", prev_meas); }
fabiombed 19:b2c04428ffed 563
fabiombed 17:582eba752042 564 if (Status==2) { sprintf(DisplayStr, "%d", measure); }
fabiombed 8:144855fe02bd 565 }
fabiombed 1:8f312c1686b6 566
fabiombed 18:319a1bb8f837 567 /* Ticker --------------------------------------------------------------------*/
fabiombed 18:319a1bb8f837 568
fabiombed 18:319a1bb8f837 569 void tickerSta_Callback(void) { tickerSta=true; }
fabiombed 18:319a1bb8f837 570 void tickerDif_Callback(void) { tickerDif=true; }
fabiombed 18:319a1bb8f837 571 void tickerPos_Callback(void) { tickerPos=true; }
fabiombed 18:319a1bb8f837 572 void tickerSun_Callback(void) { tickerSun=true; }
fabiombed 18:319a1bb8f837 573
fabiombed 8:144855fe02bd 574 /* Main ----------------------------------------------------------------------*/
fabiombed 8:144855fe02bd 575
fabiombed 8:144855fe02bd 576 int main()
fabiombed 8:144855fe02bd 577 {
fabiombed 8:144855fe02bd 578
fabiombed 8:144855fe02bd 579 // Printing to the console
fabiombed 14:644f9e7278e9 580 printf("\r\n\r\nSunTracker by Fabio Brembilla\r\n\r\n");
fabiombed 11:b5e23d64d366 581
fabiombed 8:144855fe02bd 582 Initialization();
fabiombed 14:644f9e7278e9 583 BLE_Initialization();
fabiombed 7:54984d031243 584
fabiombed 8:144855fe02bd 585 mybutton.fall(&User_Button_Pressed);
fabiombed 7:54984d031243 586
fabiombed 17:582eba752042 587 #ifdef Ble
fabiombed 18:319a1bb8f837 588 Ticker tickerSta_ticker, tickerDif_ticker, tickerPos_ticker, tickerSun_ticker;
fabiombed 18:319a1bb8f837 589 tickerSta_ticker.attach(tickerSta_Callback, TIMEOUT_STA);
fabiombed 18:319a1bb8f837 590 #endif
fabiombed 18:319a1bb8f837 591
fabiombed 8:144855fe02bd 592 // Loop until push User Button to Set 0 Point
fabiombed 14:644f9e7278e9 593 printf("\r\nWait PUSH Button");
fabiombed 8:144855fe02bd 594 strcpy(DisplayStr,"pusH");
fabiombed 19:b2c04428ffed 595 while(Status==0)
fabiombed 19:b2c04428ffed 596 {
fabiombed 14:644f9e7278e9 597 #ifdef FlightSense
fabiombed 19:b2c04428ffed 598 board->display->DisplayString(DisplayStr, strlen(DisplayStr)); // if I set fix len=4, it aligns digit from left
fabiombed 8:144855fe02bd 599 //printf("%s\n\r", DisplayStr);
fabiombed 17:582eba752042 600 #endif
fabiombed 17:582eba752042 601
fabiombed 17:582eba752042 602 #ifdef Ble
fabiombed 18:319a1bb8f837 603 if (p_customsensorservice->isStatusNotificationEn()) {
fabiombed 18:319a1bb8f837 604 if (tickerSta) {
fabiombed 18:319a1bb8f837 605 tickerSta=false;
fabiombed 19:b2c04428ffed 606 #ifdef Debug
fabiombed 19:b2c04428ffed 607 printf("\n\r\n\rSend BLE Display Status %d", Status);
fabiombed 19:b2c04428ffed 608 #endif
fabiombed 18:319a1bb8f837 609 //p_customsensorservice->sendEnvStatus(Status, TimeStamp);
fabiombed 18:319a1bb8f837 610 p_customsensorservice->updateEnvStatus(Status, TimeStamp);
fabiombed 18:319a1bb8f837 611 }
fabiombed 17:582eba752042 612 }
fabiombed 18:319a1bb8f837 613 p_BLEdev->waitForEvent(); // Without it the CLIENT can't start to communicate
fabiombed 14:644f9e7278e9 614 #endif
fabiombed 8:144855fe02bd 615 }
fabiombed 4:1d3d071a4c2c 616
fabiombed 14:644f9e7278e9 617 printf("\r\n\r\nStart Main Loop");
fabiombed 18:319a1bb8f837 618
fabiombed 18:319a1bb8f837 619 #ifdef Ble
fabiombed 18:319a1bb8f837 620 tickerDif_ticker.attach(tickerDif_Callback, TIMEOUT_DIF);
fabiombed 18:319a1bb8f837 621 tickerPos_ticker.attach(tickerPos_Callback, TIMEOUT_POS);
fabiombed 18:319a1bb8f837 622 tickerSun_ticker.attach(tickerSun_Callback, TIMEOUT_SUN);
fabiombed 18:319a1bb8f837 623 #endif
fabiombed 17:582eba752042 624
fabiombed 14:644f9e7278e9 625 #ifdef EasySpin
fabiombed 14:644f9e7278e9 626 motor->Enable(); // To put the motor on hold by execute CmdEnable
fabiombed 14:644f9e7278e9 627 #endif
fabiombed 10:d154094297ce 628
fabiombed 6:4cbf7303b496 629 // Main Loop
fabiombed 8:144855fe02bd 630 while(true)
fabiombed 19:b2c04428ffed 631 {
fabiombed 17:582eba752042 632 if (Status==1 | Status==2) { Measure_Babybear(); }
fabiombed 15:019b8d60c89d 633
fabiombed 19:b2c04428ffed 634 // If BLE is not connected, it uses Accelerometer from SERVER, otherwise receive the value from CLIENT
fabiombed 19:b2c04428ffed 635 if (BLEConnected==false) {
fabiombed 19:b2c04428ffed 636 if (Status==3) { Measure_Accelerometer(); }
fabiombed 19:b2c04428ffed 637 }
fabiombed 15:019b8d60c89d 638
fabiombed 8:144855fe02bd 639 Control_Motor();
fabiombed 8:144855fe02bd 640 Measure_SolarPanel();
fabiombed 14:644f9e7278e9 641
fabiombed 14:644f9e7278e9 642 #ifdef FlightSense
fabiombed 19:b2c04428ffed 643 board->display->DisplayString(DisplayStr, strlen(DisplayStr)); // if I set fix len=4, it aligns digit from left
fabiombed 14:644f9e7278e9 644 //printf("%s\n\r", DisplayStr);
fabiombed 18:319a1bb8f837 645 #endif
fabiombed 17:582eba752042 646
fabiombed 18:319a1bb8f837 647 #ifdef Ble
fabiombed 19:b2c04428ffed 648 //if (Status==3 & BLEConnected==false) { diff=0; }
fabiombed 19:b2c04428ffed 649 //if Status 3 and BLE not connected set diff=0 (no more useful if use SERVER Accelerometer)
fabiombed 19:b2c04428ffed 650 //After disconnect CLIENT, until it receive the timeout/notify to be really disconnected, it not receive diff!
fabiombed 19:b2c04428ffed 651
fabiombed 18:319a1bb8f837 652 if (p_customsensorservice->isStatusNotificationEn()) {
fabiombed 18:319a1bb8f837 653 if (tickerSta) {
fabiombed 18:319a1bb8f837 654 tickerSta=false;
fabiombed 19:b2c04428ffed 655 #ifdef Debug
fabiombed 19:b2c04428ffed 656 printf("\n\r\n\rSend BLE Display Status %d", Status);
fabiombed 19:b2c04428ffed 657 #endif
fabiombed 18:319a1bb8f837 658 //p_customsensorservice->sendEnvStatus(Status, TimeStamp);
fabiombed 18:319a1bb8f837 659 p_customsensorservice->updateEnvStatus(Status, TimeStamp);
fabiombed 18:319a1bb8f837 660 }
fabiombed 18:319a1bb8f837 661 }
fabiombed 18:319a1bb8f837 662 if (p_customsensorservice->isDifferenceNotificationEn()) {
fabiombed 18:319a1bb8f837 663 if (tickerDif) {
fabiombed 18:319a1bb8f837 664 tickerDif=false;
fabiombed 19:b2c04428ffed 665 #ifdef Debug
fabiombed 19:b2c04428ffed 666 printf("\n\rSend BLE Difference %d lux/mems", diff); // Send BLE diff, no diff_abs
fabiombed 19:b2c04428ffed 667 #endif
fabiombed 18:319a1bb8f837 668 //p_customsensorservice->sendEnvDifference(diff, TimeStamp);
fabiombed 18:319a1bb8f837 669 p_customsensorservice->updateEnvDifference(diff, TimeStamp);
fabiombed 18:319a1bb8f837 670 }
fabiombed 18:319a1bb8f837 671 }
fabiombed 18:319a1bb8f837 672 if (p_customsensorservice->isPositionNotificationEn()) {
fabiombed 18:319a1bb8f837 673 if (tickerPos) {
fabiombed 19:b2c04428ffed 674 tickerPos=false;
fabiombed 19:b2c04428ffed 675 #ifdef Debug
fabiombed 19:b2c04428ffed 676 printf("\n\rSend BLE Position %d", pos);
fabiombed 19:b2c04428ffed 677 #endif
fabiombed 18:319a1bb8f837 678 //p_customsensorservice->sendEnvPosition(pos, TimeStamp);
fabiombed 18:319a1bb8f837 679 p_customsensorservice->updateEnvPosition(pos, TimeStamp);
fabiombed 18:319a1bb8f837 680 }
fabiombed 14:644f9e7278e9 681 }
fabiombed 18:319a1bb8f837 682 if (p_customsensorservice->isSunpanelNotificationEn()) {
fabiombed 18:319a1bb8f837 683 if (tickerSun) {
fabiombed 19:b2c04428ffed 684 tickerSun=false;
fabiombed 19:b2c04428ffed 685 #ifdef Debug
fabiombed 19:b2c04428ffed 686 printf("\n\rSend BLE SunPanel %d mV", measure);
fabiombed 19:b2c04428ffed 687 #endif
fabiombed 18:319a1bb8f837 688 //p_customsensorservice->sendEnvSunpanel(measure, TimeStamp);
fabiombed 18:319a1bb8f837 689 p_customsensorservice->updateEnvSunpanel(measure, TimeStamp);
fabiombed 18:319a1bb8f837 690 }
fabiombed 18:319a1bb8f837 691 }
fabiombed 18:319a1bb8f837 692
fabiombed 11:b5e23d64d366 693 p_BLEdev->waitForEvent();
fabiombed 14:644f9e7278e9 694 #endif
fabiombed 8:144855fe02bd 695 }
fabiombed 4:1d3d071a4c2c 696
fabiombed 8:144855fe02bd 697 //status_l=board->sensor_left->StopMeasurement(als_continuous_polling);
fabiombed 8:144855fe02bd 698 //status_r=board->sensor_right->StopMeasurement(als_continuous_polling);
fabiombed 14:644f9e7278e9 699 }