Smartage application

Dependencies:   BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2

Fork of STM32L0_LoRa by Helmut Tschemernjak

Committer:
marcozecchini
Date:
Fri May 25 16:21:57 2018 +0000
Revision:
25:18a57be7bb17
Parent:
24:bb733d746bda
Child:
26:d93f1206909c
Gyroscope implementation works fine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:c43b6919ae15 1 /*
Helmut64 17:98f2528e8399 2 * Copyright (c) 2018 HELIOS Software GmbH
Helmut64 0:c43b6919ae15 3 * 30826 Garbsen (Hannover) Germany
Helmut64 0:c43b6919ae15 4 * Licensed under the Apache License, Version 2.0);
Helmut64 0:c43b6919ae15 5 */
Helmut64 0:c43b6919ae15 6 #include "main.h"
Helmut64 0:c43b6919ae15 7
Helmut64 0:c43b6919ae15 8
Helmut64 17:98f2528e8399 9 DigitalOut myled(LED);
marcozecchini 19:7763501775e5 10 //D12 TRIGGER D11 ECHO
marcozecchini 19:7763501775e5 11 HCSR04 sensor(D12, D11);
marcozecchini 24:bb733d746bda 12 /* Instantiate the expansion board */
marcozecchini 24:bb733d746bda 13 XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
marcozecchini 24:bb733d746bda 14 volatile int mems_event = 0;
marcozecchini 24:bb733d746bda 15 volatile int notification = 0;
marcozecchini 24:bb733d746bda 16
marcozecchini 24:bb733d746bda 17 /* Sensor initialization */
marcozecchini 24:bb733d746bda 18 HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
marcozecchini 24:bb733d746bda 19 LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
marcozecchini 24:bb733d746bda 20
Helmut64 17:98f2528e8399 21 int main() {
Helmut64 17:98f2528e8399 22 #ifdef HELTEC_STM32L4
Helmut64 17:98f2528e8399 23 DigitalOut vext(POWER_VEXT);
Helmut64 17:98f2528e8399 24 vext = POWER_VEXT_ON;
Helmut64 17:98f2528e8399 25 #endif
Helmut64 17:98f2528e8399 26 /*
Helmut64 17:98f2528e8399 27 * inits the Serial or USBSerial when available (230400 baud).
Helmut64 17:98f2528e8399 28 * If the serial uart is not is not connected it swiches to USB Serial
Helmut64 17:98f2528e8399 29 * blinking LED means USBSerial detected, waiting for a connect.
Helmut64 17:98f2528e8399 30 * It waits up to 30 seconds for a USB terminal connections
Helmut64 17:98f2528e8399 31 */
marcozecchini 19:7763501775e5 32 InitSerial(30*1000, &myled);
marcozecchini 24:bb733d746bda 33 hum_temp->enable();
marcozecchini 24:bb733d746bda 34
marcozecchini 24:bb733d746bda 35 /* Enable LSM6DSL accelerometer */
marcozecchini 24:bb733d746bda 36 acc_gyro->enable_x();
marcozecchini 25:18a57be7bb17 37 /* Enable 6D Orientation. */
marcozecchini 25:18a57be7bb17 38 acc_gyro->enable_6d_orientation();
marcozecchini 24:bb733d746bda 39
marcozecchini 20:1557c9d9c183 40 print_stuff();
marcozecchini 24:bb733d746bda 41 bool empty = true;
marcozecchini 24:bb733d746bda 42 //ADD THE WAIT
marcozecchini 19:7763501775e5 43 while(1){
marcozecchini 19:7763501775e5 44
marcozecchini 24:bb733d746bda 45 char distance[4], empty_distance[4], temperature[4];//Message to be sent
marcozecchini 24:bb733d746bda 46 get_distance(distance);
marcozecchini 24:bb733d746bda 47 get_temperature(temperature);
marcozecchini 25:18a57be7bb17 48 notification = send_orientation();
marcozecchini 24:bb733d746bda 49
marcozecchini 24:bb733d746bda 50 if(empty) {
marcozecchini 24:bb733d746bda 51 memcpy(empty_distance, distance, 4);
marcozecchini 24:bb733d746bda 52 SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); //invia due volte
marcozecchini 24:bb733d746bda 53 empty = false;
marcozecchini 24:bb733d746bda 54 }
marcozecchini 24:bb733d746bda 55 else{
marcozecchini 24:bb733d746bda 56 SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification);
marcozecchini 24:bb733d746bda 57 }
marcozecchini 24:bb733d746bda 58
marcozecchini 19:7763501775e5 59 }
marcozecchini 22:2c1359292de1 60 }
marcozecchini 22:2c1359292de1 61
marcozecchini 22:2c1359292de1 62 void get_distance(char message[]){
marcozecchini 22:2c1359292de1 63 //Ultrasonic measurement
marcozecchini 24:bb733d746bda 64 long distance = 401;
marcozecchini 24:bb733d746bda 65 while (distance >= 400) distance = sensor.distance();
marcozecchini 22:2c1359292de1 66 dprintf("distance %d \n",distance);
marcozecchini 22:2c1359292de1 67
marcozecchini 22:2c1359292de1 68 sprintf(message, "%d", distance);
marcozecchini 24:bb733d746bda 69 }
marcozecchini 24:bb733d746bda 70
marcozecchini 24:bb733d746bda 71 void get_temperature(char message[]){
marcozecchini 24:bb733d746bda 72 float value;
marcozecchini 24:bb733d746bda 73 hum_temp->get_temperature(&value);
marcozecchini 24:bb733d746bda 74 dprintf("temperature %f", value);
marcozecchini 24:bb733d746bda 75
marcozecchini 24:bb733d746bda 76 sprintf(message, "%f", value);
marcozecchini 24:bb733d746bda 77 dprintf(message);
marcozecchini 24:bb733d746bda 78
marcozecchini 25:18a57be7bb17 79 }
marcozecchini 25:18a57be7bb17 80
marcozecchini 25:18a57be7bb17 81 /* Print the orientation. */
marcozecchini 25:18a57be7bb17 82 bool send_orientation() {
marcozecchini 25:18a57be7bb17 83 uint8_t xl = 0;
marcozecchini 25:18a57be7bb17 84 uint8_t xh = 0;
marcozecchini 25:18a57be7bb17 85 uint8_t yl = 0;
marcozecchini 25:18a57be7bb17 86 uint8_t yh = 0;
marcozecchini 25:18a57be7bb17 87 uint8_t zl = 0;
marcozecchini 25:18a57be7bb17 88 uint8_t zh = 0;
marcozecchini 25:18a57be7bb17 89
marcozecchini 25:18a57be7bb17 90 acc_gyro->get_6d_orientation_xl(&xl);
marcozecchini 25:18a57be7bb17 91 acc_gyro->get_6d_orientation_xh(&xh);
marcozecchini 25:18a57be7bb17 92 acc_gyro->get_6d_orientation_yl(&yl);
marcozecchini 25:18a57be7bb17 93 acc_gyro->get_6d_orientation_yh(&yh);
marcozecchini 25:18a57be7bb17 94 acc_gyro->get_6d_orientation_zl(&zl);
marcozecchini 25:18a57be7bb17 95 acc_gyro->get_6d_orientation_zh(&zh);
marcozecchini 25:18a57be7bb17 96
marcozecchini 25:18a57be7bb17 97
marcozecchini 25:18a57be7bb17 98 if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) {
marcozecchini 25:18a57be7bb17 99 return false;
marcozecchini 25:18a57be7bb17 100 }
marcozecchini 25:18a57be7bb17 101
marcozecchini 25:18a57be7bb17 102 else {
marcozecchini 25:18a57be7bb17 103 return true;
marcozecchini 25:18a57be7bb17 104 }
marcozecchini 19:7763501775e5 105 }