It is currently Tue Jan 21, 2020 1:00 am
       


All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: RP6 Wifi Linux
PostPosted: Wed Mar 16, 2016 6:52 pm 
Offline
New User

Joined: Wed Mar 16, 2016 6:36 pm
Posts: 3
Hello,

i would like to exchange data between a RP6 robot (with M256) and an android phone. But the robot doesn't seem to receive any data. So i tried to replace the phone with a computer and it doesn't work too.


You can find here the remote control code :
Code:
/*
* ****************************************************************************
* RP6 ROBOT - Remote Control
* ****************************************************************************
* Example: WIFI_REMOTE 1.1 - WIFI Remote Control From a Android device
* Author: Aikaterini Minardou
* ****************************************************************************
* Description: 
*   In this program the WLAN Module receives the commands that are sent from the
*  the user's Android device and it is also programs the microcontroller
*   to continious update the user about sensor values (Light, Battery level,
*  Bumpers state and  )
*
*/
// Includes:

#include "RP6M256Lib.h"       // M256 Lib

#include "RP6I2CmasterTWI.h"   // I2C Master Lib

/*****************************************************************************/
/*****************************************************************************/
// Include the "RP6 Control M256 I2C Master library".
// This allows I2C control of the Robot base in a nearly identical API as
// on the Robot Base.

#include "RP6M256_I2CMasterLib.h" 
/****************************************************************************/
/**
* WIFI receiver.
* This checks if a whole input line has been received.

*/
uint8_t getInputLine_WIFI(void)
{
   if(getBufferLength_WIFI())                   
      return parseLine_WIFI(readChar_WIFI());
   return 0;
}


/*****************************************************************************/

// Behaviour command type:

#define IDLE  0
#define RANGE 15  // used for the light sensors

// The behaviour command data type:
typedef struct {
   uint8_t  speed_left;  // left speed *** both used for
   uint8_t  speed_right; // right speed *** turning commands
   unsigned dir:2;       // direction (FWD, BWD, LEFT, RIGHT)
   unsigned move:1;      // move flag // if it move
   unsigned rotate:1;    // rotate flag // or if it will rotate and move
   uint16_t move_value;  // move value is used for distance and angle values
   uint8_t  state;       // state of the behaviour
} behaviour_command_t;

behaviour_command_t STOP = {0, 0, FWD, false, false, 0, IDLE};   
//The STOP behaviour//

/*****************************************************************************/
/*****************************************************************************/
// Behaviour WIFI:

#define WIFI_ACTIVE 1
#define WIFI_EXECUTE_CMD 2

behaviour_command_t wifiControl = {0, 0, FWD,
                           false, false, 0, IDLE};


/**
* This function allows the remote controll according the received commands from the WLAN
* module
* This behaviour first waits to be activated, then it surpresses all other behaviours
* (except for the low battery behaviour).
*
*/
void wifi_Control(void)
{
   static uint8_t speedl = 40;
   static uint8_t speedr = 40;
   
   while(true)
   {
   if(getInputLine_WIFI())
         {
            if(strcmp(receiveBuffer_WIFI,"cmd")==0)
            {
               writeString_P_WIFI("\nWIFI COMMAND MODE ACTIVE\n");
            writeString_P("\nWIFI COMMAND MODE ACTIVE\n");
            }
            else
            {
               writeString_P_WIFI("\nENTER CMD\n");
            writeString_P("\nENTER CMD\n");
            }
         }
   }
   
   /*switch(wifiControl.state)
   {
      case IDLE:
         if(getInputLine_WIFI())
         {
            writeString_WIFI("getInputLineWIFI():");
            writeString_WIFI(receiveBuffer_WIFI);
            if(strcmp(receiveBuffer_WIFI,"here")==0)
            {
               writeString_P_WIFI("Hello User");
               wifiControl.state = WIFI_ACTIVE;
               break;
            }
         }
      break;
      case WIFI_ACTIVE:
         if(getInputLine_WIFI())
         {
            if( strcmp(receiveBuffer_WIFI,"fwd")==0)
            {
               wifiControl.speed_left = speedl;
               wifiControl.speed_right = speedr;
               writeString_P_WIFI("FWD!");
               wifiControl.dir = FWD;
            }
            else if(strcmp(receiveBuffer_WIFI,"bwd")==0)
            {
               wifiControl.speed_left = speedl;
               wifiControl.speed_right = speedr;
               writeString_P_WIFI("BWD!");
               wifiControl.dir = BWD;
            }
            else if(strcmp(receiveBuffer_WIFI,"left")==0)
            {
               if(speedl/2 >= 30)
               {
                  wifiControl.speed_left = speedl / 2;   // reduce speed for rotation otherwise
                  wifiControl.speed_right = speedr / 2;  // it can be quite fast
               }
               else if(speedl > 10)
               {
                  wifiControl.speed_left = 30;
                  wifiControl.speed_right = 30;
               }
               writeString_P_WIFI("LEFT!");
               wifiControl.dir = LEFT;
            }
            else if(strcmp(receiveBuffer_WIFI,"right")==0)
            {
               if(speedl/2 >= 30)
               {
                  wifiControl.speed_left = speedl / 2;
                  wifiControl.speed_right = speedr / 2;
               }
               else if(speedl > 10)
               {
                  wifiControl.speed_left = 30;
                  wifiControl.speed_right = 30;
               }
               writeString_P_WIFI("RIGHT!");
               wifiControl.dir = RIGHT;
            }
         }
      break;
      default:
         wifiControl.state = IDLE;
      break;
   }*/
}

/**
Checks to see if any obstacle is found
if not it will continue
else it will warn the user
**/
void warnForObs(void){

if(obstacle_right && obstacle_left)
   writeString_P_WIFI("Obsahead");
else if(obstacle_left)
   writeString_P_WIFI("Obsleft");
else if(obstacle_right)
   writeString_P_WIFI("Obsright");

}


/**
* Timed Watchdog display - the other heartbeat function
* does not work in this example as we use blocked moving functions here.
*/
void watchDogRequest(void)
{
   static uint8_t heartbeat2 = false;
   if(heartbeat2)
   {
      heartbeat2 = false;
   }
   else
   {
      setCursorPosLCD(1, 14);
      heartbeat2 = true;
   }
}

/*****************************************************************************/
// Behaviour control:

/**
* This function processes the movement commands that the behaviours generate.
* Depending on the values in the behaviour_command_t struct, it sets motor
* speed, moves a given distance or rotates.
*/
void moveCommand(behaviour_command_t * cmd)
{
   if(cmd->move_value > 0)  // move or rotate?
   {
      if(cmd->rotate)
         rotate(cmd->speed_left, cmd->dir, cmd->move_value, false);
      else if(cmd->move)
         move(cmd->speed_left, cmd->dir, DIST_MM(cmd->move_value), false);
      cmd->move_value = 0; // clear move value - the move commands are only
                           // given once and then runs in background.
   }
   else if(!(cmd->move || cmd->rotate)) // just move at speed?
   {
      changeDirection(cmd->dir);
      moveAtSpeed(cmd->speed_left,cmd->speed_right);
   }
   else if(isMovementComplete()) // movement complete? --> clear flags!
   {
      cmd->rotate = false;
      cmd->move = false;
   }
}
#define BATTERY_LOW 1
behaviour_command_t checkLowBattery = {0, 0, FWD,
                           false, false, 0, IDLE};

/*This function sends a string to the control interface about the levels of
brightness in the area*/

void light_detection(void){

   if(   adcLSL <RANGE && adcLSR < RANGE){
      writeString_P_WIFI(" DARK");
   } else{
      writeString_P_WIFI("BRIGHT");
   }

}

void behaviour_checkLowBattery(void)
{
}

/**
* This is the Event Handler and it gets called when the Battery Voltage
* is getting low! The Parameter isVoltageLow is true, when the voltage
* is low and false, when the voltage is OK.
*/

void batteryVoltageLow(uint8_t isVoltageLow)
{

    if(isVoltageLow)
   {

      writeString_P_WIFI("LOW Battery");
      writeIntegerLength_WIFI(adcBat,DEC,4);
   }
   else
   {
      writeString_P_WIFI("Battery Ok!");
      writeIntegerLength_WIFI(adcBat,DEC,4);
   }
}

/**
* The behaviourController task controls the subsumption architechture.
* It implements the priority levels of the different behaviours.
*
*
*/
void behaviourController(void)
{
    // Call all the behaviour tasks:
   wifi_Control();

    // Execute the commands depending on priority levels:
   if(checkLowBattery.state != IDLE) // Highest priority - 2
   {
      writeString_P_WIFI("BATTERY OK!!!");
      
   }else if(wifiControl.state != IDLE) // Priority - 1
   {
      moveCommand(&wifiControl);
   }
   else                     // Lowest priority - 0
   {
      moveCommand(&STOP);  // Default command - do nothing!
                      // In the current implementation this never
                      // happens.
   }
}



// I2C Requests:

/**
* The I2C_requestedDataReady Event Handler
*/
void I2C_requestedDataReady(uint8_t dataRequestID)
{
   checkRP6Status(dataRequestID);
}

/*****************************************************************************/
// I2C Error handler

/**
* This function gets called automatically if there was an I2C Error like
* the slave sent a "not acknowledge" (NACK, error codes e.g. 0x20 or 0x30).
*/
void I2C_transmissionError(uint8_t errorState)
{
   writeString_P("\nI2C ERROR - TWI STATE: 0x");
   writeInteger(errorState, HEX);
   writeChar('\n');
}

/*****************************************************************************/
// Main function - The program starts here:

int main(void)
{
   initRP6M256();
   initLCD();

   // ---------------------------------------
   WDT_setRequestHandler(watchDogRequest);

   // ---------------------------------------
   I2CTWI_initMaster(100); 
   I2CTWI_setRequestedDataReadyHandler(I2C_requestedDataReady);
   I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);


   setLEDs(0b1111);
   mSleep(1000);
   setLEDs(0b0000);
   
   // ---------------------------------------
   // Setup ACS power:
   I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_ACS_POWER, ACS_PWR_OFF);
   // Enable Watchdog for Interrupt requests:
   I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT, true);
   // Enable timed watchdog requests:
   I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT_RQ, true);

   startStopwatch1();
   startStopwatch2();
   startStopwatch3();
   
   while(true)
   {
      /*writeIntegerLength_WIFI(adcBat,DEC,4);
      mSleep(600);
      warnForObs();
      mSleep(600);
      task_checkINT();
      mSleep(600);
      light_detection();
      mSleep(600);
       task_I2CTWI();
      mSleep(600);*/
      behaviourController();
   }
   return 0;
}



And here the Client Java code :
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient2 {
    String serverHostname = new String("192.168.1.18");
    BufferedReader stdIn;
    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    public void open(){
        System.out.println("Attemping to connect to host " + serverHostname
                + " on port 2001.");
        try {
            echoSocket = new Socket(serverHostname, 2001);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + serverHostname);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for " + "the connection to: "
                    + serverHostname);
        }
    }


    public void send(String s){
        out.println(s);
    }

    public static void main(String[] args) {
        EchoClient2 ec = new EchoClient2();
        ec.open();
        ec.send("cmd\r\n");
    }
}


Regards.


Top
 Profile E-mail  
 
 Post subject: Re: RP6 Wifi Linux
PostPosted: Tue Mar 22, 2016 6:58 pm 
Offline
Administrator
User avatar

Joined: Fri Mar 25, 2005 6:19 pm
Posts: 1633
Location: NRW
Hello,

did you close the connection to RobotLoader before starting your Java program?
In theory it supports multiple connections, but not in such simple cases.

You should add several debug text outputs via the normal SERIAL interface with cable.
This makes it MUCH easier to see what happens (and what not happens).

Maybe first try to let two Java programs (on the same PC) communicate.
See if this works at all like you did it - might be problematic that your program terminates too early...

Best Regards,
SlyD


Top
 Profile E-mail  
 
 Post subject: Re: RP6 Wifi Linux
PostPosted: Fri Mar 25, 2016 3:22 pm 
Offline
New User

Joined: Wed Mar 16, 2016 6:36 pm
Posts: 3
First, I try with two Java program on the same PC and it works.

And by closing the connection with RobotLoader and using SERIAL communication, i managed to receive the data on the robot.

Thanks for your help.


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group