HB_Stratos
Published © GPL3+

Making the DRL ASURO Robot work with the Arduino IDE

After two weeks of work I am happy to present my findings how to make ASURO work with modern software.

IntermediateProtip1,230
Making the DRL ASURO Robot work with the Arduino IDE

Things used in this project

Hardware components

Asuro Robot Kit by Arexx
Also required are the included CD-ROM as well as the also included infrared flash stick.
×1

Software apps and online services

Arduino IDE
Arduino IDE
Asuro Flash
Provided with the ASURO Robot kit
HTerm
ASURINO Libary
Windows 10
Microsoft Windows 10

Story

Read more

Code

Functions to Import

C/C++
Copy this code in your arduino sketch, then you can start writing programs for asuro. (replace the standard code)
Refer to "Functions" for instructions how to use the functions
#include <Asuro.h>
Asuro asuro = Asuro();
//FUNCTIONS-----------------------------------------------------------
int currentSpeedR = 0;
int currentSpeedL = 0;

void motorSpeedR_(int speedR){
  //speed input
  analogWrite(10, abs(speedR));

  //forward/backward
  if (speedR >= 0) {
    digitalWrite(12, LOW);
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
  }
}
void motorSpeedL_(int speedL){
  //speed input
  analogWrite(9, abs(speedL));

  //forward/backward
  if (speedL >= 0) {
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
  } else {
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
}
void motorSpeed_(int L, int R){
  motorSpeedR_(R);
  motorSpeedL_(L);  
}

void motorSpeedR(int speedR){
  int speedBefore = currentSpeedR;
  currentSpeedR = speedR;

  if(speedBefore == 0){
    if(speedR > 0){
      motorSpeedR_(255);
      delay(75);  
    }else if(speedR < 0){
      motorSpeedR_(-255);
      delay(75);  
    }
  }
  motorSpeedR_(speedR);
}
void motorSpeedL(int speedL){
  int speedBefore = currentSpeedL;
  currentSpeedL = speedL;

  if(speedBefore == 0){
    if(speedL > 0){
      motorSpeedL_(255);
      delay(75);  
    }else if(speedL < 0){
      motorSpeedL_(-255);
      delay(75);  
    }
  }
  motorSpeedL_(speedL);
}
void motorSpeed(int L, int R){
  motorSpeedR(R);
  motorSpeedL(L);  
}

int readEncoderR(){
  return (analogRead(A0));
}
int readEncoderL(){
  return (analogRead(A1));
}

boolean readEncoderRdigital(){
  return digitalRead(A0);  
}
boolean readEncoderLdigital(){
  return digitalRead(A1);
}

int readSwitchesRaw(){
  int Average = 0;
  int MeasurementsToAverage = 16;
  for(int i = 0; i < MeasurementsToAverage; ++i)
  {
    Average += analogRead(A4);
    delay(1);
  }
  Average /= MeasurementsToAverage;
  return(Average);
}
int getSwitches(){
  int s = readSwitchesRaw();
  if(s > 500){
    return 0;  
  }else if(s >= 50){
    return 1;  
  }else if(s >= 25){
    return 2;  
  }else if(s >= 12){
    return 3;
  }else if(s >= 4){
    return 4;  
  }else if(s >= 2){
    return 5;  
  }else if(s >= 0){
    return 6;  
  }else{
    return 0;  
  }
}
boolean switchesPressed(){
  if(readSwitchesRaw() <= 700){
    return true;  
  }else{
    return false;  
  }
}

int readLineSensorR(){
  return analogRead(A2);
}
int readLineSensorL(){
  return analogRead(A3);  
}

void setLedFront(boolean onoff){
  if(onoff){
    digitalWrite(6, HIGH);  
  }else{
    digitalWrite(6, LOW);  
  }
}

void setLedStatusGreen(boolean onoff){
  if(onoff){
    digitalWrite(8, HIGH);  
  }else{
    digitalWrite(8, LOW);  
  }
}
void setLedStatusRed(boolean onoff){
  if(onoff){
    digitalWrite(2, HIGH);  
  }else{
    digitalWrite(2, LOW);  
  }
}

//Setup---------------------------------------------------------------
void setup()
{
  asuro.Init();
  Serial.begin(2400);

  //left motor
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
  //right motor
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  //encoder LEDs
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  //Linesensor LED
  pinMode(6, OUTPUT);
  //Status LED
  pinMode(2, OUTPUT);
  pinMode(8, OUTPUT);
}

//LOOP----------------------------------------------------------------
void loop(){

}

Example 1 - The "Wall Hater"

C/C++
This code will make the Robot drive forwards until it hits a wall, then it will drive backwards a bit, turn and continue driving. Requires the Functions form "Code to Copy
// The 'Wall-Hater': This code will make the Robot drive forwards until it hits a wall, then it will drive backwards a bit, turn and continue driving
void loop()
{
  if(!switchesPressed()){      //If no switch is pressed 
    motorSpeed(255, 255);      //drive forwards full speed
  }else{                       //If a switch is pressed
    motorSpeed(-255, -255);    // revese full speed
    delay(500);                                  //for half a second
    motorSpeed(255, -255);     //left motor full forward, right motor full backward
    delay(250);                //pause code for half  a second while turning
  }                            // then continue (go to top of loop), if no switch is pressed with driving straight
}

Example 2 - The "Line Follower"

C/C++
This code uses the right light sensor to follow a line (black line on white background).
Requires the Functions from "Code to Copy
//The 'Line Follower': this code uses the right light sensor to follow a line (black line on white background)
void loop()
{
  setLedFront(true);                    //enables the front LED to make reading values possible.
  Serial.println(readLineSensorR());    //prints the linesensor value to serial (read with HTerm)

  if(readLineSensorR() > 200){          //if it sees white
    motorSpeed_(0, 100);                //it turns the right motor on to turn towards the line
  }else{                                //if it sees black
    motorSpeed_(100, 0);                //it turns the left motor on to turn towards the white again
  }
}

Failed Speed Test Code

C/C++
If you want to use this code directly (not recomended) you will have to integrate it with the functions from "Code to Copy" as they are needed to make it work. I recomend to not use this code but to make your own, improved version of it.
//set variables for the filter
int prevMillis = 0; const float filterSetting = 0.5;
int countR = 0; int nowFreqR = 0; int smoothFreqR = 0; boolean encoderStateR = false; boolean prevEncoderStateR = false; int angleRawR = 0;
int countL = 0; int nowFreqL = 0; int smoothFreqL = 0; boolean encoderStateL = false; boolean prevEncoderStateL = false; int angleRawL = 0;

void runFreq(){            // !IMPORTANT! call as often as possible (loop)
  //read encoder changes   //reading Code commented, done with interrupts, see below
  //Right Side
  /*encoderStateR = trueEncoderR();
  if(encoderStateR == !prevEncoderStateR){
    prevEncoderStateR = encoderStateR;
    countR++;
    angleRawR++;
    
  }
  //Left Side
  encoderStateL = trueEncoderL();
  if(encoderStateL == !prevEncoderStateL){
    prevEncoderStateL = encoderStateL;
    countL++;
    angleRawL++;
  }*/
  
  if((millis() - prevMillis) > 250){    
  //this function is called every loop, but still will only read every 250 ms
  //it is meant caculate a smoothd freqency value from the interrupt count
  // here a modification if the function is not called often enough to        
  //to compensate for the excess time that has passed may be an idea
  //Right Side  
    prevMillis = millis(); //reset the counter
    nowFreqR = countR;  //make the value from the readings the now point
    smoothFreqR = filterSetting * nowFreqR + (1 - filterSetting) * smoothFreqR;  //filter the nowFreq value (exponential filter)
    countR = 0;
    //Left Side  //same as right side
    nowFreqL = countL;
    smoothFreqL = filterSetting * nowFreqL + (1 - filterSetting) * smoothFreqL;
    countL = 0;
  }  
}

int getFreqR(){  //Simple functions to get the smoothed value as the run function gets called every loop and and does not output the 
//working with the global variables only also works
  return smoothFreqR;
}
int getFreqL(){
  return smoothFreqL;  
}

/*void setLedStatusRed(boolean onoff){  //this needs to be commented out of the original functions as PD2 becomes an interupt IN
  if(onoff){
    digitalWrite(2, HIGH);  
  }else{
    digitalWrite(2, LOW);  
  }
}*/

void interruptR(){
  countR++;  
}
void interruptL(){
  countL++;
}

void setup(){
  //Status LED
    //pinMode(2, OUTPUT); //pin output disabled as it becomes an interrupt in (as mentioned ^)
  pinMode(8, OUTPUT);
  //Interrupts
  pinMode(2, INPUT_PULLUP);  //set pin as an input
  attachInterrupt(digitalPinToInterrupt(2), interruptL, RISING);  //attach an interrupt to the rising side of the digitalIN
  pinMode(3, INPUT_PULLUP);  //same as the other one
  attachInterrupt(digitalPinToInterrupt(3), interruptR, RISING);
}

void loop(){
  runFreq();  //call this fuction every loop, loop should not have any delays
}

Credits

HB_Stratos
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.