Week 6 8

1 minute read

WEEK 6-8

  1. Study the code of the interfaces of the robot of the WebSim repository. More in detail the function getObject(). To understand it I studied the OpenCV library for image processing.

  2. I have carried out a proof of concept of the use of Chrome WebSerial Api for communication through the serial port. I have made two small examples and to send messages to the serial port I have created a web page.

    1. The first example => Turn on led arduino board
      • First I have added to Arduino a program that reads from the serial port and turns the LED on / off according to what it receives.
          char input;
        
          void setup(){
        
              pinMode(LED_BUILTIN, OUTPUT); // Declaramos que utilizaremos el pin 2 como salida
              digitalWrite(LED_BUILTIN,LOW);
              Serial.begin(9600);
          }
        
          void loop(){
              if (Serial.available()>0){
        
                  input=Serial.read();
             
                  if (input=='1'){
        
                      digitalWrite(LED_BUILTIN, HIGH); 
        
                  }
        
                  else if(input=='0')
        
                  {
        
                      digitalWrite(LED_BUILTIN, LOW);
        
                  }
        
              }
          }
        
      • Through the web if I select the led field I send a ‘1’ and if I remove the field send a ‘0’. The board receives these messages and turns the LED on or off depending on the message received.
    2. The second example => Movimiento Mbot
      • First I added a program to the Mbot to move (forward / backward / left / right / stop) based on the message received by the serial port.
          char input;
          const int pinPWMA = 6;
          const int pinAIN1_AIN2 = 7;
          const int pinPWMB = 5;
          const int pinBIN1_BIN2 = 4;
        
          void setup()
          {
              pinMode(pinPWMA, OUTPUT);
              pinMode(pinAIN1_AIN2, OUTPUT);
        
              pinMode(pinPWMB, OUTPUT);
              pinMode(pinBIN1_BIN2, OUTPUT);
              Serial.begin(9600);
          }
        
          void loop()
          {
              if (Serial.available()>0){
            
                  input=Serial.read();
                            
                  if (input=='0'){
                      Avance(255);
                      delay(2000); 
                  }
                  else if(input=='1')
                  {
                      Retroceso(255);
                      delay(2000);
                  }
                  else if(input=='2')
                  {
                      Izquierda(255);
                      delay(2000);
                  }
                  else if(input=='3')
                  {
                      Derecha(255);
                      delay(2000);
                  }
                  else if(input=='4')
                  {
                      Parar();
                      delay(2000);
                  }
              }
          }
        
          void Avance(int velocidad)
          {
              analogWrite(pinPWMA, velocidad);
              digitalWrite(pinAIN1_AIN2, LOW);
        
              analogWrite(pinPWMB, velocidad);
              digitalWrite(pinBIN1_BIN2, HIGH);
          }
        
          void Retroceso(int velocidad)
          {
              analogWrite(pinPWMA, velocidad);
              digitalWrite(pinAIN1_AIN2, HIGH);
        
              analogWrite(pinPWMB, velocidad);
              digitalWrite(pinBIN1_BIN2, LOW);
          }
        
          void Izquierda(int velocidad)
          {
              analogWrite(pinPWMA, velocidad);
              digitalWrite(pinAIN1_AIN2, HIGH);
        
              analogWrite(pinPWMB, velocidad);
              digitalWrite(pinBIN1_BIN2, HIGH);
          }
        
          void Derecha(int velocidad)
          {
              analogWrite(pinPWMA, velocidad);
              digitalWrite(pinAIN1_AIN2, LOW);
        
              analogWrite(pinPWMB, velocidad);
              digitalWrite(pinBIN1_BIN2, LOW);
          }
        
          void Parar()
          {
              analogWrite(pinPWMA, 0);
              digitalWrite(pinAIN1_AIN2, HIGH);
        
              analogWrite(pinPWMB, 0);
              digitalWrite(pinBIN1_BIN2, HIGH);
          }
        
      • Through the web selecting the movement. It is sent by the serial port

        MOVEMENT MESSAGE
        Advance ‘0’
        Back ‘1’
        Left ‘2’
        Rigth ‘3’
        Stop ‘4’

Updated: