The localization library for SCRIBE

Dependents:   SCRIBE_stepper

Committer:
nibab
Date:
Fri Apr 22 18:30:52 2016 +0000
Revision:
7:fcdba69a0c22
Parent:
6:ee872bf49be6
Child:
8:91db52943f3e
To use the ping sensors: first call measure() to update X and Y and then call getX() and getY() to retrieve X and Y respectively.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nibab 0:a5cac0a5e41d 1 #include "localization.h"
nibab 0:a5cac0a5e41d 2
nibab 0:a5cac0a5e41d 3
nibab 0:a5cac0a5e41d 4 void dist(int distance)
nibab 0:a5cac0a5e41d 5 {
nibab 0:a5cac0a5e41d 6 //put code here to happen when the distance is changed
nibab 0:a5cac0a5e41d 7 printf("Distance1 changed to %dmm\r\n", distance);
nibab 0:a5cac0a5e41d 8 }
nibab 0:a5cac0a5e41d 9
nibab 5:dcce3f262830 10 BNO055 imu(p28, p27);
nibab 7:fcdba69a0c22 11 ultrasonic mu1(p26, p25, .1, 1, &dist);
nibab 7:fcdba69a0c22 12 ultrasonic mu2(p30, p29, .1, 1, &dist);
nibab 6:ee872bf49be6 13 PwmOut pin24(p24);
nibab 0:a5cac0a5e41d 14
nibab 0:a5cac0a5e41d 15 localization::localization(){
nibab 0:a5cac0a5e41d 16 X = 0;
nibab 0:a5cac0a5e41d 17 Y = 0;
nibab 0:a5cac0a5e41d 18 }
nibab 0:a5cac0a5e41d 19
nibab 0:a5cac0a5e41d 20 void localization::reset(){
nibab 6:ee872bf49be6 21 pin24.period_ms(20);
nibab 6:ee872bf49be6 22
nibab 7:fcdba69a0c22 23 mu1.startUpdates();//start mesuring the distance
nibab 7:fcdba69a0c22 24 mu2.startUpdates();
nibab 0:a5cac0a5e41d 25 imu.reset();
nibab 0:a5cac0a5e41d 26 // Check that BNO055 is connected
nibab 0:a5cac0a5e41d 27 if (!imu.check()){
nibab 0:a5cac0a5e41d 28 while (true){
nibab 0:a5cac0a5e41d 29 printf("BNO055 IMU not connected");
nibab 0:a5cac0a5e41d 30 wait(0.1);
nibab 0:a5cac0a5e41d 31 }
nibab 0:a5cac0a5e41d 32 }
nibab 0:a5cac0a5e41d 33 // Display sensor information
nibab 0:a5cac0a5e41d 34 printf("BNO055 found\r\n\r\n");
nibab 0:a5cac0a5e41d 35 printf("Chip ID: %d\r\n",imu.ID.id);
nibab 0:a5cac0a5e41d 36 printf("Accelerometer ID: %d\r\n",imu.ID.accel);
nibab 0:a5cac0a5e41d 37 printf("Gyroscope ID: %d\r\n",imu.ID.gyro);
nibab 0:a5cac0a5e41d 38 printf("Magnetometer ID: %d\r\n\r\n",imu.ID.mag);
nibab 0:a5cac0a5e41d 39 printf("Firmware version v%d.%0d\r\n",imu.ID.sw[0],imu.ID.sw[1]);
nibab 0:a5cac0a5e41d 40 printf("Bootloader version v%d\r\n\r\n",imu.ID.bootload);
nibab 0:a5cac0a5e41d 41 // Display chip serial number
nibab 0:a5cac0a5e41d 42 for (int i = 0; i<4; i++){
nibab 0:a5cac0a5e41d 43 printf("%d.%d.%d.%d\r\n",imu.ID.serial[i*4],imu.ID.serial[i*4+1],imu.ID.serial[i*4+2],imu.ID.serial[i*4+3]);
nibab 0:a5cac0a5e41d 44 }
nibab 0:a5cac0a5e41d 45 printf("\r\n");
nibab 0:a5cac0a5e41d 46 }
nibab 0:a5cac0a5e41d 47
nibab 1:2cd9602780f4 48 float localization::getAngle(){
nibab 1:2cd9602780f4 49 #ifndef VERTICAL //On horizontal plane
nibab 1:2cd9602780f4 50 imu.setmode(OPERATION_MODE_NDOF);
nibab 1:2cd9602780f4 51 imu.get_angles();
nibab 1:2cd9602780f4 52 float angle = imu.euler.yaw;
nibab 1:2cd9602780f4 53 return angle;
nibab 1:2cd9602780f4 54 #else // On vertical plane
nibab 1:2cd9602780f4 55 imu.setmode(OPERATION_MODE_NDOF);
nibab 1:2cd9602780f4 56 imu.get_grv();
nibab 1:2cd9602780f4 57 float gravityX = imu.gravity.x, gravityY = imu.gravity.y;
nibab 1:2cd9602780f4 58 float radian = atan(gravityY/gravityX);
nibab 1:2cd9602780f4 59 float degree = radian * 180 / 3.14159265359;
nibab 1:2cd9602780f4 60 if(gravityX >= 0 && gravityY >= 0)
nibab 1:2cd9602780f4 61 return degree;
nibab 1:2cd9602780f4 62 else if(gravityX < 0)
nibab 1:2cd9602780f4 63 return 180 + degree;
nibab 1:2cd9602780f4 64 else // gravityX >= 0 && gravityY < 0
nibab 1:2cd9602780f4 65 return 360 + degree;
nibab 1:2cd9602780f4 66 #endif
nibab 1:2cd9602780f4 67 }
nibab 1:2cd9602780f4 68
nibab 0:a5cac0a5e41d 69 void localization::measure(){
nibab 7:fcdba69a0c22 70 /*
nibab 0:a5cac0a5e41d 71 while(1){
nibab 0:a5cac0a5e41d 72 imu.setmode(OPERATION_MODE_NDOF);
nibab 0:a5cac0a5e41d 73 imu.get_angles();
nibab 2:e9d692eea8de 74 float angle = this->getAngle();
nibab 2:e9d692eea8de 75 printf("Angle: %.3f\r\n", angle);
nibab 2:e9d692eea8de 76 if(angle < 359.0 && angle >= 180.0){
nibab 0:a5cac0a5e41d 77 //turn right here
nibab 0:a5cac0a5e41d 78 printf("Turn right!\r\n");
nibab 0:a5cac0a5e41d 79 wait(0.1);
nibab 0:a5cac0a5e41d 80 continue;
nibab 2:e9d692eea8de 81 }else if(angle >= 1.0 && angle < 180.0){
nibab 0:a5cac0a5e41d 82 //turn left here
nibab 0:a5cac0a5e41d 83 printf("Turn left!\r\n");
nibab 0:a5cac0a5e41d 84 wait(0.1);
nibab 0:a5cac0a5e41d 85 continue;
nibab 0:a5cac0a5e41d 86 }else{break;}
nibab 0:a5cac0a5e41d 87 }
nibab 7:fcdba69a0c22 88 */
nibab 0:a5cac0a5e41d 89 //mu1.checkDistance(); //call checkDistance() as much as possible, as this is where
nibab 0:a5cac0a5e41d 90 //the class checks if dist needs to be called.
nibab 0:a5cac0a5e41d 91
nibab 0:a5cac0a5e41d 92 int sum1 = 0, sum2 = 0;
nibab 0:a5cac0a5e41d 93 for(int i = 0; i < 50; i++){ // Average over 10 times
nibab 0:a5cac0a5e41d 94 sum1 += mu1.getCurrentDistance();
nibab 0:a5cac0a5e41d 95 sum2 += mu2.getCurrentDistance();
nibab 0:a5cac0a5e41d 96 }
nibab 0:a5cac0a5e41d 97 X = sum1/50;
nibab 7:fcdba69a0c22 98 Y = sum2/50;
nibab 0:a5cac0a5e41d 99 //printf("X is %dmm\r\n", X);
nibab 0:a5cac0a5e41d 100 //printf("Y is %dmm\r\n", Y);
nibab 0:a5cac0a5e41d 101 //wait(0.1);
nibab 0:a5cac0a5e41d 102 }
nibab 0:a5cac0a5e41d 103
nibab 6:ee872bf49be6 104 void localization::servo(int degree){
nibab 6:ee872bf49be6 105 /*Input have to be between 0 and 180*/
nibab 6:ee872bf49be6 106 if (degree > 180)
nibab 6:ee872bf49be6 107 degree = 180;
nibab 6:ee872bf49be6 108 else if (degree < 0)
nibab 6:ee872bf49be6 109 degree = 0;
nibab 6:ee872bf49be6 110 int highTime_us = (int)((degree / 180.0) * 2000) + 500; // Minimum 500us, maximum 2500us
nibab 6:ee872bf49be6 111
nibab 6:ee872bf49be6 112 pin24.pulsewidth_us(highTime_us); //Set the duty cycle
nibab 6:ee872bf49be6 113 }
nibab 6:ee872bf49be6 114
nibab 0:a5cac0a5e41d 115 int localization::getX(){
nibab 0:a5cac0a5e41d 116 return X;
nibab 0:a5cac0a5e41d 117 }
nibab 0:a5cac0a5e41d 118
nibab 0:a5cac0a5e41d 119 int localization::getY(){
nibab 0:a5cac0a5e41d 120 return Y;
nibab 7:fcdba69a0c22 121 }