Программа считывает показания датчиков и управляет сервомашинками.

Dependencies:   mbed-src

Fork of NUCLEO_BLUENRG by Ostap Ostapsky

Committer:
Sergeev
Date:
Mon Aug 25 09:45:34 2014 +0000
Revision:
1:fb307cfca15c
Parent:
0:aa1e012ec210
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ostapsky 0:aa1e012ec210 1 #include "mbed.h"
ostapsky 0:aa1e012ec210 2 #include "tag_math.h"
ostapsky 0:aa1e012ec210 3 #include "stm32f4xx_hal.h"
ostapsky 0:aa1e012ec210 4 #include "bluenrg_shield_bsp.h"
ostapsky 0:aa1e012ec210 5 #include "osal.h"
ostapsky 0:aa1e012ec210 6 #include "sample_service.h"
ostapsky 0:aa1e012ec210 7 #include <stdio.h>
ostapsky 0:aa1e012ec210 8
ostapsky 0:aa1e012ec210 9 #define BDADDR_SIZE 6
ostapsky 0:aa1e012ec210 10
ostapsky 0:aa1e012ec210 11 /* SPI handler declaration */
ostapsky 0:aa1e012ec210 12 SPI_HandleTypeDef SpiHandle;
ostapsky 0:aa1e012ec210 13
ostapsky 0:aa1e012ec210 14 Serial pc(SERIAL_TX, SERIAL_RX);
Sergeev 1:fb307cfca15c 15 PwmOut mypwm(D2);
Sergeev 1:fb307cfca15c 16 AnalogIn analog_value(A2);
Sergeev 1:fb307cfca15c 17 int flag = 0;
ostapsky 0:aa1e012ec210 18
ostapsky 0:aa1e012ec210 19 extern volatile uint8_t set_connectable;
ostapsky 0:aa1e012ec210 20 extern volatile int connected;
ostapsky 0:aa1e012ec210 21 extern volatile uint8_t notification_enabled;
ostapsky 0:aa1e012ec210 22
ostapsky 0:aa1e012ec210 23 void User_Process(void);
ostapsky 0:aa1e012ec210 24
ostapsky 0:aa1e012ec210 25
ostapsky 0:aa1e012ec210 26 /******************************************************************************/
ostapsky 0:aa1e012ec210 27 /******************************************************************************/
ostapsky 0:aa1e012ec210 28 int main(void)
ostapsky 0:aa1e012ec210 29 {
ostapsky 0:aa1e012ec210 30 tHalUint8 CLIENT_BDADDR[] = {0xbb, 0x00, 0x00, 0xE1, 0x80, 0x02};
ostapsky 0:aa1e012ec210 31
ostapsky 0:aa1e012ec210 32 tHalUint8 bdaddr[BDADDR_SIZE];
ostapsky 0:aa1e012ec210 33
ostapsky 0:aa1e012ec210 34 uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
ostapsky 0:aa1e012ec210 35 int ret;
ostapsky 0:aa1e012ec210 36
ostapsky 0:aa1e012ec210 37 /* Hardware init*/
ostapsky 0:aa1e012ec210 38 HAL_Init();
ostapsky 0:aa1e012ec210 39
ostapsky 0:aa1e012ec210 40 /* Configure the system clock */
ostapsky 0:aa1e012ec210 41 SystemClock_Config();
ostapsky 0:aa1e012ec210 42
Sergeev 1:fb307cfca15c 43 mypwm.period_ms(20);
Sergeev 1:fb307cfca15c 44 //mypwm.pulsewidth_ms(1.5);
Sergeev 1:fb307cfca15c 45
ostapsky 0:aa1e012ec210 46 /* Initialize the BlueNRG SPI driver */
ostapsky 0:aa1e012ec210 47 BNRG_SPI_Init();
ostapsky 0:aa1e012ec210 48
Sergeev 1:fb307cfca15c 49
ostapsky 0:aa1e012ec210 50 /* Initialize the BlueNRG HCI */
ostapsky 0:aa1e012ec210 51 HCI_Init();
ostapsky 0:aa1e012ec210 52
ostapsky 0:aa1e012ec210 53 /* Reset BlueNRG hardware */
ostapsky 0:aa1e012ec210 54 BlueNRG_RST();
ostapsky 0:aa1e012ec210 55
ostapsky 0:aa1e012ec210 56 Osal_MemCpy(bdaddr, CLIENT_BDADDR, sizeof(CLIENT_BDADDR));
ostapsky 0:aa1e012ec210 57
ostapsky 0:aa1e012ec210 58 ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET,
ostapsky 0:aa1e012ec210 59 CONFIG_DATA_PUBADDR_LEN,
ostapsky 0:aa1e012ec210 60 bdaddr);
ostapsky 0:aa1e012ec210 61 if(ret){
ostapsky 0:aa1e012ec210 62 pc.printf("Setting BD_ADDR failed.\n");
ostapsky 0:aa1e012ec210 63 }
ostapsky 0:aa1e012ec210 64
ostapsky 0:aa1e012ec210 65 ret = aci_gatt_init();
ostapsky 0:aa1e012ec210 66 if(ret){
ostapsky 0:aa1e012ec210 67 pc.printf("GATT_Init failed.\n");
ostapsky 0:aa1e012ec210 68 }
ostapsky 0:aa1e012ec210 69
ostapsky 0:aa1e012ec210 70 ret = aci_gap_init(GAP_CENTRAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle);
ostapsky 0:aa1e012ec210 71
ostapsky 0:aa1e012ec210 72 if(ret != BLE_STATUS_SUCCESS){
ostapsky 0:aa1e012ec210 73 pc.printf("GAP_Init failed.\n");
ostapsky 0:aa1e012ec210 74 }
ostapsky 0:aa1e012ec210 75
ostapsky 0:aa1e012ec210 76 ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED,
ostapsky 0:aa1e012ec210 77 OOB_AUTH_DATA_ABSENT,
ostapsky 0:aa1e012ec210 78 NULL,
ostapsky 0:aa1e012ec210 79 7,
ostapsky 0:aa1e012ec210 80 16,
ostapsky 0:aa1e012ec210 81 USE_FIXED_PIN_FOR_PAIRING,
ostapsky 0:aa1e012ec210 82 123456,
ostapsky 0:aa1e012ec210 83 BONDING);
ostapsky 0:aa1e012ec210 84
ostapsky 0:aa1e012ec210 85
ostapsky 0:aa1e012ec210 86 if (ret != BLE_STATUS_SUCCESS) {
ostapsky 0:aa1e012ec210 87 pc.printf("BLE Stack Initialization failed.\n");
ostapsky 0:aa1e012ec210 88 }
ostapsky 0:aa1e012ec210 89
ostapsky 0:aa1e012ec210 90 /* Set output power level */
ostapsky 0:aa1e012ec210 91 ret = aci_hal_set_tx_power_level(1,4);
ostapsky 0:aa1e012ec210 92
Sergeev 1:fb307cfca15c 93 static uint32_t cnt1=0;
Sergeev 1:fb307cfca15c 94
ostapsky 0:aa1e012ec210 95 /* Infinite loop */
ostapsky 0:aa1e012ec210 96 while (1)
ostapsky 0:aa1e012ec210 97 {
Sergeev 1:fb307cfca15c 98 /*
Sergeev 1:fb307cfca15c 99 for(float offset=0.0; offset<0.001; offset+=0.0001) {
Sergeev 1:fb307cfca15c 100 mypwm.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
Sergeev 1:fb307cfca15c 101 wait(0.25);
Sergeev 1:fb307cfca15c 102 }
Sergeev 1:fb307cfca15c 103 */
Sergeev 1:fb307cfca15c 104
Sergeev 1:fb307cfca15c 105 if (HAL_GetTick() > (cnt1 + 1000))
Sergeev 1:fb307cfca15c 106 {
Sergeev 1:fb307cfca15c 107 cnt1=HAL_GetTick();
Sergeev 1:fb307cfca15c 108 if (flag==0){
Sergeev 1:fb307cfca15c 109 mypwm.period_ms(20);
Sergeev 1:fb307cfca15c 110 mypwm.pulsewidth_ms(0.9);
Sergeev 1:fb307cfca15c 111 flag++;
Sergeev 1:fb307cfca15c 112 }
Sergeev 1:fb307cfca15c 113 else if (flag==1){
Sergeev 1:fb307cfca15c 114 mypwm.period_ms(20);
Sergeev 1:fb307cfca15c 115 mypwm.pulsewidth_ms(1.5);
Sergeev 1:fb307cfca15c 116 flag++;
Sergeev 1:fb307cfca15c 117 }
Sergeev 1:fb307cfca15c 118 else if (flag==2){
Sergeev 1:fb307cfca15c 119 mypwm.period_ms(20);
Sergeev 1:fb307cfca15c 120 mypwm.pulsewidth_ms(2.0);
Sergeev 1:fb307cfca15c 121 flag=0;
Sergeev 1:fb307cfca15c 122 }
Sergeev 1:fb307cfca15c 123 }
ostapsky 0:aa1e012ec210 124 HCI_Process();
ostapsky 0:aa1e012ec210 125 User_Process();
ostapsky 0:aa1e012ec210 126 }
ostapsky 0:aa1e012ec210 127
ostapsky 0:aa1e012ec210 128 }
ostapsky 0:aa1e012ec210 129
ostapsky 0:aa1e012ec210 130 /******************************************************************************/
ostapsky 0:aa1e012ec210 131 /******************************************************************************/
ostapsky 0:aa1e012ec210 132
ostapsky 0:aa1e012ec210 133 void User_Process(void)
ostapsky 0:aa1e012ec210 134 {
ostapsky 0:aa1e012ec210 135 static uint32_t cnt;
ostapsky 0:aa1e012ec210 136 static uint8_t acc_en_sent = 0;
ostapsky 0:aa1e012ec210 137 tHalUint8 data[2];
Sergeev 1:fb307cfca15c 138 uint8_t* temp;
ostapsky 0:aa1e012ec210 139 //uint8_t data_length;
ostapsky 0:aa1e012ec210 140
ostapsky 0:aa1e012ec210 141 if(set_connectable){
ostapsky 0:aa1e012ec210 142 Make_Connection();
ostapsky 0:aa1e012ec210 143 set_connectable = FALSE;
ostapsky 0:aa1e012ec210 144 }
ostapsky 0:aa1e012ec210 145
ostapsky 0:aa1e012ec210 146 /*if(connected && !notification_enabled) {
ostapsky 0:aa1e012ec210 147 enableNotification();
ostapsky 0:aa1e012ec210 148 }*/
ostapsky 0:aa1e012ec210 149
ostapsky 0:aa1e012ec210 150 if((connected)&&(!acc_en_sent)){
ostapsky 0:aa1e012ec210 151
ostapsky 0:aa1e012ec210 152 HAL_Delay(100);
ostapsky 0:aa1e012ec210 153 data[0] = 0x01;
ostapsky 0:aa1e012ec210 154 sendData(0x0029, data, 1);
ostapsky 0:aa1e012ec210 155
ostapsky 0:aa1e012ec210 156 HAL_Delay(100);
ostapsky 0:aa1e012ec210 157 data[0] = 0x01;
ostapsky 0:aa1e012ec210 158 sendData(0x0031, data, 1);
ostapsky 0:aa1e012ec210 159
ostapsky 0:aa1e012ec210 160 HAL_Delay(100);
ostapsky 0:aa1e012ec210 161 data[0] = 0x01;
ostapsky 0:aa1e012ec210 162 sendData(0x003C, data, 1);
ostapsky 0:aa1e012ec210 163
ostapsky 0:aa1e012ec210 164 HAL_Delay(100);
ostapsky 0:aa1e012ec210 165 data[0] = 0x10;
ostapsky 0:aa1e012ec210 166 sendData(0x0034, data, 1);
ostapsky 0:aa1e012ec210 167
ostapsky 0:aa1e012ec210 168
ostapsky 0:aa1e012ec210 169 acc_en_sent = 1;
ostapsky 0:aa1e012ec210 170 cnt = HAL_GetTick();
ostapsky 0:aa1e012ec210 171 }
ostapsky 0:aa1e012ec210 172
Sergeev 1:fb307cfca15c 173 if (HAL_GetTick() > (cnt +2000))
ostapsky 0:aa1e012ec210 174 {
ostapsky 0:aa1e012ec210 175 cnt = HAL_GetTick();
ostapsky 0:aa1e012ec210 176 if (connected && acc_en_sent)
ostapsky 0:aa1e012ec210 177 {
Sergeev 1:fb307cfca15c 178
ostapsky 0:aa1e012ec210 179
Sergeev 1:fb307cfca15c 180 float sheint = analog_value.read();
Sergeev 1:fb307cfca15c 181 pc.printf("Light = %f;\n", sheint);
ostapsky 0:aa1e012ec210 182 pc.printf("T = %f; ", calcTmpTarget(readValue(0x25, NULL)));
Sergeev 1:fb307cfca15c 183 temp = readValue(0x2D, NULL);
ostapsky 0:aa1e012ec210 184 pc.printf("Ax = %d; Ay = %d; Az = %d ", (signed char) temp[0], (signed char) temp[1], (signed char) temp[2]);
ostapsky 0:aa1e012ec210 185 temp = readValue(0x38, NULL);
ostapsky 0:aa1e012ec210 186 pc.printf("H = %f; \r\n", calcHumRel(temp));
ostapsky 0:aa1e012ec210 187
ostapsky 0:aa1e012ec210 188
Sergeev 1:fb307cfca15c 189 /*temp = readValue(0x2D, NULL);
Sergeev 1:fb307cfca15c 190 pc.printf("%d %d \r\n", (signed char) temp[0], (signed char) temp[1]);*/
Sergeev 1:fb307cfca15c 191
Sergeev 1:fb307cfca15c 192 /*pc.printf("%d ", (signed short)calcTmpTarget(readValue(0x25, NULL)));
Sergeev 1:fb307cfca15c 193 temp = readValue(0x38, NULL);
Sergeev 1:fb307cfca15c 194 pc.printf("%d \r\n", (signed short)calcHumRel(temp));*/
Sergeev 1:fb307cfca15c 195
ostapsky 0:aa1e012ec210 196 //GATT services scan
ostapsky 0:aa1e012ec210 197 /*for (unsigned char j = 1; j < 0x88; j++)
ostapsky 0:aa1e012ec210 198 {
ostapsky 0:aa1e012ec210 199 pc.printf("\n %.2X: ", j);
ostapsky 0:aa1e012ec210 200 uint8_t* temp = readValue(j+1, &data_length);
ostapsky 0:aa1e012ec210 201
ostapsky 0:aa1e012ec210 202
ostapsky 0:aa1e012ec210 203 for(int i = 0; i < data_length; i++) {
ostapsky 0:aa1e012ec210 204 pc.printf("%.2X:", temp[i]);
ostapsky 0:aa1e012ec210 205 }
ostapsky 0:aa1e012ec210 206
ostapsky 0:aa1e012ec210 207 for(int i = 0; i < data_length; i++) {
ostapsky 0:aa1e012ec210 208 pc.printf("%c", temp[i]);
ostapsky 0:aa1e012ec210 209 }
ostapsky 0:aa1e012ec210 210
ostapsky 0:aa1e012ec210 211 }
ostapsky 0:aa1e012ec210 212 */
ostapsky 0:aa1e012ec210 213
ostapsky 0:aa1e012ec210 214 }
ostapsky 0:aa1e012ec210 215 }
ostapsky 0:aa1e012ec210 216
ostapsky 0:aa1e012ec210 217 }
ostapsky 0:aa1e012ec210 218 /******************************************************************************/
ostapsky 0:aa1e012ec210 219 /******************************************************************************/
ostapsky 0:aa1e012ec210 220
ostapsky 0:aa1e012ec210 221 /*********************** END OF FILE ******************************************/