That is the code i use in my project, consist in hand what is controlled by a glove with sensors flex. I will explain all the factors and the tips i use in my code.
Dependencies: flex_sensor mbed Servo
Here we have the code of the both sides of the project, the part will control the arm and the part will move our arm.
Revision 2:0f0535b697ce, committed 2015-07-01
- Comitter:
- Ronjea
- Date:
- Wed Jul 01 08:54:01 2015 +0000
- Parent:
- 1:8347f6c06e1c
- Commit message:
- change;
Changed in this revision
--- a/gloveSensorSend.lib Wed Jul 01 08:52:31 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/Ronjea/code/gloveSensorSend/#17ebf84f093c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gloveSensorSend/flex_sensor.lib Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/s_meika/code/flex_sensor/#689bf33a6c3f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gloveSensorSend/gloveSensorSend.cpp Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,142 @@ +#include "mbed.h" +#include "flex_sensor.h" + +Serial xbee1(PA_11, PA_12); //Initalise xbee_lib +Serial pc(USBTX, USBRX); //Initalise PC serial comms + +// Declaramos los pines analogicos de los cuales vamos a leer los valores que necesitamos +flex_sensor chico_flex(A0); +flex_sensor gordo_flex(A1); +flex_sensor indice_flex(A2); +flex_sensor corte_flex(A3); +flex_sensor anillo_flex(A4); + +//Variable donde guardaremos de la posicion de nuestros dedos y con las que vamos a operar +int chicoPosicion, gordoPosicion, anilloPosicion, indicePosicion, cortePosicion, + chicoCerrado, gordoCerrado, anilloCerrado, indiceCerrado, corteCerrado, + chicoAbierto, gordoAbierto, anilloAbierto, indiceAbierto, corteAbierto; +/* Estas variables son las que vamos a mandar a traves del XBee, como solo se pueden mandar bytes + y nuestro numero a enviar es de menos de 1 byte siempre le hacemos la conversion al final*/ +int8_t chicoPosicionMandar, gordoPosicionMandar, anilloPosicionMandar, + indicePosicionMandar, cortePosicionMandar; +unsigned int hextoint(const char *hex); +uint8_t reescala(int value, int inMin, int inMax, int outMin, int outMax) { + return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; +} + +int main() { + xbee1.baud(38400); + chicoAbierto = 3600; + gordoAbierto = 3200; + anilloAbierto = 3220; + indiceAbierto = 3400; + corteAbierto = 3400; + + chicoCerrado = chico_flex.get_raw_value(); + gordoCerrado = gordo_flex.get_raw_value(); + anilloCerrado = anillo_flex.get_raw_value(); + indiceCerrado = indice_flex.get_raw_value(); + corteCerrado = corte_flex.get_raw_value(); + + while (1) { + + //Traducimos el valor analogico de entrada del sensor a grados + //con el reescalado y asi obtener la posicion + //Estos son los valores que van a ser mandados + chicoPosicion = chico_flex.get_raw_value(); + gordoPosicion = gordo_flex.get_raw_value(); + anilloPosicion = anillo_flex.get_raw_value(); + indicePosicion = indice_flex.get_raw_value(); + cortePosicion = corte_flex.get_raw_value(); + pc.printf("Valor minimo o maximo indice %d\r\n", indicePosicion); + pc.printf("Valor minimo o maximo gordo %d\r\n", gordoPosicion); + pc.printf("Valor minimo o maximo anillo %d\r\n", anilloPosicion); + pc.printf("Valor minimo o maximo chico %d\r\n", chicoPosicion); + + //Abierto + if (gordoPosicion > 3200) { + gordoAbierto = 3200; + gordoPosicion = gordoAbierto; + } else if (gordoPosicion < gordoAbierto) { + gordoAbierto = gordoPosicion; + } + if (indicePosicion > 3300) { + indiceAbierto = 3300; + indicePosicion = indiceAbierto; + } else if (indicePosicion > indiceAbierto) { + indiceAbierto = indicePosicion; + } + if (cortePosicion > 3400) { + + corteAbierto = 3400; + cortePosicion = corteAbierto; + } else if (cortePosicion > corteAbierto) { + corteAbierto = cortePosicion; + } + if (anilloPosicion > 3220) { + anilloAbierto = 3220; + anilloPosicion = anilloAbierto; + } else if (anilloPosicion > anilloAbierto) { + anilloAbierto = anilloPosicion; + } + if (chicoPosicion > 3600) { + chicoAbierto = 3600; + chicoPosicion = chicoAbierto; + } else if (chicoPosicion > chicoAbierto) { + chicoAbierto = chicoPosicion; + } + + //Cerrado + if (gordoPosicion < 2450) { + gordoCerrado = 2450; + gordoPosicion = gordoCerrado; + } else if (gordoPosicion < gordoCerrado) { + gordoCerrado = gordoPosicion; + } + if (indicePosicion < 2500) { + indiceCerrado = 2500; + indicePosicion = indiceCerrado; + } else if (indicePosicion < indiceCerrado) { + indiceCerrado = indicePosicion; + + } + if (cortePosicion < 2500) { + corteCerrado = 2500; + cortePosicion = corteCerrado; + } else if (cortePosicion < corteCerrado) { + corteCerrado = cortePosicion; + } + if (anilloPosicion < 2360) { + anilloCerrado = 2360; + anilloPosicion = anilloCerrado; + } else if (anilloPosicion < anilloCerrado) { + anilloCerrado = anilloPosicion; + } + if (chicoPosicion < 2450) { + chicoCerrado = 2450; + chicoPosicion = chicoCerrado; + } else if (chicoPosicion < chicoCerrado) { + chicoCerrado = chicoPosicion; + } + //Cerrado significa que esta sin fuerza y abierto forzado + chicoPosicionMandar = reescala(chicoPosicion, chicoCerrado, + chicoAbierto, -10, 90); + gordoPosicionMandar = reescala(gordoPosicion, gordoCerrado, + gordoAbierto, -10, 90); + anilloPosicionMandar = reescala(anilloPosicion, anilloCerrado, + anilloAbierto, -25, 90); + indicePosicionMandar = reescala(indicePosicion, indiceCerrado, + indiceAbierto, -35, 90); + cortePosicionMandar = reescala(cortePosicion, corteCerrado, + corteAbierto, -20, 90); + pc.printf("%d", cortePosicionMandar); + + xbee1.printf("%c%c%c%c%c%c%c", 0x7E, chicoPosicionMandar, + gordoPosicionMandar, anilloPosicionMandar, indicePosicionMandar, + cortePosicionMandar, 0x0D); + pc.printf("%c,%d,%c", 0x7E, chicoPosicionMandar, 0x0D); + + wait_ms(30); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gloveSensorSend/mbed.bld Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file
--- a/handServoReciever.lib Wed Jul 01 08:52:31 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/Ronjea/code/handServoReciever/#3c6e54cb905d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handServoReciever/Servo.lib Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handServoReciever/handServoReciever.cpp Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" +#include "Servo.h" +Serial xbee1(PA_11, PA_12); //Inicializamos el xbee + +Serial pc(USBTX, USBRX); +Servo chico(D9); +Servo gordo(D3); +Servo indice(D5); +Servo corte(D6); +Servo anillo(D10); + +int8_t chicoPosicion, gordoPosicion, anilloPosicion, indicePosicion, + cortePosicion; +char finalDeTrama; +void calibrarServos(); +void calibrarServos() { + chico.calibrate(0.00095, 90); + chico.position(-10); + gordo.calibrate(0.00095, 90); + gordo.position(-10); + indice.calibrate(0.00095, 90); + indice.position(-20); + corte.calibrate(0.00095, 90); + corte.position(-10); + anillo.calibrate(0.00095, 90); + anillo.position(-10); +} + +int main() { + calibrarServos(); // Con esta funcion calibramos los servos y los ponemos en su posicion + xbee1.baud(38400); + while (1) { + + //Traducimos el valor analogico de entrada del sensor a grados + //con el reescalado y asi obtener la posicion + if (xbee1.readable()) { + if (xbee1.getc() == 0x7E) { + //Recogemos las variables en el orden que + //necesitamos para que se muevan los dedos + chicoPosicion = xbee1.getc(); + gordoPosicion = xbee1.getc(); + anilloPosicion = xbee1.getc(); + indicePosicion = xbee1.getc(); + cortePosicion = xbee1.getc(); + finalDeTrama = xbee1.getc(); + pc.printf("Esto es : %d \r\n", cortePosicion); + if (finalDeTrama == 0x0D) { + pc.printf("Trama correcta \r\n"); + + chico.position(chicoPosicion); + gordo.position(gordoPosicion); + anillo.position(anilloPosicion); + indice.position(indicePosicion); + corte.position(cortePosicion); + + } else { + pc.printf("Trama INCORRECTA \r\n"); + } + } + } + + } + + //Movemos a la posicion los dedos dandole la informacion necesaria a los servos. + wait_ms(30); + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handServoReciever/mbed.bld Wed Jul 01 08:54:01 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file