Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
EdOliver
Published © MIT

Brainium Rehab: Rehabilitation Trainer with Robotic Arm

Rehabilitation system based on SmartEdge Agile which detects rehabilitation movements and gives feedback to the patient.

AdvancedFull instructions provided15 hours2,029

Things used in this project

Hardware components

SmartEdge Agile Brainium
Avnet SmartEdge Agile Brainium
×1
Robotic arm
×1
Adafruit feather huzzah 32
×1
Standard core wire
×1
8 Channel DC 5V Relay Module with Optocoupler x1.
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python anaconda
CloudMqtt

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

System Architecture

Showcases how everything works together.

Connection diagram for the Robotic Arm

Follow the diagram correctly, any mishap could fry circuits.

Code

certificate

Python
this goes with the MQTT python file. You have to replace the infile.
-----BEGIN CERTIFICATE-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END CERTIFICATE-----

Brainium rehab for ESP32

C/C++
Just install it in your ESP32, just for ESP32
#define Vcc 13
#define Vee 12
#define Claw 27
#define Wrist 33 
#define Elbow 15
#define Shoulder 32
#define Base 14
#define Ddelay 11

#include <WiFi.h>
#include <PubSubClient.h>
 
const char* ssid = "YOURSSID";
const char* password =  "YOURPASS";
const char* mqttServer = "m12.cloudmqtt.com";
const int mqttPort = 12345;
const char* mqttUser = "YOURCLOUDMQTTUSER";
const char* mqttPassword = "YOURCLOUDMQTTPASS";
bool sw = false;
int counter = 0;
int mov=0;
 
WiFiClient espClient;
PubSubClient client(espClient);
 
void setup() {
  pinMode(Vcc,OUTPUT);  
  pinMode(Vee,OUTPUT); 
  pinMode(Claw,OUTPUT);
  pinMode(Wrist,OUTPUT);
  pinMode(Elbow,OUTPUT);
  pinMode(Shoulder,OUTPUT);
  pinMode(Base,OUTPUT);
  // Turning Off Everything
  digitalWrite(Vcc,LOW); 
  digitalWrite(Vee,HIGH); 
  digitalWrite(Claw,HIGH);
  digitalWrite(Wrist,HIGH);
  digitalWrite(Elbow,HIGH);
  digitalWrite(Shoulder,HIGH);
  digitalWrite(Base,HIGH);
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ANYNAME", mqttUser, mqttPassword )) {
 
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
 
  client.publish("/ESPTopic", "Hello from ESP32"); 
  client.subscribe("/ArmTopic"); 
  delay(5000);
  digitalWrite(Vcc,HIGH);
  delay(5000);
  Serial.print("Move Arm Start");
  FmoveP();
  Fmove();
  Fmove();
  Fmove();
  Serial.print("Move Arm Stop");

}
 
void callback(char* topic, byte* payload, unsigned int length) {
 
  String datas="";
  for (int i = 0; i < length; i++) 
  {
    datas+=char(payload[i]);
  }
  Serial.println(datas);
  if(datas=="Sit Flex" && mov==0 && counter<10)
  {
    counter++;
  }
  else if(datas=="Sit Flex" && mov==0 && counter==10)
  {
    mov++;
    counter=0;
    FmoveR();
    SmoveP();
    Smove();
    Smove();
    Smove();
  }
  else if(datas=="Sit Up" && mov==1 && counter<10)
  {
    counter++;
  }
  else if(datas=="Sit Up" && mov==1 && counter==10)
  {
    mov++;
    counter=0;
    SmoveR();
    TmoveP();
    Tmove();
    Tmove();
    Tmove();
  }
   else if(datas=="Sit Inside" && mov==2 && counter<10)
  {
    counter++;
  }
  else if(datas=="Sit Inside" && mov==2 && counter==10)
  {
    mov++;
    counter=0;
    TmoveR();
  }
  
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

void up(int pin,int Delay)
{
  digitalWrite(Claw,HIGH);
  digitalWrite(Wrist,HIGH);
  digitalWrite(Elbow,HIGH);
  digitalWrite(Shoulder,HIGH);
  digitalWrite(Base,HIGH);
  digitalWrite(pin,LOW);
  digitalWrite(Vcc,LOW);  // +Vcc ON
  delay(Delay);
  digitalWrite(Vcc,HIGH);
  digitalWrite(pin,HIGH);
}

void down(int pin, int Delay)
{
  digitalWrite(Claw,LOW);
  digitalWrite(Wrist,LOW);
  digitalWrite(Elbow,LOW);
  digitalWrite(Shoulder,LOW);
  digitalWrite(Base,LOW);
  digitalWrite(pin,HIGH);
  digitalWrite(Vee,LOW); // -Vee ON
  delay(Delay);
  digitalWrite(Vee,HIGH);
  digitalWrite(Claw,HIGH);
  digitalWrite(Wrist,HIGH);
  digitalWrite(Elbow,HIGH);
  digitalWrite(Shoulder,HIGH);
  digitalWrite(Base,HIGH);
  
}


void FmoveP(void)
{
  down(Shoulder,5000);
  down(Elbow,5500);
}
void Fmove(void)
{
  up(Wrist,3500);
  down(Wrist,3500);
}
void FmoveR(void)
{
  up(Shoulder,5500);
  up(Elbow,6000);
}

void SmoveP(void)
{
  up(Base,6000);
  down(Shoulder,5000);
  down(Elbow,5300);
}
void Smove(void)
{
  up(Elbow,6500);
  down(Elbow,5300);
}
void SmoveR(void)
{
  up(Elbow,6000);
  up(Shoulder,5500);
  down(Base,6000);
  
}

void TmoveP(void)
{
  up(Base,6300);
  down(Shoulder,5000);
  up(Elbow,3000);
  up(Wrist,3500); 
}
void Tmove(void)
{
  up(Elbow,4000);
  down(Elbow,4000);
  up(Elbow,4000);
  down(Elbow,4000);
}
void TmoveR(void)
{
  down(Elbow,3500);
  down(Wrist,3500);
  up(Shoulder,5700);
  down(Base,6300);
}


void reconnect() {
 while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP32Client2", mqttUser, mqttPassword )) {
 
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
  client.publish("/ESPTopic", "Hello from ESP32"); 
  client.subscribe("/ArmTopic"); 
}

MQTTRehab.py

Python
Python file for the gateway to be ran in Python anaconda on a computer (laptop preferred)
import uuid
import json
import paho.mqtt.client as mqtt
import time

time.sleep(2)

# Brain MQTT CREDENTIALS

mqtt_user_name = 'oauth2-user'
mqtt_password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'  # copy and paste here external client id from your account
user_id = 'USERNUMBER'  # copy and paste here your user id
device_id = 'XXXXXXXXXXXXXXXXXXXX'  # copy and paste here your device id
ind=""

alerts_topic = '/v1/users/{user_id}/in/alerts'.format(user_id=user_id)
acc_norm_datasource_topic = '/v1/users/{user_id}/in/devices/{device_id}/datasources/MOTION'.format(
    user_id=user_id,
    device_id=device_id)

ca_cert_path = 'cacert.crt'


def on_connect(client, userdata, flags, rc):
    print('Client 1 Connected with result code {code}'.format(code=rc))

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print ("Unexpected MQTT disconnection. Will auto-reconnect")

def on_message(client, userdata, msg):
    global ind
    global mqttc
    ind= json.loads(str(msg.payload.decode("utf-8")))
    print(ind[0]['name'])
    # CLOUD MQTT CREDENTIALS
    client2 = mqtt.Client()
    client2.username_pw_set("USER", "PASSWORD")
    client2.connect("m12.cloudmqtt.com", 12345)
    client2.publish("/ArmTopic",ind[0]['name'])

def main():    
    client1 = mqtt.Client(client_id="7624876298468192648726348726938", transport='websockets')
    client_iad1=str(uuid.uuid4())
    print(client_iad1)
    client1.on_connect = on_connect
    client1.on_message = on_message
    client1.on_disconnect = on_disconnect
    client1.tls_set(ca_certs=ca_cert_path)
    client1.username_pw_set(mqtt_user_name, mqtt_password)
    client1.connect('ns01-wss.brainium.com', 443)
    client1.subscribe(acc_norm_datasource_topic)
    client1.loop_forever()

if __name__ == "__main__":
    main()

BriniumRehab Github

https://github.com/EddOliver/Brainium-Rehab

Credits

EdOliver

EdOliver

38 projects • 74 followers
Engineer, Scientist, Maker. Entrepreneur and Futurist.
Thanks to Victor Altamirano.

Comments