UVic Assistive Technology Lab / Mbed 2 deprecated DSLR_Camera_Gimbal

Dependencies:   mbed ros_lib_kinetic

Committer:
group-UVic-Assistive-Technolog
Date:
Wed Jan 31 05:24:12 2018 +0000
Revision:
0:3a767f41cf04
Child:
1:1ac7d472cfa2
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-UVic-Assistive-Technolog 0:3a767f41cf04 1
group-UVic-Assistive-Technolog 0:3a767f41cf04 2 #include <stdint.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 3 #include "mbed.h"
group-UVic-Assistive-Technolog 0:3a767f41cf04 4 #include <ros.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 5 #include <std_msgs/Empty.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 6 #include <std_msgs/Float32MultiArray.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 7 #include <BNO055.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 8 #include <initializations.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 9 #include <definitions.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 10 #include <prototypes.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 11 #include <Mx28.h>
group-UVic-Assistive-Technolog 0:3a767f41cf04 12
group-UVic-Assistive-Technolog 0:3a767f41cf04 13 void runLift(void){
group-UVic-Assistive-Technolog 0:3a767f41cf04 14 if (liftFlag && control.liftRun){
group-UVic-Assistive-Technolog 0:3a767f41cf04 15 liftFlag = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 16 // Set direction
group-UVic-Assistive-Technolog 0:3a767f41cf04 17 if(control.height > currentPosition){
group-UVic-Assistive-Technolog 0:3a767f41cf04 18 liftDirection.write(LIFTUP);
group-UVic-Assistive-Technolog 0:3a767f41cf04 19 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 20 else if(control.height < currentPosition){
group-UVic-Assistive-Technolog 0:3a767f41cf04 21 liftDirection.write(LIFTDOWN);
group-UVic-Assistive-Technolog 0:3a767f41cf04 22 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 23 // Check if arrived at destination height.
group-UVic-Assistive-Technolog 0:3a767f41cf04 24 if (control.height == currentPosition){
group-UVic-Assistive-Technolog 0:3a767f41cf04 25 control.liftRun = FALSE;
group-UVic-Assistive-Technolog 0:3a767f41cf04 26 liftSpeed.write(0);
group-UVic-Assistive-Technolog 0:3a767f41cf04 27 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 28 else {
group-UVic-Assistive-Technolog 0:3a767f41cf04 29 liftSpeed.write(0.5);
group-UVic-Assistive-Technolog 0:3a767f41cf04 30 checkLift(currentPosition, stall);
group-UVic-Assistive-Technolog 0:3a767f41cf04 31 // Check for stalling
group-UVic-Assistive-Technolog 0:3a767f41cf04 32 if(stall && liftSpeed.read() > 0){
group-UVic-Assistive-Technolog 0:3a767f41cf04 33 stall = FALSE;
group-UVic-Assistive-Technolog 0:3a767f41cf04 34 control.liftRun = FALSE;
group-UVic-Assistive-Technolog 0:3a767f41cf04 35 liftSpeed.write(0);
group-UVic-Assistive-Technolog 0:3a767f41cf04 36 if (liftDirection.read() == LIFTUP){
group-UVic-Assistive-Technolog 0:3a767f41cf04 37 currentPosition = LIFTHEIGHTMAX;
group-UVic-Assistive-Technolog 0:3a767f41cf04 38 }else{
group-UVic-Assistive-Technolog 0:3a767f41cf04 39 currentPosition = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 40 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 41 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 42 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 43 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 44 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 45
group-UVic-Assistive-Technolog 0:3a767f41cf04 46 // Keeps track of the rotation of the lift motor. After one full rotation up,
group-UVic-Assistive-Technolog 0:3a767f41cf04 47 // increments *position. If the motor doesn't rotate within
group-UVic-Assistive-Technolog 0:3a767f41cf04 48 // ((STALLTIME * HALLCHECKTIME * 4)/10^6) seconds, will set *stall TRUE.
group-UVic-Assistive-Technolog 0:3a767f41cf04 49 void checkLift(int& position, bool& stall) {
group-UVic-Assistive-Technolog 0:3a767f41cf04 50
group-UVic-Assistive-Technolog 0:3a767f41cf04 51 static char state = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 52 static char hallState1 = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 53 static char hallState2 = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 54 static char hallState1count = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 55 static char hallState2count = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 56 static char debounce = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 57 static int stallcount = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 58 int movementStart = position;
group-UVic-Assistive-Technolog 0:3a767f41cf04 59
group-UVic-Assistive-Technolog 0:3a767f41cf04 60 // Debounce the hall sensors. Check them 3 times, if they are
group-UVic-Assistive-Technolog 0:3a767f41cf04 61 // high for 2 or more, set them as high, else low.
group-UVic-Assistive-Technolog 0:3a767f41cf04 62 if (debounce< 3){
group-UVic-Assistive-Technolog 0:3a767f41cf04 63 if (hallSensor1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 64 hallState1count++;
group-UVic-Assistive-Technolog 0:3a767f41cf04 65 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 66 if (hallSensor2){
group-UVic-Assistive-Technolog 0:3a767f41cf04 67 hallState2count++;
group-UVic-Assistive-Technolog 0:3a767f41cf04 68 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 69 debounce++;
group-UVic-Assistive-Technolog 0:3a767f41cf04 70 }else{
group-UVic-Assistive-Technolog 0:3a767f41cf04 71 if (hallState1count > 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 72 hallState1 = 1;
group-UVic-Assistive-Technolog 0:3a767f41cf04 73 }else{
group-UVic-Assistive-Technolog 0:3a767f41cf04 74 hallState1 = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 75 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 76 if (hallState2count > 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 77 hallState2 = 1;
group-UVic-Assistive-Technolog 0:3a767f41cf04 78 }else{
group-UVic-Assistive-Technolog 0:3a767f41cf04 79 hallState2 = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 80 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 81 hallState1count = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 82 hallState2count = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 83
group-UVic-Assistive-Technolog 0:3a767f41cf04 84 switch (state){
group-UVic-Assistive-Technolog 0:3a767f41cf04 85
group-UVic-Assistive-Technolog 0:3a767f41cf04 86 case 0: // Hall 1 - low, Hall 2 - low
group-UVic-Assistive-Technolog 0:3a767f41cf04 87 if (hallState2 == 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 88 state = 1;
group-UVic-Assistive-Technolog 0:3a767f41cf04 89 position++;
group-UVic-Assistive-Technolog 0:3a767f41cf04 90 } else if (hallState1 == 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 91 state = 3;
group-UVic-Assistive-Technolog 0:3a767f41cf04 92 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 93 break;
group-UVic-Assistive-Technolog 0:3a767f41cf04 94
group-UVic-Assistive-Technolog 0:3a767f41cf04 95 case 1: // Hall 1 - low, Hall 2 - high
group-UVic-Assistive-Technolog 0:3a767f41cf04 96 if (hallState1 == 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 97 state = 2;
group-UVic-Assistive-Technolog 0:3a767f41cf04 98 } else if (hallState2 == 0){
group-UVic-Assistive-Technolog 0:3a767f41cf04 99 state = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 100 position--;
group-UVic-Assistive-Technolog 0:3a767f41cf04 101 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 102 break;
group-UVic-Assistive-Technolog 0:3a767f41cf04 103
group-UVic-Assistive-Technolog 0:3a767f41cf04 104 case 2: // Hall 1 - high, Hall 2 - high
group-UVic-Assistive-Technolog 0:3a767f41cf04 105 if (hallState2 == 0){
group-UVic-Assistive-Technolog 0:3a767f41cf04 106 state = 3;
group-UVic-Assistive-Technolog 0:3a767f41cf04 107 } else if (hallState1 == 0){
group-UVic-Assistive-Technolog 0:3a767f41cf04 108 state = 1;
group-UVic-Assistive-Technolog 0:3a767f41cf04 109 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 110 break;
group-UVic-Assistive-Technolog 0:3a767f41cf04 111
group-UVic-Assistive-Technolog 0:3a767f41cf04 112 case 3: // Hall 1 - high, Hall 2 - low
group-UVic-Assistive-Technolog 0:3a767f41cf04 113 if (hallState1 == 0){
group-UVic-Assistive-Technolog 0:3a767f41cf04 114 state = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 115 } else if (hallState2 == 1){
group-UVic-Assistive-Technolog 0:3a767f41cf04 116 state = 2;
group-UVic-Assistive-Technolog 0:3a767f41cf04 117 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 118 break;
group-UVic-Assistive-Technolog 0:3a767f41cf04 119 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 120 debounce = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 121
group-UVic-Assistive-Technolog 0:3a767f41cf04 122 // Check for stalling
group-UVic-Assistive-Technolog 0:3a767f41cf04 123 if (movementStart == position){
group-UVic-Assistive-Technolog 0:3a767f41cf04 124 stallcount++;
group-UVic-Assistive-Technolog 0:3a767f41cf04 125 if (stallcount >= STALLTIME){
group-UVic-Assistive-Technolog 0:3a767f41cf04 126 stall = TRUE;
group-UVic-Assistive-Technolog 0:3a767f41cf04 127 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 128 } else{
group-UVic-Assistive-Technolog 0:3a767f41cf04 129 stallcount = 0;
group-UVic-Assistive-Technolog 0:3a767f41cf04 130 stall = FALSE;
group-UVic-Assistive-Technolog 0:3a767f41cf04 131 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 132
group-UVic-Assistive-Technolog 0:3a767f41cf04 133 }
group-UVic-Assistive-Technolog 0:3a767f41cf04 134 }