Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions firmware/lucidgloves-firmware/AdvancedConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define CALIBRATION_LOOPS -1//How many loops should be calibrated. Set to -1 to always be calibrated.

//Comm defines, no touchy
#define COMM_SERIAL 0
#define COMM_BTSERIAL 1
#define COMM_SERIAL 0
#define COMM_BTSERIAL 1

//Encoding
#define ENCODING 1
Expand All @@ -23,13 +23,13 @@
#if defined(__AVR__)
#define ANALOG_MAX 1023
#elif defined(ESP32)
#define ANALOG_MAX 4095
#define ANALOG_MAX 4095
#endif


//ANALOG_MAX OVERRIDE:
//uncomment and set as needed (only touch if you know what you are doing)
//#define ANALOG_MAX 4095
//#define ANALOG_MAX 4095

#ifndef ANALOG_MAX
#error "This board doesn't have an auto ANALOG_MAX assignment, please set it manually by uncommenting ANALOG_MAX OVERRIDE!"
Expand Down
56 changes: 28 additions & 28 deletions firmware/lucidgloves-firmware/Encoding.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@
bool bButton;
bool grab;
bool pinch;
};
};

struct outputData{
struct outputData{
int* hapticLimits;
};
};
*/

#if ENCODING == ENCODING_LEGACY
//legacy encoding
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) {
static char stringToEncode[75];
sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, joyClick,
triggerButton, aButton, bButton, grab, pinch
);

sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, joyClick,
triggerButton, aButton, bButton, grab, pinch
);
return stringToEncode;
}
//legacy decoding
void decodeData(char* stringToDecode, int* hapticLimits){
byte index = 0;
char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters
while(ptr != NULL)
{
hapticLimits[index] = atoi(ptr);
index++;
ptr = strtok(NULL, "&"); // takes a list of delimiters
}
void decodeData(char* stringToDecode, int* hapticLimits) {
byte index = 0;
char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters
while (ptr != NULL)
{
hapticLimits[index] = atoi(ptr);
index++;
ptr = strtok(NULL, "&"); // takes a list of delimiters
}
}
#endif

#if ENCODING == ENCODE_ALPHA
//alphabetic encoding
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){
char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) {
static char stringToEncode[75];
int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0;
sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, trigger, joyClick?"H":"",
triggerButton?"I":"", aButton?"J":"", bButton?"K":"", grab?"L":"", pinch?"M":"", menu?"N":"", calib?"O":""
);
int trigger = (flexion[1] > ANALOG_MAX / 2) ? (flexion[1] - ANALOG_MAX / 2) * 2 : 0;
sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n",
flexion[0], flexion[1], flexion[2], flexion[3], flexion[4],
joyX, joyY, trigger, joyClick ? "H" : "",
triggerButton ? "I" : "", aButton ? "J" : "", bButton ? "K" : "", grab ? "L" : "", pinch ? "M" : "", menu ? "N" : "", calib ? "O" : ""
);
return stringToEncode;
}

//legacy decoding
void decodeData(char* stringToDecode, int* hapticLimits){
void decodeData(char* stringToDecode, int* hapticLimits) {
hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb
hapticLimits[1] = getArgument(stringToDecode, 'B'); //index
hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle
Expand All @@ -63,7 +63,7 @@ void decodeData(char* stringToDecode, int* hapticLimits){
//Serial.println("Haptic: "+ (String)hapticLimits[0] + " " + (String)hapticLimits[1] + " " + (String)hapticLimits[2] + " " + (String)hapticLimits[3] + " " + (String)hapticLimits[4] + " ");
}

int getArgument(char* stringToDecode, char command){
int getArgument(char* stringToDecode, char command) {
char* start = strchr(stringToDecode, command);
if (start == NULL)
return -1;
Expand Down
12 changes: 6 additions & 6 deletions firmware/lucidgloves-firmware/SerialBTCommunication.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ class BTSerialCommunication : public ICommunication {
private:
bool m_isOpen;
BluetoothSerial m_SerialBT;

public:
BTSerialCommunication() {
m_isOpen = false;
}

bool isOpen(){
bool isOpen() {
return m_isOpen;
}

void start(){
void start() {
Serial.begin(115200);
m_SerialBT.begin(BTSERIAL_DEVICE_NAME);
Serial.println("The device started, now you can pair it with bluetooth!");
m_isOpen = true;
}

void output(char* data){
void output(char* data) {
m_SerialBT.print(data);
}

bool readData(char* input){
bool readData(char* input) {
/*byte size = m_SerialBT.readBytesUntil('\n', input, 100);
input[size] = NULL;*/
input[size] = NULL;*/
String message = m_SerialBT.readStringUntil('\n');
strcpy(input, message.c_str());
return input != NULL && strlen(input) > 0;
Expand Down
8 changes: 4 additions & 4 deletions firmware/lucidgloves-firmware/SerialCommunication.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class SerialCommunication : public ICommunication {
m_isOpen = false;
}

bool isOpen(){
bool isOpen() {
return m_isOpen;
}

void start(){
void start() {
//Serial.setTimeout(1000000);
Serial.begin(SERIAL_BAUD_RATE);
m_isOpen = true;
}

void output(char* data){
void output(char* data) {
Serial.print(data);
Serial.flush();
}

bool readData(char* input){
bool readData(char* input) {
byte size = Serial.readBytesUntil('\n', input, 100);
input[size] = NULL;
return input != NULL && strlen(input) > 0;
Expand Down
74 changes: 37 additions & 37 deletions firmware/lucidgloves-firmware/_main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,82 @@

#define CALIB_OVERRIDE false
#if USING_CALIB_PIN && COMMUNICATION == COMM_SERIAL && PIN_CALIB == 0 && !CALIB_OVERRIDE
#error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this."
#error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this."
#endif

ICommunication* comm;
int loops = 0;
void setup() {
#if COMMUNICATION == COMM_SERIAL
comm = new SerialCommunication();
#elif COMMUNICATION == COMM_BTSERIAL
comm = new BTSerialCommunication();
#endif
#if COMMUNICATION == COMM_SERIAL
comm = new SerialCommunication();
#elif COMMUNICATION == COMM_BTSERIAL
comm = new BTSerialCommunication();
#endif
comm->start();

setupInputs();

#if USING_FORCE_FEEDBACK
setupServoHaptics();
#endif
#if USING_FORCE_FEEDBACK
setupServoHaptics();
#endif

}

void loop() {
if (comm->isOpen()){
#if USING_CALIB_PIN
if (comm->isOpen()) {
#if USING_CALIB_PIN
bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB;
if (calibButton)
loops = 0;
#else
#else
bool calibButton = false;
#endif
#endif

bool calibrate = false;
if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING){
if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING) {
calibrate = true;
loops++;
}

int* fingerPos = getFingerPositions(calibrate, calibButton);
bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY;

#if TRIGGER_GESTURE
#if TRIGGER_GESTURE
bool triggerButton = triggerGesture(fingerPos);
#else
#else
bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER;
#endif
#endif

bool aButton = getButton(PIN_A_BTN) != INVERT_A;
bool bButton = getButton(PIN_B_BTN) != INVERT_B;

#if GRAB_GESTURE
#if GRAB_GESTURE
bool grabButton = grabGesture(fingerPos);
#else
#else
bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB;
#endif
#endif

#if PINCH_GESTURE
#if PINCH_GESTURE
bool pinchButton = pinchGesture(fingerPos);
#else
#else
bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH;
#endif
#endif

bool menuButton = getButton(PIN_MENU_BTN) != INVERT_MENU;

comm->output(encode(fingerPos, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton));

#if USING_FORCE_FEEDBACK
char received[100];
if (comm->readData(received)){
int hapticLimits[5];
//This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later
if(String(received).length() >= 10) {
decodeData(received, hapticLimits);
writeServoHaptics(hapticLimits);
}
#if USING_FORCE_FEEDBACK
char received[100];
if (comm->readData(received)) {
int hapticLimits[5];
//This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later
if (String(received).length() >= 10) {
decodeData(received, hapticLimits);
writeServoHaptics(hapticLimits);
}
#endif
}
#endif
delay(LOOP_TIME);
}
}
12 changes: 6 additions & 6 deletions firmware/lucidgloves-firmware/gesture.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bool grabGesture(int *flexion){
return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX/2 ? 0:1;
bool grabGesture(int *flexion) {
return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX / 2 ? 0 : 1;
}

bool pinchGesture(int *flexion){
return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX/2 ? 0:1;
bool pinchGesture(int *flexion) {
return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX / 2 ? 0 : 1;
}

bool triggerGesture(int *flexion){
return flexion[INDEX_IND]<=(ANALOG_MAX/2)?0:1;
bool triggerGesture(int *flexion) {
return flexion[INDEX_IND] <= (ANALOG_MAX / 2) ? 0 : 1;
}
36 changes: 18 additions & 18 deletions firmware/lucidgloves-firmware/haptics.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#if USING_FORCE_FEEDBACK

#if defined(ESP32)
#include "ESP32Servo.h"
#include "ESP32Servo.h"
#else
#include "Servo.h"
#include "Servo.h"
#endif

Servo pinkyServo;
Expand All @@ -12,7 +12,7 @@ Servo middleServo;
Servo indexServo;
Servo thumbServo;

void setupServoHaptics(){
void setupServoHaptics() {
pinkyServo.attach(PIN_PINKY_MOTOR);
ringServo.attach(PIN_RING_MOTOR);
middleServo.attach(PIN_MIDDLE_MOTOR);
Expand All @@ -21,34 +21,34 @@ void setupServoHaptics(){
}

//static scaling, maps to entire range of servo
void scaleLimits(int* hapticLimits, float* scaledLimits){
for (int i = 0; i < 5; i++){
void scaleLimits(int* hapticLimits, float* scaledLimits) {
for (int i = 0; i < 5; i++) {
scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f;
}
}

//dynamic scaling, maps to the limits calibrated from your finger
void dynScaleLimits(int* hapticLimits, float* scaledLimits){
void dynScaleLimits(int* hapticLimits, float* scaledLimits) {
//will be refactored to take min and max as an argument

/* this implementation of dynamic scaling relies on the assumption
* that the servo reaches 2/3 of the potentiometer's range,
* and that 0 degrees is geared to the start of the potentiometer.
* Different hardware types may need to handle dynamic scaling differently.
*/
for (int i = 0; i < sizeof(hapticLimits); i++){
/* this implementation of dynamic scaling relies on the assumption
that the servo reaches 2/3 of the potentiometer's range,
and that 0 degrees is geared to the start of the potentiometer.
Different hardware types may need to handle dynamic scaling differently.
*/
for (int i = 0; i < sizeof(hapticLimits); i++) {
scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f;
}
}

void writeServoHaptics(int* hapticLimits){
void writeServoHaptics(int* hapticLimits) {
float scaledLimits[5];
scaleLimits(hapticLimits, scaledLimits);
if(hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]);
if(hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]);
if(hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]);
if(hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]);
if(hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]);
if (hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]);
if (hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]);
if (hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]);
if (hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]);
if (hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]);
}

#endif
Loading