Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Committer:
llarose
Date:
Wed Apr 08 14:10:12 2015 +0000
Revision:
43:92c9e58b8a3a
Parent:
40:cf5a55961d8c
Child:
48:bc531288c220
Child:
50:48e8da5fc1ae
FlexSensor adaptation ? la nouvelle plage de tension issue du circuit de conditionnement.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
llarose 1:601d2922ff06 1 #include "FlexSensor.h"
Thierry19 23:5bb76b7c35da 2 #define VALEURFLEX 0
llarose 43:92c9e58b8a3a 3 #define test_flex 1
llarose 43:92c9e58b8a3a 4 #define print_finger_state 1
llarose 43:92c9e58b8a3a 5 #define safety_calibration 0 // if minimum for a finger goes bellow seuil_calibration, it reset its calibration.
llarose 43:92c9e58b8a3a 6 #define SEUIL_CALIBRATION 10000
llarose 17:1a634bb615f3 7 Serial f_pc(USBTX, USBRX);
llarose 43:92c9e58b8a3a 8 DigitalOut led1(LED1);
llarose 43:92c9e58b8a3a 9 DigitalOut led2(LED2);
llarose 43:92c9e58b8a3a 10 DigitalOut led3(LED3);
llarose 43:92c9e58b8a3a 11
llarose 1:601d2922ff06 12 FlexSensor::FlexSensor() //default constructor
llarose 1:601d2922ff06 13 {
pete1801 40:cf5a55961d8c 14 FlexSensor(p18, p19, p20);
llarose 17:1a634bb615f3 15 }
llarose 17:1a634bb615f3 16
llarose 17:1a634bb615f3 17 FlexSensor::FlexSensor(PinName pinIndex, PinName pinMajeur, PinName pinAnnulaire)
llarose 17:1a634bb615f3 18 {
llarose 17:1a634bb615f3 19 this->AinIndex = new AnalogIn(pinIndex);
llarose 17:1a634bb615f3 20 this->AinMajeur = new AnalogIn(pinMajeur);
llarose 17:1a634bb615f3 21 this->AinAnnulaire = new AnalogIn(pinAnnulaire);
llarose 17:1a634bb615f3 22 this->limit = 0xC350;
llarose 17:1a634bb615f3 23
groygirard 12:ebb08773dbdb 24 flex_data.index = 0x01;
groygirard 12:ebb08773dbdb 25 flex_data.majeur = 0x01;
groygirard 12:ebb08773dbdb 26 flex_data.annulaire = 0x01;
llarose 43:92c9e58b8a3a 27
llarose 1:601d2922ff06 28 }
groygirard 12:ebb08773dbdb 29
llarose 1:601d2922ff06 30 FlexSensor::~FlexSensor()
llarose 1:601d2922ff06 31 {
llarose 17:1a634bb615f3 32 delete this->AinIndex;
llarose 17:1a634bb615f3 33 delete this->AinMajeur;
llarose 17:1a634bb615f3 34 delete this->AinAnnulaire;
llarose 17:1a634bb615f3 35 }
llarose 17:1a634bb615f3 36
llarose 17:1a634bb615f3 37 void FlexSensor::Run()
llarose 17:1a634bb615f3 38 {
llarose 17:1a634bb615f3 39 #if WITH_MOY
llarose 17:1a634bb615f3 40 unsigned short dataIndex[MOY_MOB_N];
llarose 17:1a634bb615f3 41 unsigned short dataMajeur[MOY_MOB_N];
llarose 17:1a634bb615f3 42 unsigned short dataAnnulaire[MOY_MOB_N];
llarose 17:1a634bb615f3 43 unsigned short moyIndex(0);
llarose 17:1a634bb615f3 44 unsigned short moyMajeur(0);
llarose 17:1a634bb615f3 45 unsigned short moyAnnulaire(0);
llarose 17:1a634bb615f3 46 #endif
llarose 17:1a634bb615f3 47
llarose 17:1a634bb615f3 48 while(true)
llarose 17:1a634bb615f3 49 {
llarose 17:1a634bb615f3 50 // Moyenne mobile
llarose 17:1a634bb615f3 51 #if WITH_MOY
llarose 17:1a634bb615f3 52 for(int i(0); i<MOY_MOB_N-1; ++i)
llarose 17:1a634bb615f3 53 {
llarose 17:1a634bb615f3 54 dataIndex[i] = dataIndex[1+i];
llarose 17:1a634bb615f3 55 dataMajeur[i] = dataMajeur[1+i];
llarose 17:1a634bb615f3 56 dataAnnulaire[i] = dataAnnulaire[1+i];
llarose 17:1a634bb615f3 57 }
llarose 17:1a634bb615f3 58 dataIndex[MOY_MOB_N-1] = AinIndex->read_u16();
llarose 17:1a634bb615f3 59 dataMajeur[MOY_MOB_N-1] = AinMajeur->read_u16();
llarose 17:1a634bb615f3 60 dataAnnulaire[MOY_MOB_N-1] = AinAnnulaire->read_u16();
llarose 17:1a634bb615f3 61
llarose 17:1a634bb615f3 62 moyIndex = abs(moyIndex - dataIndex[0] + dataIndex[MOY_MOB_N-1]);
llarose 17:1a634bb615f3 63 moyMajeur = abs(moyMajeur - dataMajeur[0] + dataMajeur[MOY_MOB_N-1]);
llarose 17:1a634bb615f3 64 moyAnnulaire = abs(moyAnnulaire - dataAnnulaire[0] + dataAnnulaire[MOY_MOB_N-1]);
llarose 17:1a634bb615f3 65
Thierry19 23:5bb76b7c35da 66 //flex_data.index c'est un bool?
llarose 17:1a634bb615f3 67 mutex.lock();
llarose 17:1a634bb615f3 68 flex_data.index = moyIndex;
llarose 17:1a634bb615f3 69 flex_data.majeur = moyMajeur;
llarose 17:1a634bb615f3 70 flex_data.annulaire = moyAnnulaire;
llarose 17:1a634bb615f3 71 mutex.unlock();
llarose 17:1a634bb615f3 72
llarose 17:1a634bb615f3 73 #else
llarose 17:1a634bb615f3 74
llarose 17:1a634bb615f3 75 mutex.lock();
llarose 43:92c9e58b8a3a 76
llarose 43:92c9e58b8a3a 77 set_flex_range();
llarose 43:92c9e58b8a3a 78
Thierry19 23:5bb76b7c35da 79 #if VALEURFLEX
llarose 17:1a634bb615f3 80 flex_data.indexU32 = AinIndex->read_u16();
llarose 43:92c9e58b8a3a 81 flex_data.indexU32 > flex_range.limit_index ? flex_data.index = 1 : flex_data.index = 0;
llarose 17:1a634bb615f3 82 flex_data.majeurU32 = AinMajeur->read_u16();
llarose 43:92c9e58b8a3a 83 flex_data.majeurU32 > flex_range.limit_majeur ? flex_data.majeur = 1 : flex_data.majeur = 0;
llarose 17:1a634bb615f3 84 flex_data.annulaireU32 = AinAnnulaire->read_u16();
llarose 43:92c9e58b8a3a 85 flex_data.annulaireU32 > flex_range.limit_annulaire ? flex_data.annulaire = 1 : flex_data.annulaire = 0;
Thierry19 23:5bb76b7c35da 86 #else
llarose 43:92c9e58b8a3a 87 AinIndex->read_u16() > flex_range.limit_index ? flex_data.index = 1 : flex_data.index = 0;
llarose 43:92c9e58b8a3a 88 AinMajeur->read_u16() > flex_range.limit_majeur ? flex_data.majeur = 1 : flex_data.majeur = 0;
llarose 43:92c9e58b8a3a 89 AinAnnulaire->read_u16() > flex_range.limit_annulaire ? flex_data.annulaire = 1 : flex_data.annulaire = 0;
llarose 43:92c9e58b8a3a 90 #endif
llarose 43:92c9e58b8a3a 91 #if print_finger_state
llarose 43:92c9e58b8a3a 92 printf("majeur: %u, limit : %u, min : %u, max: %u\r\n", flex_data.majeur, flex_range.limit_majeur, flex_range.min_majeur, flex_range.max_majeur);
llarose 43:92c9e58b8a3a 93 //printf("index : %i, majeur: %i, annulaire : %i\r\n", flex_data.index, flex_data.majeur, flex_data.annulaire);
Thierry19 23:5bb76b7c35da 94 #endif
Thierry19 23:5bb76b7c35da 95
llarose 43:92c9e58b8a3a 96 led1 = flex_data.index;
llarose 43:92c9e58b8a3a 97 led2 = flex_data.majeur;
llarose 43:92c9e58b8a3a 98 led3 = flex_data.annulaire;
llarose 17:1a634bb615f3 99 mutex.unlock();
llarose 17:1a634bb615f3 100
llarose 17:1a634bb615f3 101 #endif
llarose 17:1a634bb615f3 102
llarose 17:1a634bb615f3 103 }
llarose 1:601d2922ff06 104 }
llarose 43:92c9e58b8a3a 105
llarose 43:92c9e58b8a3a 106 void FlexSensor::set_flex_range()
llarose 43:92c9e58b8a3a 107 {
llarose 43:92c9e58b8a3a 108 unsigned short indexu16 = AinIndex->read_u16();
llarose 43:92c9e58b8a3a 109 unsigned short majeuru16 = AinMajeur->read_u16();
llarose 43:92c9e58b8a3a 110 unsigned short annulaireu16 = AinAnnulaire->read_u16();
llarose 43:92c9e58b8a3a 111
llarose 43:92c9e58b8a3a 112 #if safety_calibration
llarose 43:92c9e58b8a3a 113 if(indexu16 < SEUIL_CALIBRATION || majeuru16 < SEUIL_CALIBRATION || annulaireu16 < SEUIL_CALIBRATION)
llarose 43:92c9e58b8a3a 114 reset_range();
llarose 43:92c9e58b8a3a 115 #endif
llarose 43:92c9e58b8a3a 116
llarose 43:92c9e58b8a3a 117 if(indexu16 < flex_range.min_index)
llarose 43:92c9e58b8a3a 118 {
llarose 43:92c9e58b8a3a 119 flex_range.min_index = indexu16;
llarose 43:92c9e58b8a3a 120 flex_range.limit_index = flex_range.min_index + ((flex_range.max_index - flex_range.min_index) >> 2);
llarose 43:92c9e58b8a3a 121 #if test_flex
llarose 43:92c9e58b8a3a 122 printf("new index_min : %x\r\n", flex_range.min_index);
llarose 43:92c9e58b8a3a 123 #endif
llarose 43:92c9e58b8a3a 124 }
llarose 43:92c9e58b8a3a 125 if(indexu16 > flex_range.max_index)
llarose 43:92c9e58b8a3a 126 {
llarose 43:92c9e58b8a3a 127 flex_range.max_index = indexu16;
llarose 43:92c9e58b8a3a 128 flex_range.limit_index = flex_range.min_index + ((flex_range.max_index - flex_range.min_index) >> 2);
llarose 43:92c9e58b8a3a 129 #if test_flex
llarose 43:92c9e58b8a3a 130 printf("new index max : %x\r\n", flex_range.max_index);
llarose 43:92c9e58b8a3a 131 #endif
llarose 43:92c9e58b8a3a 132 }
llarose 43:92c9e58b8a3a 133
llarose 43:92c9e58b8a3a 134 if(majeuru16 < flex_range.min_majeur)
llarose 43:92c9e58b8a3a 135 {
llarose 43:92c9e58b8a3a 136 flex_range.min_majeur = majeuru16;
llarose 43:92c9e58b8a3a 137 flex_range.limit_majeur = flex_range.min_majeur + ((flex_range.max_majeur - flex_range.min_majeur) >> 4);
llarose 43:92c9e58b8a3a 138 #if test_flex
llarose 43:92c9e58b8a3a 139 printf("new majeur min : %x\r\n", flex_range.min_majeur);
llarose 43:92c9e58b8a3a 140 #endif
llarose 43:92c9e58b8a3a 141 }
llarose 43:92c9e58b8a3a 142 if(majeuru16 > flex_range.max_majeur)
llarose 43:92c9e58b8a3a 143 {
llarose 43:92c9e58b8a3a 144 flex_range.max_majeur = majeuru16;
llarose 43:92c9e58b8a3a 145 flex_range.limit_majeur = flex_range.min_majeur + ((flex_range.max_majeur - flex_range.min_majeur) >> 4);
llarose 43:92c9e58b8a3a 146 #if test_flex
llarose 43:92c9e58b8a3a 147 printf("new majeur max : %x\r\n", flex_range.max_majeur);
llarose 43:92c9e58b8a3a 148 #endif
llarose 43:92c9e58b8a3a 149 }
llarose 43:92c9e58b8a3a 150
llarose 43:92c9e58b8a3a 151 if(annulaireu16 < flex_range.min_annulaire)
llarose 43:92c9e58b8a3a 152 {
llarose 43:92c9e58b8a3a 153 flex_range.min_annulaire = annulaireu16;
llarose 43:92c9e58b8a3a 154 flex_range.limit_annulaire = flex_range.min_annulaire + ((flex_range.max_annulaire - flex_range.min_annulaire) >> 2);
llarose 43:92c9e58b8a3a 155 #if test_flex
llarose 43:92c9e58b8a3a 156 printf("new annulaire min : %x\r\n", flex_range.min_annulaire);
llarose 43:92c9e58b8a3a 157 #endif
llarose 43:92c9e58b8a3a 158 }
llarose 43:92c9e58b8a3a 159 if(annulaireu16 > flex_range.max_annulaire)
llarose 43:92c9e58b8a3a 160 {
llarose 43:92c9e58b8a3a 161 flex_range.max_annulaire = annulaireu16;
llarose 43:92c9e58b8a3a 162 flex_range.limit_annulaire = flex_range.min_annulaire + ((flex_range.max_annulaire - flex_range.min_annulaire) >> 2);
llarose 43:92c9e58b8a3a 163 #if test_flex
llarose 43:92c9e58b8a3a 164 printf("new annulaire max : %x\r\n", flex_range.max_annulaire);
llarose 43:92c9e58b8a3a 165 #endif
llarose 43:92c9e58b8a3a 166 }
llarose 43:92c9e58b8a3a 167 }
llarose 43:92c9e58b8a3a 168
llarose 43:92c9e58b8a3a 169 void FlexSensor::reset_range()
llarose 43:92c9e58b8a3a 170 {
llarose 43:92c9e58b8a3a 171 flex_range_t new_range;
llarose 43:92c9e58b8a3a 172 flex_range = new_range;
llarose 43:92c9e58b8a3a 173 }
groygirard 12:ebb08773dbdb 174
groygirard 12:ebb08773dbdb 175 flex_t FlexSensor::get_flex_values()
groygirard 12:ebb08773dbdb 176 {
llarose 17:1a634bb615f3 177 mutex.lock();
llarose 17:1a634bb615f3 178 flex_t result = flex_data;
llarose 17:1a634bb615f3 179 mutex.unlock();
llarose 17:1a634bb615f3 180
llarose 17:1a634bb615f3 181 return result;
llarose 17:1a634bb615f3 182 }