Jose Romaní
Published © GPL3+

Put Attention: Brain Controlled Mecanum Wheel

Thanks to EEG, the Arduino robot car makes left turns. Speedier or slower according to the level of attention meassured

AdvancedShowcase (no instructions)144
Put Attention: Brain Controlled Mecanum Wheel

Things used in this project

Hardware components

omni direction mecanum wheel robotic kit 2
I bought it in Amazon
×1
NeuroSky MindWave Mobile 2 BrainWave Starter
Also in neurosky.com
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
18650 battery
In local store in Vigo
×1

Software apps and online services

Arduino IDE
Arduino IDE
unzip files

Story

Read more

Code

mindwave_mecanum_left_turn - copy.ino

Arduino
Download from to the Arduino Mega of the mecanum wheel robot
/*
Brain Controlled Mecanum Wheel Robot. Left turns.
by Jose Romani https://sites.google.com/view/pi9hmb/
25/05/2020
based on code of osoyoo and neurosky, found on the internet.
*/

int move_speed = 60 ;
#define MIN_SPEED  50 //60 or 50
#define MAX_SPEED  250

#define speedPinR 9   //  RIGHT WHEEL PWM pin D45 connect front MODEL-X ENA 
#define RightMotorDirPin1  22    //Front Right Motor direction pin 1 to Front MODEL-X IN1  (K1)
#define RightMotorDirPin2  24   //Front Right Motor direction pin 2 to Front MODEL-X IN2   (K1)                                 
#define LeftMotorDirPin1  26    //Left front Motor direction pin 1 to Front MODEL-X IN3 (  K3)
#define LeftMotorDirPin2  28   //Left front Motor direction pin 2 to Front MODEL-X IN4 (  K3)
#define speedPinL 10   // Left WHEEL PWM pin D7 connect front MODEL-X ENB

#define speedPinRB 11   //  RIGHT WHEEL PWM pin connect Back MODEL-X ENA 
#define RightMotorDirPin1B  5    //Rear Right Motor direction pin 1 to Back MODEL-X IN1 (  K1)
#define RightMotorDirPin2B 6    //Rear Right Motor direction pin 2 to Back MODEL-X IN2 (  K1) 
#define LeftMotorDirPin1B 7    //Rear left Motor direction pin 1 to Back MODEL-X IN3  K3
#define LeftMotorDirPin2B 8  //Rear left Motor direction pin 2 to Back MODEL-X IN4  k3
#define speedPinLB 12    //   LEFT WHEEL  PWM pin D8 connect Rear MODEL-X ENB

//-----------------------------------------
//mindwave headset parameters--------------
#define MINDWAVE_BAUD 57600
// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {
  0
};
byte poorQuality = 0;
byte attention = 0;
boolean bigPacket = false;
//---------------------------------------

//changes the wheels speed and commands lef_turn
void mindwaveSpeed(int attention) {
  int control;
  control = map(attention, 0, 100, MIN_SPEED, MAX_SPEED); //it is very rare to achieve the top attention
  move_speed = control;
  left_turn(move_speed); 
}

// Read data from Serial UART //
byte ReadOneByte() {
  int ByteRead;
  while (!Serial1.available());
  ByteRead = Serial1.read();
  return ByteRead;
}

void mindwaveAttent() {
  // Look for sync bytes
  if (ReadOneByte() == 170)
  {
    if (ReadOneByte() == 170)
    {
      payloadLength = ReadOneByte();
      if (payloadLength > 169)                     //Payload length can not be greater than 169
        return;

      generatedChecksum = 0;
      for (int i = 0; i < payloadLength; i++)
      {
        payloadData[i] = ReadOneByte();            //Read payload into memory
        generatedChecksum += payloadData[i];
      }

      checksum = ReadOneByte();                     //Read checksum byte from stream
      generatedChecksum = 255 - generatedChecksum;   //Take one's compliment of generated checksum
      if (checksum == generatedChecksum)
      {
        poorQuality = 200;
        attention = 0;

        for (int i = 0; i < payloadLength; i++)
        { // Parse the payload
          switch (payloadData[i])
          {
            case 2:
              i++;
              poorQuality = payloadData[i];
              bigPacket = true;
              break;
            case 4:
              i++;
              attention = payloadData[i];
              break;
            case 5:
              i++;
              break;
            case 0x80:
              i = i + 3;
              break;
            case 0x83:
              i = i + 25;
              break;
            default:
              break;
          } // switch
        } // for loop
        if (bigPacket && poorQuality == 0){Serial.println(attention); //to the serial monitor. Debugg
          mindwaveSpeed(attention);} //changes the wheels speed and commands left turns
        bigPacket = false;
      }
      else
      {
        //Checksum Error
      }  // end if else for checksum
    } // end if read 0xAA byte (170 en binario)
  } // end if read 0xAA byte

}


/*motor control*/
void left_turn(int speed) {
  RL_bck(0);
  RR_fwd(speed);
  FR_fwd(speed);
  FL_bck(0);
}


void FR_fwd(int speed)  //front-right wheel forward turn
{
  digitalWrite(RightMotorDirPin1, LOW);
  digitalWrite(RightMotorDirPin2, HIGH);
  analogWrite(speedPinR, speed);
}

void FL_bck(int speed) // front-left wheel backward turn
{
  digitalWrite(LeftMotorDirPin1, HIGH);
  digitalWrite(LeftMotorDirPin2, LOW);
  analogWrite(speedPinL, speed);
}

void RR_fwd(int speed)  //rear-right wheel forward turn
{
  digitalWrite(RightMotorDirPin1B, LOW);
  digitalWrite(RightMotorDirPin2B, HIGH);
  analogWrite(speedPinRB, speed);
}


void RL_bck(int speed)    //rear-left wheel backward turn
{
  digitalWrite(LeftMotorDirPin1B, HIGH);
  digitalWrite(LeftMotorDirPin2B, LOW);
  analogWrite(speedPinLB, speed);
}


//Pins initialize
void init_GPIO()
{
  pinMode(RightMotorDirPin1, OUTPUT);
  pinMode(RightMotorDirPin2, OUTPUT);
  pinMode(speedPinL, OUTPUT);

  pinMode(LeftMotorDirPin1, OUTPUT);
  pinMode(LeftMotorDirPin2, OUTPUT);
  pinMode(speedPinR, OUTPUT);
  pinMode(RightMotorDirPin1B, OUTPUT);
  pinMode(RightMotorDirPin2B, OUTPUT);
  pinMode(speedPinLB, OUTPUT);

  pinMode(LeftMotorDirPin1B, OUTPUT);
  pinMode(LeftMotorDirPin2B, OUTPUT);
  pinMode(speedPinRB, OUTPUT);

}

void setup() {
  init_GPIO();
  Serial.begin(57600);
  Serial1.begin(MINDWAVE_BAUD); //The other Serial port is used for debugging 
}

void loop() {
  mindwaveAttent();
}

Credits

Jose Romaní

Jose Romaní

5 projects • 0 followers
Industrial Technical Engineer in Electronics and Automation

Comments