cc

Dependencies:   mbed

Fork of AS5048 by Saxion Lectoraat MT

Committer:
pirottealex
Date:
Tue Mar 27 13:27:06 2018 +0000
Revision:
5:6b4cee1dd258
cc;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pirottealex 5:6b4cee1dd258 1 #include "mbed.h"
pirottealex 5:6b4cee1dd258 2 #include <as5048spi.h>
pirottealex 5:6b4cee1dd258 3 #define PI 3.1415926
pirottealex 5:6b4cee1dd258 4
pirottealex 5:6b4cee1dd258 5 // The sensors connection are attached to pins 5-8
pirottealex 5:6b4cee1dd258 6 As5048Spi sensor1(p5, p6, p7,1);
pirottealex 5:6b4cee1dd258 7
pirottealex 5:6b4cee1dd258 8
pirottealex 5:6b4cee1dd258 9 Serial pc(USBTX, USBRX); // tx, rx
pirottealex 5:6b4cee1dd258 10 float vitesse,delta_deg,tour_sec;
pirottealex 5:6b4cee1dd258 11 int degrees_av,degrees;
pirottealex 5:6b4cee1dd258 12 Timer delta_t;
pirottealex 5:6b4cee1dd258 13
pirottealex 5:6b4cee1dd258 14
pirottealex 5:6b4cee1dd258 15 int main() {
pirottealex 5:6b4cee1dd258 16 pc.baud(38400);
pirottealex 5:6b4cee1dd258 17 delta_t.start();
pirottealex 5:6b4cee1dd258 18 while(1) {
pirottealex 5:6b4cee1dd258 19 //
pirottealex 5:6b4cee1dd258 20 const int* angles = sensor1.read_angle();
pirottealex 5:6b4cee1dd258 21
pirottealex 5:6b4cee1dd258 22 int angle = angles[0];
pirottealex 5:6b4cee1dd258 23
pirottealex 5:6b4cee1dd258 24 // The read angle returns the value returned over the SPI bus, including parity bit
pirottealex 5:6b4cee1dd258 25 //pc.printf("Read result: %x\r\n", angle);
pirottealex 5:6b4cee1dd258 26
pirottealex 5:6b4cee1dd258 27 if( As5048Spi::parity_check(angle) )
pirottealex 5:6b4cee1dd258 28 {
pirottealex 5:6b4cee1dd258 29 degrees = As5048Spi::degrees(angle)/100;
pirottealex 5:6b4cee1dd258 30 pc.printf("deg=%d\n\r", degrees);
pirottealex 5:6b4cee1dd258 31 // Convert range from 0 to 2^14-1 to 0 - 360 degrees
pirottealex 5:6b4cee1dd258 32 /*degrees_av=degrees;
pirottealex 5:6b4cee1dd258 33 degrees = As5048Spi::degrees(angle)/100;
pirottealex 5:6b4cee1dd258 34 delta_deg=(degrees-degrees_av);
pirottealex 5:6b4cee1dd258 35 if(delta_deg<0)
pirottealex 5:6b4cee1dd258 36 {
pirottealex 5:6b4cee1dd258 37 delta_deg=delta_deg+360;
pirottealex 5:6b4cee1dd258 38 }
pirottealex 5:6b4cee1dd258 39 tour_sec=((delta_deg)/delta_t.read())/(11*360);
pirottealex 5:6b4cee1dd258 40 vitesse=tour_sec*5.9*PI;
pirottealex 5:6b4cee1dd258 41 delta_t.reset();
pirottealex 5:6b4cee1dd258 42 //vitesse=vitesse/(6.283185*11);
pirottealex 5:6b4cee1dd258 43
pirottealex 5:6b4cee1dd258 44 pc.printf("vitesse=%0.2f m/sec \n\r",vitesse);
pirottealex 5:6b4cee1dd258 45
pirottealex 5:6b4cee1dd258 46 //pc.printf("Parity check succesfull.\r\n");
pirottealex 5:6b4cee1dd258 47
pirottealex 5:6b4cee1dd258 48 //pc.printf("Angle: %i degrees\r\n", degrees );*/
pirottealex 5:6b4cee1dd258 49 }
pirottealex 5:6b4cee1dd258 50 else
pirottealex 5:6b4cee1dd258 51 {
pirottealex 5:6b4cee1dd258 52 //pc.printf("Parity check failed.\r\n");
pirottealex 5:6b4cee1dd258 53 }
pirottealex 5:6b4cee1dd258 54
pirottealex 5:6b4cee1dd258 55 }
pirottealex 5:6b4cee1dd258 56 }
pirottealex 5:6b4cee1dd258 57