First release.

Dependencies:   FXOS8700CQ SDFileSystem mbed

Final program

  • Controling a robot by Bluetooth, it is capable of making 90° turns and move pre-defined distances.

Code by:

  • Mayumi Hori
  • Sarahí Morán
  • Gerardo Carmona
Committer:
gerardo_carmona
Date:
Fri Nov 07 05:12:50 2014 +0000
Revision:
5:b384cf06de76
Parent:
4:c60636c95b80
Not finished!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gerardo_carmona 0:3a322aad8c88 1 // ----- Libraries ------------------------------------------------------------------
gerardo_carmona 0:3a322aad8c88 2 #include "mbed.h"
gerardo_carmona 0:3a322aad8c88 3 #include "FXOS8700CQ.h"
gerardo_carmona 1:ab09b233da7b 4 #include "magnometer.h"
gerardo_carmona 0:3a322aad8c88 5
gerardo_carmona 0:3a322aad8c88 6 // ----- I/O Pins -------------------------------------------------------------------
gerardo_carmona 0:3a322aad8c88 7 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
gerardo_carmona 0:3a322aad8c88 8
gerardo_carmona 0:3a322aad8c88 9 // ----- Others ---------------------------------------------------------------------
gerardo_carmona 0:3a322aad8c88 10 SRAWDATA accel_data;
gerardo_carmona 0:3a322aad8c88 11 SRAWDATA magn_data;
gerardo_carmona 0:3a322aad8c88 12
gerardo_carmona 0:3a322aad8c88 13 // ----- Variables ------------------------------------------------------------------
gerardo_carmona 0:3a322aad8c88 14 double mag_x, mag_y;
gerardo_carmona 1:ab09b233da7b 15 double avg_x, avg_y;
gerardo_carmona 1:ab09b233da7b 16 //double average;
gerardo_carmona 0:3a322aad8c88 17
gerardo_carmona 0:3a322aad8c88 18 // ----- Functions ------------------------------------------------------------------
gerardo_carmona 4:c60636c95b80 19 void init_magnometer(){
gerardo_carmona 4:c60636c95b80 20 fxos.enable();
gerardo_carmona 4:c60636c95b80 21 }
gerardo_carmona 4:c60636c95b80 22
gerardo_carmona 0:3a322aad8c88 23 double get_mag_x(){
gerardo_carmona 0:3a322aad8c88 24 fxos.get_data(&accel_data, &magn_data);
gerardo_carmona 0:3a322aad8c88 25 return magn_data.x;
gerardo_carmona 0:3a322aad8c88 26 }
gerardo_carmona 0:3a322aad8c88 27
gerardo_carmona 0:3a322aad8c88 28 double get_mag_y(){
gerardo_carmona 0:3a322aad8c88 29 fxos.get_data(&accel_data, &magn_data);
gerardo_carmona 0:3a322aad8c88 30 return magn_data.y;
gerardo_carmona 0:3a322aad8c88 31 }
gerardo_carmona 0:3a322aad8c88 32
gerardo_carmona 0:3a322aad8c88 33 double get_mag_angle(){
gerardo_carmona 0:3a322aad8c88 34 double _angle;
gerardo_carmona 0:3a322aad8c88 35
gerardo_carmona 0:3a322aad8c88 36 for (int i = 0; i < 10; i++){
gerardo_carmona 0:3a322aad8c88 37 fxos.get_data(&accel_data, &magn_data);
gerardo_carmona 0:3a322aad8c88 38 avg_x += magn_data.x;
gerardo_carmona 0:3a322aad8c88 39 avg_y += magn_data.y;
gerardo_carmona 0:3a322aad8c88 40 //pc.printf("X: %d \tY: %d\r\n", magn_data.x, magn_data.y);
gerardo_carmona 0:3a322aad8c88 41 //pc.printf("%d\n", magn_data.x);
gerardo_carmona 0:3a322aad8c88 42 }
gerardo_carmona 0:3a322aad8c88 43
gerardo_carmona 4:c60636c95b80 44 avg_x = avg_x / 10.0;
gerardo_carmona 4:c60636c95b80 45 avg_y = avg_y / 10.0;
gerardo_carmona 0:3a322aad8c88 46
gerardo_carmona 1:ab09b233da7b 47 //pc.printf("%d\n", avg_x);
gerardo_carmona 4:c60636c95b80 48 _angle = atan2(-avg_y, avg_x) * 180.0 / 3.14159;
gerardo_carmona 0:3a322aad8c88 49 return _angle;
gerardo_carmona 0:3a322aad8c88 50 }