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:
fabiombed
Date:
Tue Apr 19 08:53:07 2016 +0000
Revision:
17:582eba752042
Parent:
16:d69c0d5d5ab2
Child:
18:319a1bb8f837
Updated to BlueST Protocol

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