Skip to content

Commit

Permalink
Improving resolution of servo control.
Browse files Browse the repository at this point in the history
Changing getEnvelop to return a float and using a locally defined mapf to
avoid the coarse conversion which happens with int values of 2 to 10. upsidedownlabs#38
  • Loading branch information
Kevin J Walters committed Jan 12, 2024
1 parent 18bd185 commit 4845c59
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions software/ClawController/ClawController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// SOFTWARE.

#include <inttypes.h>
#include <math.h>

#if defined(ESP32)
#include <ESP32Servo.h>
Expand All @@ -49,6 +50,10 @@ int data_index;
int flag=0;
Servo servo;

inline float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void setup() {
// Serial connection begin
Serial.begin(BAUD_RATE);
Expand Down Expand Up @@ -84,8 +89,10 @@ void loop() {
timer += 1000000 / SAMPLE_RATE;
int sensor_value = analogRead(INPUT_PIN);
int signal = (int)EMGFilter((float)sensor_value);
int envelop = getEnvelop(abs(signal));
int servo_position = constrain(map(envelop, EMG_MIN, EMG_MAX, SERVO_MIN, SERVO_MAX),
float envelop = getEnvelop(abs(signal));
int servo_position = constrain((int)roundf(mapf(envelop,
EMG_MIN, EMG_MAX,
SERVO_MIN, SERVO_MAX)),
SERVO_MIN, SERVO_MAX);
servo.write(servo_position);
Serial.print(signal);
Expand All @@ -95,12 +102,12 @@ void loop() {
}

// Envelop detection algorithm
int getEnvelop(int abs_emg){
float getEnvelop(int abs_emg){
sum -= circular_buffer[data_index];
sum += abs_emg;
circular_buffer[data_index] = abs_emg;
data_index = (data_index + 1) % BUFFER_SIZE;
return (sum/BUFFER_SIZE) * 2;
return (float(sum) / BUFFER_SIZE) * 2.0f;
}

// Band-Pass Butterworth IIR digital filter, generated using filter_gen.py.
Expand Down

0 comments on commit 4845c59

Please sign in to comment.