Go to Post Now that the robot is shipped, maybe I can get to bed before the alarm clock goes off =] - DarMagi [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 09-01-2017, 06:35
ProfessorAlekM ProfessorAlekM is offline
Registered User
FRC #6190
 
Join Date: Jan 2016
Location: Canton, Michigan
Posts: 33
ProfessorAlekM has a little shameless behaviour in the past
Robot will not drive when using UDP in autonomous

Hello!

I've wrote UDP code to communicate with a beaglebone from the roboRIO, however when the I put on autonomous mode and the UDP is used none of the motors work, and when I change back to teleop I still can't control anything.

Here is the code:

Code:
public class Robot extends IterativeRobot {
	//Beaglebone Communications
    DatagramSocket serverSocket;
    byte[] receiveData;
    byte[] sendData;
Code for robot init
Code:
try {
			serverSocket = new DatagramSocket(9876);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	receiveData = new byte[1024];
        sendData = new byte[1024];
Code:
public void autonomousPeriodic() {
    	
    	//Receive Data
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        try {
			serverSocket.receive(receivePacket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String sentence = new String( receivePacket.getData());
        System.out.println("RECEIVED: " + sentence);
        InetAddress IPAddress = receivePacket.getAddress();
        int port = receivePacket.getPort();

        //Return data
        String capitalizedSentence = sentence.toUpperCase();
        sendData = capitalizedSentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        try {
			serverSocket.send(sendPacket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        switch (sentence) {
        	case "a":
        		myRobot.tankDrive(0.5, 0.5);
        	break;
        	
        	case "b":
        		myRobot.tankDrive(0,0);
        	break;
        }

        Timer.delay(0.005);

    }
Thanks for any help!
Reply With Quote
  #2   Spotlight this post!  
Unread 09-01-2017, 09:40
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 377
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Robot will not drive when using UDP in autonomous

Quote:
Originally Posted by ProfessorAlekM View Post
Hello!

I've wrote UDP code to communicate with a beaglebone from the roboRIO, however when the I put on autonomous mode and the UDP is used none of the motors work, and when I change back to teleop I still can't control anything.

Here is the code:

Code:
public class Robot extends IterativeRobot {
	//Beaglebone Communications
    DatagramSocket serverSocket;
    byte[] receiveData;
    byte[] sendData;
Code for robot init
Code:
try {
			serverSocket = new DatagramSocket(9876);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	receiveData = new byte[1024];
        sendData = new byte[1024];
Code:
public void autonomousPeriodic() {
    	
    	//Receive Data
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        try {
			serverSocket.receive(receivePacket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String sentence = new String( receivePacket.getData());
        System.out.println("RECEIVED: " + sentence);
        InetAddress IPAddress = receivePacket.getAddress();
        int port = receivePacket.getPort();

        //Return data
        String capitalizedSentence = sentence.toUpperCase();
        sendData = capitalizedSentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        try {
			serverSocket.send(sendPacket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        switch (sentence) {
        	case "a":
        		myRobot.tankDrive(0.5, 0.5);
        	break;
        	
        	case "b":
        		myRobot.tankDrive(0,0);
        	break;
        }

        Timer.delay(0.005);

    }
Thanks for any help!
You may be running into a Java subtlety here.

Code:
boolean eq = ("abc" == "abc");
System.out.println(eq); // false!
I'm not entirely sure, but it may be the same problem in your switch statement. Try using String.equals in an if-else instead.

Otherwise, use the debugger to verify that sentence contains the "a" or "b" you need.
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
  #3   Spotlight this post!  
Unread 09-01-2017, 09:47
AustinShalit's Avatar
AustinShalit AustinShalit is offline
Registered User
AKA: אוסטין
no team (WPILib Suite Developer)
 
Join Date: Dec 2013
Rookie Year: 2008
Location: Los Angeles/Worcester/Israel
Posts: 144
AustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of light
Re: Robot will not drive when using UDP in autonomous

From the documentation for DatagramSocket.receive():
Quote:
This method blocks until a datagram is received.
This means it will wait for information from the beaglebone before continuing with your user program. This waiting causes the motors to not be updated fast enough. I see two options to solve this:
We have a NetworkTables build for the beaglebone on the WPILib maven server (armhf):
http://first.wpi.edu/FRC/roborio/mav...NetworkTables/

Take a look at this document for more information:
http://wpilib.screenstepslive.com/s/...client-pc-side
__________________
Reply With Quote
  #4   Spotlight this post!  
Unread 09-01-2017, 09:50
AustinShalit's Avatar
AustinShalit AustinShalit is offline
Registered User
AKA: אוסטין
no team (WPILib Suite Developer)
 
Join Date: Dec 2013
Rookie Year: 2008
Location: Los Angeles/Worcester/Israel
Posts: 144
AustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of lightAustinShalit is a glorious beacon of light
Re: Robot will not drive when using UDP in autonomous

Quote:
Originally Posted by euhlmann View Post
You may be running into a Java subtlety here.

Code:
boolean eq = ("abc" == "abc");
System.out.println(eq); // false!
I'm not entirely sure, but it may be the same problem in your switch statement. Try using String.equals in an if-else instead.

Otherwise, use the debugger to verify that sentence contains the "a" or "b" you need.
That looks ok to me. Take a look at this Oracle document: https://docs.oracle.com/javase/8/doc...gs-switch.html
__________________
Reply With Quote
  #5   Spotlight this post!  
Unread 09-01-2017, 13:20
ProfessorAlekM ProfessorAlekM is offline
Registered User
FRC #6190
 
Join Date: Jan 2016
Location: Canton, Michigan
Posts: 33
ProfessorAlekM has a little shameless behaviour in the past
Re: Robot will not drive when using UDP in autonomous

As it turns out, the string being sent was not just a single letter, It was a single letter with another 1023 filler characters. When I made it split up the string based on spaces the switch function worked.

Also thank you for the networking table content, I will look into setting up a seperate thread because the robot "jitters" and moves a tiny bit every time a udp packet is received!
Reply With Quote
  #6   Spotlight this post!  
Unread 09-01-2017, 14:23
BenBernard BenBernard is offline
Registered User
FRC #5687 (The Outliers)
Team Role: Mentor
 
Join Date: Jan 2016
Rookie Year: 2015
Location: Portland, ME
Posts: 45
BenBernard is on a distinguished road
Re: Robot will not drive when using UDP in autonomous

The beauty of NetworkTables is that they've dealt with the threading issues for you already. If you aren't familiar with/comfortable with multithreaded programming in Java, tread carefully on the UDP route!
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi