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:
Wed Feb 03 11:22:17 2016 +0000
Revision:
5:76fb6b783487
Parent:
4:1d3d071a4c2c
Child:
6:4cbf7303b496
BLE Services Created

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 4:1d3d071a4c2c 6 * @date January 22th, 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>&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 0:becf0d313663 39 /* Includes ------------------------------------------------------------------*/
fabiombed 4:1d3d071a4c2c 40
fabiombed 0:becf0d313663 41 /* mbed specific header files. */
fabiombed 0:becf0d313663 42 #include "mbed.h"
fabiombed 4:1d3d071a4c2c 43
fabiombed 0:becf0d313663 44 /* Helper header files. */
fabiombed 0:becf0d313663 45 #include "DevSPI.h"
fabiombed 1:8f312c1686b6 46 #include "DevI2C.h"
fabiombed 0:becf0d313663 47
fabiombed 1:8f312c1686b6 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 1:8f312c1686b6 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 4:1d3d071a4c2c 59 /* Calibration files. */
fabiombed 4:1d3d071a4c2c 60 #include "MotionFX_Manager.h" // Need for osxMFX_calibFactor
fabiombed 4:1d3d071a4c2c 61
fabiombed 5:76fb6b783487 62 /* BlueTooth -----------------------------------------------------------------*/
fabiombed 5:76fb6b783487 63
fabiombed 5:76fb6b783487 64 #include "debug.h" // Need for PRINTF
fabiombed 5:76fb6b783487 65 #include "Utils.h" // Need for STORE_LE_16 and _32
fabiombed 5:76fb6b783487 66
fabiombed 5:76fb6b783487 67 typedef struct {
fabiombed 5:76fb6b783487 68 int32_t AXIS_X;
fabiombed 5:76fb6b783487 69 int32_t AXIS_Y;
fabiombed 5:76fb6b783487 70 int32_t AXIS_Z;
fabiombed 5:76fb6b783487 71 } AxesRaw_TypeDef;
fabiombed 5:76fb6b783487 72
fabiombed 5:76fb6b783487 73 typedef enum ConnectionStatus_t {
fabiombed 5:76fb6b783487 74 DISCONNECTED =0,
fabiombed 5:76fb6b783487 75 CONNECTED =1
fabiombed 5:76fb6b783487 76 }cns_t;
fabiombed 5:76fb6b783487 77
fabiombed 5:76fb6b783487 78 const unsigned LENGTH_OF_LONG_UUID = 16;
fabiombed 5:76fb6b783487 79 typedef uint16_t ShortUUIDBytes_t;
fabiombed 5:76fb6b783487 80 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
fabiombed 5:76fb6b783487 81
fabiombed 5:76fb6b783487 82 #include "CustomControlService.h"
fabiombed 5:76fb6b783487 83 #include "CustomSensorsService.h"
fabiombed 5:76fb6b783487 84
fabiombed 5:76fb6b783487 85 static BLE * p_BLEdev = NULL;
fabiombed 5:76fb6b783487 86
fabiombed 5:76fb6b783487 87 #define BLE_DEV_NAME "SunTrack"
fabiombed 5:76fb6b783487 88 #define BLE_DEV_MAC 0xAA,0xBB,0xCC,0xDD,0xEE,0xFF
fabiombed 4:1d3d071a4c2c 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 1:8f312c1686b6 105 int16_t babybear=0; // Difference (in Lux) between Left and Right
fabiombed 1:8f312c1686b6 106 int acc_data[3]; // Difference of Accelerometer
fabiombed 1:8f312c1686b6 107 int16_t diff=0; // Abs of Babybear or Accelerometer difference
fabiombed 1:8f312c1686b6 108 int16_t left=0; // Left Command for Rotate Direction
fabiombed 1:8f312c1686b6 109 int16_t right=0; // Right Command for Rotate Direction
fabiombed 1:8f312c1686b6 110 int16_t start=0; // Waiting User Button Push
fabiombed 1:8f312c1686b6 111 int32_t pos=0; // Motor Position
fabiombed 1:8f312c1686b6 112 char DisplayStr[5]; // Status Display
fabiombed 1:8f312c1686b6 113 int16_t Display=0; // Shown on Display: 0 = Motor Speed, 1 = Solar Panel Value, 2 = Manual Control
fabiombed 1:8f312c1686b6 114 int16_t status, status_t, status_b, status_l, status_r; // Babybear Status
fabiombed 1:8f312c1686b6 115
fabiombed 1:8f312c1686b6 116 /* ---------------------------------------------------------------------------*/
fabiombed 0:becf0d313663 117
fabiombed 0:becf0d313663 118 /* Motor Control Component. */
fabiombed 0:becf0d313663 119 L6474 *motor;
fabiombed 0:becf0d313663 120
fabiombed 1:8f312c1686b6 121 /* Initializing SPI bus. */
fabiombed 1:8f312c1686b6 122 DevSPI dev_spi(D11, D12, D13);
fabiombed 0:becf0d313663 123
fabiombed 1:8f312c1686b6 124 /* Initializing I2C bus. */
fabiombed 1:8f312c1686b6 125 DevI2C dev_i2c(D14, D15);
fabiombed 1:8f312c1686b6 126
fabiombed 1:8f312c1686b6 127 /* Instance board 6180XA1. */
fabiombed 1:8f312c1686b6 128 static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&dev_i2c, NC, NC, NC, NC);
fabiombed 1:8f312c1686b6 129 MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right;
fabiombed 0:becf0d313663 130
fabiombed 1:8f312c1686b6 131 /* Instance mems IKS01A1. */
fabiombed 1:8f312c1686b6 132 static X_NUCLEO_IKS01A1 *mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c);
fabiombed 1:8f312c1686b6 133 MotionSensor *accelerometer = mems->GetAccelerometer();
fabiombed 4:1d3d071a4c2c 134
fabiombed 4:1d3d071a4c2c 135 void DISP_ExecLoopBody(void) {};
fabiombed 0:becf0d313663 136
fabiombed 1:8f312c1686b6 137 AnalogIn analog_read_A1(A1);
fabiombed 1:8f312c1686b6 138
fabiombed 4:1d3d071a4c2c 139 InterruptIn mybutton(USER_BUTTON);
fabiombed 1:8f312c1686b6 140
fabiombed 1:8f312c1686b6 141 /* User_Button_Pressed -------------------------------------------------------*/
fabiombed 0:becf0d313663 142
fabiombed 1:8f312c1686b6 143 void User_Button_Pressed()
fabiombed 1:8f312c1686b6 144 {
fabiombed 4:1d3d071a4c2c 145
fabiombed 4:1d3d071a4c2c 146 if (start>0) {
fabiombed 4:1d3d071a4c2c 147 Display++;
fabiombed 4:1d3d071a4c2c 148 }
fabiombed 4:1d3d071a4c2c 149 if (Display>2) {
fabiombed 4:1d3d071a4c2c 150 Display=0;
fabiombed 4:1d3d071a4c2c 151 }
fabiombed 4:1d3d071a4c2c 152 if (start==0) {
fabiombed 4:1d3d071a4c2c 153 start=1;
fabiombed 4:1d3d071a4c2c 154 }
fabiombed 4:1d3d071a4c2c 155
fabiombed 1:8f312c1686b6 156 }
fabiombed 1:8f312c1686b6 157
fabiombed 1:8f312c1686b6 158 /* Initialization ------------------------------------------------------------*/
fabiombed 0:becf0d313663 159
fabiombed 1:8f312c1686b6 160 bool Initialization(void)
fabiombed 1:8f312c1686b6 161 {
fabiombed 4:1d3d071a4c2c 162
fabiombed 1:8f312c1686b6 163 /* Initializing Babybear Component. */
fabiombed 1:8f312c1686b6 164 status=board->InitBoard();
fabiombed 1:8f312c1686b6 165 if(status)
fabiombed 1:8f312c1686b6 166 VL6180x_ErrLog("Failed to init the board!\n\r");
fabiombed 0:becf0d313663 167
fabiombed 1:8f312c1686b6 168 // Put GPIO not used as Interrupt in Hi-Z
fabiombed 1:8f312c1686b6 169 status_t=board->sensor_top->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 1:8f312c1686b6 170 //status_b=board->sensor_botton->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF); No Present
fabiombed 4:1d3d071a4c2c 171 status_l=board->sensor_left->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 1:8f312c1686b6 172 status_r=board->sensor_right->SetGPIOxFunctionality(1, GPIOx_SELECT_OFF);
fabiombed 4:1d3d071a4c2c 173
fabiombed 0:becf0d313663 174 /* Initializing Motor Control Component. */
fabiombed 0:becf0d313663 175 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
fabiombed 0:becf0d313663 176 if (motor->Init(NULL) != COMPONENT_OK)
fabiombed 0:becf0d313663 177 return false;
fabiombed 4:1d3d071a4c2c 178
fabiombed 1:8f312c1686b6 179 motor->SetStepMode(STEP_MODE_1_8); // Default is STEP_MODE_1_16
fabiombed 4:1d3d071a4c2c 180
fabiombed 1:8f312c1686b6 181 /* Set defaults Motor Speed. */
fabiombed 1:8f312c1686b6 182 motor->SetAcceleration(SET_ACC);
fabiombed 1:8f312c1686b6 183 motor->SetDeceleration(SET_DEC);
fabiombed 1:8f312c1686b6 184 motor->SetMaxSpeed(SET_MAX); // Variable by Light/Mems Sensors
fabiombed 1:8f312c1686b6 185 motor->SetMinSpeed(SET_MIN);
fabiombed 4:1d3d071a4c2c 186
fabiombed 1:8f312c1686b6 187 return true;
fabiombed 4:1d3d071a4c2c 188
fabiombed 1:8f312c1686b6 189 }
fabiombed 0:becf0d313663 190
fabiombed 1:8f312c1686b6 191 /* Measure_Babybear ----------------------------------------------------------*/
fabiombed 1:8f312c1686b6 192
fabiombed 1:8f312c1686b6 193 void Measure_Babybear(void)
fabiombed 1:8f312c1686b6 194 {
fabiombed 1:8f312c1686b6 195
fabiombed 1:8f312c1686b6 196 status_l=board->sensor_left->GetMeasurement(als_continuous_polling, &data_sensor_left);
fabiombed 1:8f312c1686b6 197 status_r=board->sensor_right->GetMeasurement(als_continuous_polling, &data_sensor_right);
fabiombed 1:8f312c1686b6 198
fabiombed 1:8f312c1686b6 199 babybear = data_sensor_right.lux - data_sensor_left.lux;
fabiombed 1:8f312c1686b6 200
fabiombed 1:8f312c1686b6 201 diff = abs(babybear);
fabiombed 1:8f312c1686b6 202
fabiombed 4:1d3d071a4c2c 203 if (babybear>0) {
fabiombed 4:1d3d071a4c2c 204 left=0;
fabiombed 4:1d3d071a4c2c 205 right=1;
fabiombed 4:1d3d071a4c2c 206 }
fabiombed 4:1d3d071a4c2c 207 if (babybear<0) {
fabiombed 4:1d3d071a4c2c 208 left=1;
fabiombed 4:1d3d071a4c2c 209 right=0;
fabiombed 4:1d3d071a4c2c 210 }
fabiombed 4:1d3d071a4c2c 211
fabiombed 1:8f312c1686b6 212 }
fabiombed 1:8f312c1686b6 213
fabiombed 1:8f312c1686b6 214 /* Measure_Accelerometer -----------------------------------------------------*/
fabiombed 1:8f312c1686b6 215
fabiombed 1:8f312c1686b6 216 void Measure_Accelerometer(void)
fabiombed 1:8f312c1686b6 217 {
fabiombed 1:8f312c1686b6 218
fabiombed 1:8f312c1686b6 219 accelerometer->Get_X_Axes(acc_data);
fabiombed 1:8f312c1686b6 220
fabiombed 1:8f312c1686b6 221 diff = abs(acc_data[0]);
fabiombed 1:8f312c1686b6 222
fabiombed 4:1d3d071a4c2c 223 if (acc_data[0]>0) {
fabiombed 4:1d3d071a4c2c 224 left=0;
fabiombed 4:1d3d071a4c2c 225 right=1;
fabiombed 4:1d3d071a4c2c 226 }
fabiombed 4:1d3d071a4c2c 227 if (acc_data[0]<0) {
fabiombed 4:1d3d071a4c2c 228 left=1;
fabiombed 4:1d3d071a4c2c 229 right=0;
fabiombed 4:1d3d071a4c2c 230 }
fabiombed 4:1d3d071a4c2c 231
fabiombed 1:8f312c1686b6 232 }
fabiombed 0:becf0d313663 233
fabiombed 1:8f312c1686b6 234 /* Control_Motor -------------------------------------------------------------*/
fabiombed 1:8f312c1686b6 235
fabiombed 1:8f312c1686b6 236 void Control_Motor(void)
fabiombed 4:1d3d071a4c2c 237 {
fabiombed 0:becf0d313663 238
fabiombed 4:1d3d071a4c2c 239 //printf("Diff: %d lux/mems\n\r", diff);
fabiombed 4:1d3d071a4c2c 240 motor->SetMaxSpeed(diff);
fabiombed 4:1d3d071a4c2c 241
fabiombed 4:1d3d071a4c2c 242 if (diff>TOLLERANCE) {
fabiombed 1:8f312c1686b6 243 if (diff <=RANGE_1) {
fabiombed 4:1d3d071a4c2c 244 if (left) {
fabiombed 4:1d3d071a4c2c 245 strcpy(DisplayStr,"E___");
fabiombed 4:1d3d071a4c2c 246 }
fabiombed 4:1d3d071a4c2c 247 if (right) {
fabiombed 4:1d3d071a4c2c 248 strcpy(DisplayStr,"___3");
fabiombed 4:1d3d071a4c2c 249 }
fabiombed 4:1d3d071a4c2c 250 } else if (diff >RANGE_1 & diff <=RANGE_2) {
fabiombed 4:1d3d071a4c2c 251 if (left) {
fabiombed 4:1d3d071a4c2c 252 strcpy(DisplayStr,"E===");
fabiombed 4:1d3d071a4c2c 253 }
fabiombed 4:1d3d071a4c2c 254 if (right) {
fabiombed 4:1d3d071a4c2c 255 strcpy(DisplayStr,"===3");
fabiombed 4:1d3d071a4c2c 256 }
fabiombed 4:1d3d071a4c2c 257 } else if (diff >RANGE_2) {
fabiombed 4:1d3d071a4c2c 258 if (left) {
fabiombed 4:1d3d071a4c2c 259 strcpy(DisplayStr,"E~~~");
fabiombed 4:1d3d071a4c2c 260 }
fabiombed 4:1d3d071a4c2c 261 if (right) {
fabiombed 4:1d3d071a4c2c 262 strcpy(DisplayStr,"~~~3");
fabiombed 4:1d3d071a4c2c 263 }
fabiombed 4:1d3d071a4c2c 264 }
fabiombed 4:1d3d071a4c2c 265
fabiombed 1:8f312c1686b6 266 // In Case of Change Direction
fabiombed 4:1d3d071a4c2c 267 if (left & dir==2) {
fabiombed 4:1d3d071a4c2c 268 changedir=1;
fabiombed 4:1d3d071a4c2c 269 }
fabiombed 4:1d3d071a4c2c 270 if (right & dir==1) {
fabiombed 4:1d3d071a4c2c 271 changedir=1;
fabiombed 4:1d3d071a4c2c 272 }
fabiombed 4:1d3d071a4c2c 273
fabiombed 1:8f312c1686b6 274 // Run only if Stop or Change Direction
fabiombed 1:8f312c1686b6 275 if (diff>TOLLERANCE & (dir==0 | changedir==1)) {
fabiombed 4:1d3d071a4c2c 276 if (left) {
fabiombed 4:1d3d071a4c2c 277 motor->Run(StepperMotor::FWD);
fabiombed 4:1d3d071a4c2c 278 dir=1;
fabiombed 4:1d3d071a4c2c 279 changedir=0;
fabiombed 4:1d3d071a4c2c 280 }
fabiombed 4:1d3d071a4c2c 281 if (right) {
fabiombed 4:1d3d071a4c2c 282 motor->Run(StepperMotor::BWD);
fabiombed 4:1d3d071a4c2c 283 dir=2;
fabiombed 4:1d3d071a4c2c 284 changedir=0;
fabiombed 4:1d3d071a4c2c 285 }
fabiombed 4:1d3d071a4c2c 286 }
fabiombed 1:8f312c1686b6 287 }
fabiombed 4:1d3d071a4c2c 288
fabiombed 1:8f312c1686b6 289 // Get Motor Position and Control Rotation Block
fabiombed 1:8f312c1686b6 290 pos = motor->GetPosition();
fabiombed 1:8f312c1686b6 291 if (pos>STOP | pos<-STOP) {
fabiombed 4:1d3d071a4c2c 292 if (pos>0) {
fabiombed 4:1d3d071a4c2c 293 motor->GoTo(STOP);
fabiombed 4:1d3d071a4c2c 294 }
fabiombed 4:1d3d071a4c2c 295 if (pos<0) {
fabiombed 4:1d3d071a4c2c 296 motor->GoTo(-STOP);
fabiombed 4:1d3d071a4c2c 297 }
fabiombed 1:8f312c1686b6 298 }
fabiombed 4:1d3d071a4c2c 299
fabiombed 1:8f312c1686b6 300 // Stop Motor
fabiombed 4:1d3d071a4c2c 301 if (diff<=TOLLERANCE) {
fabiombed 1:8f312c1686b6 302 motor->HardStop();
fabiombed 4:1d3d071a4c2c 303 if (Display==0) {
fabiombed 4:1d3d071a4c2c 304 strcpy(DisplayStr,"----");
fabiombed 4:1d3d071a4c2c 305 }
fabiombed 4:1d3d071a4c2c 306 if (Display==2) {
fabiombed 4:1d3d071a4c2c 307 strcpy(DisplayStr,"E 3");
fabiombed 4:1d3d071a4c2c 308 }
fabiombed 1:8f312c1686b6 309 dir=0;
fabiombed 1:8f312c1686b6 310 changedir=0;
fabiombed 1:8f312c1686b6 311 }
fabiombed 4:1d3d071a4c2c 312
fabiombed 1:8f312c1686b6 313 }
fabiombed 1:8f312c1686b6 314
fabiombed 1:8f312c1686b6 315 /* Measure_SolarPanel --------------------------------------------------------*/
fabiombed 1:8f312c1686b6 316
fabiombed 1:8f312c1686b6 317 void Measure_SolarPanel(void)
fabiombed 1:8f312c1686b6 318 {
fabiombed 1:8f312c1686b6 319
fabiombed 1:8f312c1686b6 320 // AnalogIn A1: 0V return 0.0 , 3.3V return 1.0
fabiombed 1:8f312c1686b6 321 float measure = analog_read_A1.read() * 3300;
fabiombed 1:8f312c1686b6 322 //printf("Measure = %.0f mV\r\n", measure);
fabiombed 1:8f312c1686b6 323 //board->display->DisplayDigit("A", 0);
fabiombed 1:8f312c1686b6 324
fabiombed 4:1d3d071a4c2c 325 if (Display==1) {
fabiombed 4:1d3d071a4c2c 326 sprintf(DisplayStr, "%.0f", measure);
fabiombed 4:1d3d071a4c2c 327 }
fabiombed 4:1d3d071a4c2c 328
fabiombed 4:1d3d071a4c2c 329 board->display->DisplayString(DisplayStr, 4);
fabiombed 1:8f312c1686b6 330
fabiombed 1:8f312c1686b6 331 }
fabiombed 1:8f312c1686b6 332
fabiombed 5:76fb6b783487 333 /* Bluetooth CallBack ---------------------------------------------------------*/
fabiombed 5:76fb6b783487 334
fabiombed 5:76fb6b783487 335 static void onUpdatesEnabledCallback(GattAttribute::Handle_t handle)
fabiombed 5:76fb6b783487 336 {
fabiombed 5:76fb6b783487 337
fabiombed 5:76fb6b783487 338 }
fabiombed 5:76fb6b783487 339
fabiombed 5:76fb6b783487 340 static void onUpdatesDisabledCallback(Gap::Handle_t handle)
fabiombed 5:76fb6b783487 341 {
fabiombed 5:76fb6b783487 342
fabiombed 5:76fb6b783487 343 }
fabiombed 5:76fb6b783487 344
fabiombed 5:76fb6b783487 345 static void onDataReadCallback(const GattReadCallbackParams *eventDataP)
fabiombed 5:76fb6b783487 346 {
fabiombed 5:76fb6b783487 347
fabiombed 5:76fb6b783487 348 }
fabiombed 5:76fb6b783487 349
fabiombed 5:76fb6b783487 350 static void myonDataWriteCallback(const GattWriteCallbackParams *eventDataP)
fabiombed 5:76fb6b783487 351 {
fabiombed 5:76fb6b783487 352
fabiombed 5:76fb6b783487 353 }
fabiombed 5:76fb6b783487 354
fabiombed 5:76fb6b783487 355 static void onConnectionCallback(const Gap::ConnectionCallbackParams_t * connectionParams)
fabiombed 5:76fb6b783487 356 {
fabiombed 5:76fb6b783487 357
fabiombed 5:76fb6b783487 358 }
fabiombed 5:76fb6b783487 359
fabiombed 5:76fb6b783487 360 static void onDisconnectionCallback(const Gap::DisconnectionCallbackParams_t * disConnectionReason)
fabiombed 5:76fb6b783487 361 {
fabiombed 5:76fb6b783487 362
fabiombed 5:76fb6b783487 363 }
fabiombed 5:76fb6b783487 364
fabiombed 5:76fb6b783487 365 /* Bluetooth Initialization ---------------------------------------------------*/
fabiombed 5:76fb6b783487 366
fabiombed 5:76fb6b783487 367 void BLE_Initialization(void)
fabiombed 5:76fb6b783487 368 {
fabiombed 5:76fb6b783487 369
fabiombed 5:76fb6b783487 370 p_BLEdev = new BLE;
fabiombed 5:76fb6b783487 371 if (!p_BLEdev) { printf("\r\nBLE Device creation failed\r\n"); }
fabiombed 5:76fb6b783487 372
fabiombed 5:76fb6b783487 373 const Gap::Address_t BLE_address_BE = {BLE_DEV_MAC};
fabiombed 5:76fb6b783487 374 p_BLEdev->gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE);
fabiombed 5:76fb6b783487 375 printf("Line: %d \r\n", __LINE__);
fabiombed 5:76fb6b783487 376 p_BLEdev->init();
fabiombed 5:76fb6b783487 377 printf("Line: %d \r\n", __LINE__);
fabiombed 5:76fb6b783487 378
fabiombed 5:76fb6b783487 379 // Set BLE CallBack Functions
fabiombed 5:76fb6b783487 380 p_BLEdev->gattServer().onUpdatesEnabled(onUpdatesEnabledCallback);
fabiombed 5:76fb6b783487 381 p_BLEdev->gattServer().onUpdatesDisabled(onUpdatesDisabledCallback);
fabiombed 5:76fb6b783487 382 p_BLEdev->gattServer().onDataRead(onDataReadCallback);
fabiombed 5:76fb6b783487 383 p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback);
fabiombed 5:76fb6b783487 384 p_BLEdev->gap().onConnection(onConnectionCallback);
fabiombed 5:76fb6b783487 385 p_BLEdev->gap().onDisconnection(onDisconnectionCallback);
fabiombed 5:76fb6b783487 386 //p_BLEdev->gattServer().onConfirmationReceived(onConfirmationReceivedCallback);
fabiombed 5:76fb6b783487 387 //p_BLEdev->gattServer().onDataSent(onDataSentCallback);
fabiombed 5:76fb6b783487 388 //p_BLEdev->gap().onTimeout(onTimeoutCallback);
fabiombed 5:76fb6b783487 389
fabiombed 5:76fb6b783487 390 }
fabiombed 5:76fb6b783487 391
fabiombed 1:8f312c1686b6 392 /* Main ----------------------------------------------------------------------*/
fabiombed 1:8f312c1686b6 393
fabiombed 1:8f312c1686b6 394 int main()
fabiombed 4:1d3d071a4c2c 395 {
fabiombed 4:1d3d071a4c2c 396
fabiombed 4:1d3d071a4c2c 397 Initialization();
fabiombed 4:1d3d071a4c2c 398
fabiombed 5:76fb6b783487 399 //BLE_Initialization();
fabiombed 5:76fb6b783487 400
fabiombed 1:8f312c1686b6 401 mybutton.fall(&User_Button_Pressed);
fabiombed 4:1d3d071a4c2c 402
fabiombed 0:becf0d313663 403 /* Printing to the console. */
fabiombed 1:8f312c1686b6 404 printf("SunTracker by Fabio Brembilla\r\n\n");
fabiombed 4:1d3d071a4c2c 405
fabiombed 1:8f312c1686b6 406 /* Set Babybears. */
fabiombed 1:8f312c1686b6 407 status_l=board->sensor_left->AlsSetAnalogueGain(3);
fabiombed 1:8f312c1686b6 408 status_r=board->sensor_right->AlsSetAnalogueGain(3);
fabiombed 1:8f312c1686b6 409 status_l=board->sensor_left->StartMeasurement(als_continuous_polling, NULL, NULL, NULL);
fabiombed 1:8f312c1686b6 410 status_r=board->sensor_right->StartMeasurement(als_continuous_polling, NULL, NULL, NULL);
fabiombed 4:1d3d071a4c2c 411
fabiombed 1:8f312c1686b6 412 /* Loop until push User Button to Set 0 Point. */
fabiombed 1:8f312c1686b6 413 strcpy(DisplayStr,"pusH");
fabiombed 4:1d3d071a4c2c 414 while(start<1) {
fabiombed 1:8f312c1686b6 415 board->display->DisplayString(DisplayStr, 4);
fabiombed 1:8f312c1686b6 416 }
fabiombed 4:1d3d071a4c2c 417
fabiombed 0:becf0d313663 418 /* Main Loop. */
fabiombed 4:1d3d071a4c2c 419 while(true) {
fabiombed 4:1d3d071a4c2c 420 if (Display==0 | Display==1) {
fabiombed 4:1d3d071a4c2c 421 Measure_Babybear();
fabiombed 4:1d3d071a4c2c 422 }
fabiombed 4:1d3d071a4c2c 423 if (Display==2) {
fabiombed 4:1d3d071a4c2c 424 Measure_Accelerometer();
fabiombed 4:1d3d071a4c2c 425 }
fabiombed 4:1d3d071a4c2c 426
fabiombed 1:8f312c1686b6 427 Control_Motor();
fabiombed 1:8f312c1686b6 428 Measure_SolarPanel();
fabiombed 0:becf0d313663 429 }
fabiombed 4:1d3d071a4c2c 430
fabiombed 1:8f312c1686b6 431 status_l=board->sensor_left->StopMeasurement(als_continuous_polling);
fabiombed 1:8f312c1686b6 432 status_r=board->sensor_right->StopMeasurement(als_continuous_polling);
fabiombed 4:1d3d071a4c2c 433
fabiombed 0:becf0d313663 434 }