Go to Post "Greatness isn't achieved by doing the uncommon things but instead by doing the common things uncommonly well." - Wayne C. [more]
Home
Go Back   Chief Delphi > Technical > Programming
Team 51   CD-Events   CD-Media   CD-Swap   CD-Spy   FRC-Spy   Unsung FIRST Heroes   WFA
portal register members calendar search Today's Posts Mark Forums Read FAQ rules
VEXpro
The Chief Delphi Forums are sponsored by Innovation First International, Inc.
OLHSA
ADVERTISEMENT

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 09-01-2012, 11:38 PM
DarthCoder DarthCoder is offline
Registered User
FRC #4309
 
Join Date: Aug 2012
Location: Washington
Posts: 10
DarthCoder is an unknown quantity at this point
TCP communication with cRIO

I've been trying to get TCP connections working with the cRIO. I have an Android app and robot code setup. I've gotten them to connect to each other, but every time I spawn a process it crashes the whole thing. This is my code, is there any options that I'm getting wrong?

App code:
Code:
public class BotSmithsAppActivity extends Activity 
{
	static final int PORT_NUMBER = 1500;
	static final String IP_ADDR = "10.43.9.2";
	
	static final int MOVE_ARM_UP = 0;
	static final int MOVE_ARM_DOWN = 1;
	
	//Inits socket
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Arm up button
        final Button upButton = (Button)findViewById(R.id.ARM_UP);
        upButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                sendAction(MOVE_ARM_UP);
            }
        });
        
        //Arm down button
        final Button downButton = (Button)findViewById(R.id.ARM_DOWN);
        downButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                sendAction(MOVE_ARM_DOWN);
            }
        });
    }
    
    void sendAction(int actionType)
    {
    	try
        {
			Socket s = new Socket(IP_ADDR, PORT_NUMBER);
			OutputStream out = s.getOutputStream();
			
			out.write(actionType);
			
			s.close();
		} 
        catch (UnknownHostException e) 
		{
			e.printStackTrace();
		} 
        catch (IOException e) 
		{
			e.printStackTrace();
		}
    }
}
Robot code:
Code:
VOID tcpServerWorkTask(int sFd, char* address, u_short port);

#define MOVE_ARM_UP 0
#define MOVE_ARM_DOWN 1

class AppControl;

AppControl* g_appCtrl = NULL;

class AppControl : public IterativeRobot
{
public:
	
	static const unsigned int MAX_CONNECTIONS = 1;
	static const unsigned int SERVER_WORK_PRIORITY = 100;
	static const unsigned int SERVER_STACK_SIZE = 1000;
	
	sockaddr_in m_serverAddr;
	int m_sFd;
	
	char m_workName[16];
	
	std::auto_ptr<Jaguar> m_armMotor;
	
	AppControl(void)
		:m_disabled(false)
	{
		g_appCtrl = this;
		
		m_armMotor.reset(new Jaguar(3));
	}

	void TeleopInit()
	{
		m_disabled = false;
		
		int sockAddrSize = sizeof(struct sockaddr_in);
		
		//Fill out server address
		m_serverAddr.sin_family = AF_INET;
		m_serverAddr.sin_len = sockAddrSize;
		m_serverAddr.sin_port = 1500;
		m_serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
		
		//Init socket.
		if ((m_sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR) 
		{ 
			perror("socket"); 
			printf("\n Error connecting to socket.\n\n");
			return;
		}
		printf("Initialized socket.\n");
		
		//Bind to socket
		if (bind(m_sFd, (struct sockaddr*)&m_serverAddr, sockAddrSize) == ERROR) 
		{ 
			perror("bind");
			close(m_sFd);
			printf("\n Error binding to socket.\n\n");
			return;
		}
		printf("Bound socket\n");
		
		//Set socket to listen for connections.
		if (listen(m_sFd, MAX_CONNECTIONS) == ERROR)
		{ 
			perror("listen");
			close(m_sFd);
			printf("\n Error setting socket to listen mode.\n\n");
			return;
		}
		printf("Listening on socket 1500\n");
		
		//Wait for connections
		FOREVER
		{
			if ( m_disabled )
			{
				break;
			}
			
			sockaddr_in clientAddr;
			int newFd = 0;
			
			//Accept connection from client
			if ((newFd = accept (m_sFd, (struct sockaddr*)&clientAddr, &sockAddrSize)) == ERROR) 
			{
				perror("accept");
				close(m_sFd);
				printf("\n Error accepting connection\n\n");
				return;
			}
			
			//Spawn work task
			printf("Spawning work task\n");
			if (taskSpawn(m_workName, SERVER_WORK_PRIORITY, 0, SERVER_STACK_SIZE,
			(FUNCPTR)tcpServerWorkTask, newFd,
			(int)inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), 
			0, 0, 0, 0, 0, 0, 0) == ERROR)
			{ 
				perror("taskSpawn");
				printf("\nError spawning task closing new file descriptor\n\n");
				close(newFd);
			}
		}
		
		printf("closing server socket.\n");
		close(m_sFd);
	}
	
	void TeleopDisabled()
	{
		m_disabled = true;
	}
	
	bool m_disabled;
	
	friend VOID tcpServerWorkTask(int sFd, char* address, u_short port);
};

VOID tcpServerWorkTask(int sFd, char* address, u_short port)
{
	unsigned int i = 0;
	read(sFd, (char*)&i, sizeof(unsigned int));
	
	std::cout << "Data: " << i << "\n";
	std::cout << "Port: " << (int)port << "\n";
	std::cout << "Address: " << address << "\n";
 	
	if ( g_appCtrl != NULL )
	{
		if ( i == MOVE_ARM_UP )
		{
			printf("Arm up\n");
			//g_appCtrl->m_armMotor->Set(-0.5f);
		}
		else if ( i == MOVE_ARM_DOWN )
		{
			printf("Arm down\n");
			//g_appCtrl->m_armMotor->Set(0.5f);
		}
	}
	
	printf("freeing address\n");
	free(address);
	printf("Closing file descriptor.\n");
	close(sFd);
	printf("Closed file descriptor.\n");
}

START_ROBOT_CLASS(AppControl);
I left out includes and imports. I get some Task Exited error. The error code is 1114118.
Reply With Quote
  #2   Spotlight this post!  
Unread 09-02-2012, 12:01 AM
Hjelstrom's Avatar
Hjelstrom Hjelstrom is offline
Mentor
FRC #0987 (High Rollers)
Team Role: Mentor
 
Join Date: Mar 2008
Rookie Year: 2005
Location: Las Vegas
Posts: 103
Hjelstrom is just really niceHjelstrom is just really niceHjelstrom is just really niceHjelstrom is just really nice
Re: TCP communication with cRIO

I'm not sure exactly what is wrong but I think you need a different architecture for your tcp server code on the robot. We were able to get things working using the examples on this website:

http://beej.us/guide/bgnet/

In particular, our "server" code uses 'select' to see if any of the clients have anything to say. We can then "poll" rather than putting our robot code inside of an infinite loop like you're having to do. This page has some information on using 'select':

http://beej.us/guide/bgnet/output/ht...ed.html#select

Using this, our server code was all inside a function call rather than the other way around and we didn't have to spawn any tasks.

Hope this helps!
Reply With Quote
  #3   Spotlight this post!  
Unread 09-02-2012, 05:42 AM
Greg McKaskle Greg McKaskle is offline
Registered User
no team (Team NI)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 3,372
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: TCP communication with cRIO

The VxWorks taskSpawn command has great flexibility, but on the down-side, it gives you plenty power to shoot yourself in the foot.

I looked up the error code and didn't quite find it. http://www.vxdev.com/docs/vx55man/vx...noNumList.html has memlib errors that go up to 1114117, but no ...8. All of those are related to a bad pointer or access to memory you aren't supposed to access. I don't have vxworks dev tools at home. Try searching for the error code. I suspect you'll find it in the memlib header file and it will give a hint as to the issue.

Meanwhile, the easiest problems to have with spawning a task are to make the stack too small or to leave floating point registers out of the stack frame which will lead to a corruption once you accidentally use floats. Try adding a zero to the stack size and reviewing all of the other jillion parameters to spawn to see if any of them sound necessary.

Greg McKaskle
Reply With Quote
  #4   Spotlight this post!  
Unread 09-02-2012, 10:22 AM
DarthCoder DarthCoder is offline
Registered User
FRC #4309
 
Join Date: Aug 2012
Location: Washington
Posts: 10
DarthCoder is an unknown quantity at this point
Re: TCP communication with cRIO

Okay, I'll test that out when I can actually access the robot.
Reply With Quote
  #5   Spotlight this post!  
Unread 09-02-2012, 10:23 AM
Wiifi's Avatar
Wiifi Wiifi is offline
Team mentor, coach and volunteer
AKA: Evan Hochstein
FRC #2470 (The Blitz Team)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Bloomington, Minnesota
Posts: 568
Wiifi will become famous soon enoughWiifi will become famous soon enough
Re: TCP communication with cRIO

I'm certainly not an expert when it comes to TCP packets and IP addresses as the such... But if your team number is 4309 shouldn't your ip address in the code be 10.43.09.2?? I believe you are representing yourself in the code as team 439. I'm probably wrong but that is my guess!

Quote:
static final String IP_ADDR = "10.43.9.2";
__________________
2008-2010 Team #2470 - Student Member
2010-? FRC #2470 Mentor
2012-? FRC #3081 Mentor
2012-? FTC #5943 Coach
GOFIRST at Dunwoody President
2013 WFFA recipient - 10k Lakes
Reply With Quote
  #6   Spotlight this post!  
Unread 09-02-2012, 10:28 AM
DarthCoder DarthCoder is offline
Registered User
FRC #4309
 
Join Date: Aug 2012
Location: Washington
Posts: 10
DarthCoder is an unknown quantity at this point
Re: TCP communication with cRIO

I can connect just fine using that ip address with everything else. But you might be right, I'll change it and see what happens. I've been able to spawn tasks, they just crash.
Reply With Quote
  #7   Spotlight this post!  
Unread 09-02-2012, 10:53 AM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Itinerant Programmer
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 6,783
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: TCP communication with cRIO

Quote:
Originally Posted by Wiifi View Post
But if your team number is 4309 shouldn't your ip address in the code be 10.43.09.2??
Um, no.
009 or 09 or 9 are all identical numbers.
The .9. translates into a bit pattern padded with leading zeros to fill out the field.
Adding more leading zeros has no effect.
__________________
"Everyone you will ever meet knows something you don't" - Bill Nye

Last edited by Mark McLeod : 09-02-2012 at 10:56 AM.
Reply With Quote
  #8   Spotlight this post!  
Unread 09-02-2012, 11:59 AM
Wiifi's Avatar
Wiifi Wiifi is offline
Team mentor, coach and volunteer
AKA: Evan Hochstein
FRC #2470 (The Blitz Team)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Bloomington, Minnesota
Posts: 568
Wiifi will become famous soon enoughWiifi will become famous soon enough
Re: TCP communication with cRIO

Quote:
Originally Posted by Mark McLeod View Post
Um, no.
009 or 09 or 9 are all identical numbers.
The .9. translates into a bit pattern padded with leading zeros to fill out the field.
Adding more leading zeros has no effect.
That is what I figured, logically though it confuses me. For sake of argument let's say we have team 439, 4309 and 4390 on the field all at the same time, what happens to communications then?
__________________
2008-2010 Team #2470 - Student Member
2010-? FRC #2470 Mentor
2012-? FRC #3081 Mentor
2012-? FTC #5943 Coach
GOFIRST at Dunwoody President
2013 WFFA recipient - 10k Lakes
Reply With Quote
  #9   Spotlight this post!  
Unread 09-02-2012, 12:23 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 7,051
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Send a message via AIM to Alan Anderson
Re: TCP communication with cRIO

Quote:
Originally Posted by Wiifi View Post
For sake of argument let's say we have team 439, 4309 and 4390 on the field all at the same time, what happens to communications then?
The IP addresses are all distinct:

Team 439: 10.4.39.X
Team 4309: 10.43.9.X
Team 4390: 10.43.90.X
Reply With Quote
  #10   Spotlight this post!  
Unread 09-10-2012, 05:43 PM
mjcoss mjcoss is offline
Registered User
FRC #0303
 
Join Date: Jan 2009
Location: Bridgewater,NJ
Posts: 69
mjcoss is a jewel in the roughmjcoss is a jewel in the roughmjcoss is a jewel in the roughmjcoss is a jewel in the rough
Re: TCP communication with cRIO

I have code that we used last year to have the driverstation send data to the crio. It spawns a task, and while it wasn't used to move the robot, it may help with what you are trying to do.

I'll post it when I get home. It is udp, not tcp because I didn't think we need the over head for what we were doing, and could afford to lose messages if need be. And it's not really robust but ...
Reply With Quote
  #11   Spotlight this post!  
Unread 09-10-2012, 08:05 PM
mjcoss mjcoss is offline
Registered User
FRC #0303
 
Join Date: Jan 2009
Location: Bridgewater,NJ
Posts: 69
mjcoss is a jewel in the roughmjcoss is a jewel in the roughmjcoss is a jewel in the roughmjcoss is a jewel in the rough
Re: TCP communication with cRIO

Here's the code that we used. Worked fine for what we needed, and hopefully helps you.

Code:
#include "WPILib.h"
#include "Tracker.h"
#include "sockLib.h" 
#include "inetLib.h" 
#include "stdioLib.h" 
#include "strLib.h" 
#include "ioLib.h" 
#include "fioLib.h"

#define BAD_ANGLE			361.0
#define BAD_DISTANCE		-1.0

struct request { 
	char message[1023];     /* message buffer */ 
};

int
udpServer(Tracker *p) 
{ 
	struct sockaddr_in  serverAddr;    /* server's socket address */ 
	struct sockaddr_in  clientAddr;    /* client's socket address */ 
	struct request      clientRequest; /* request/Message from client */ 
	int                 sockAddrSize;  /* size of socket address structure */ 
	int                 sFd;           /* socket file descriptor */ 
	char                inetAddr[INET_ADDR_LEN];
	Tracker::targetInfo_t 		myinfo;
	

	sockAddrSize = sizeof(struct sockaddr_in); 
    bzero((char *)&serverAddr, sockAddrSize);
    serverAddr.sin_family = AF_INET; 
    serverAddr.sin_port = htons(p->getPort()); 
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
	if((sFd = socket(AF_INET, SOCK_DGRAM, 0)) == ERROR) { 
		perror("socket"); 
		return ERROR;
	}
	
    if(bind(sFd, (struct sockaddr *)&serverAddr, sockAddrSize) == ERROR) { 
    	perror("bind");
    	close(sFd);
    	return ERROR;
    }
    for(;;) {
       	char *ptr = clientRequest.message;
 
        if(recvfrom(sFd, (char *)&clientRequest, sizeof(clientRequest), 0, 
            (struct sockaddr *)&clientAddr, &sockAddrSize) == ERROR) {
            perror("recvfrom");
            close(sFd);
            return ERROR;
        }
        
        inet_ntoa_b(clientAddr.sin_addr, inetAddr);
        
        p->packetsReceived++;
       	memcpy((char *)&(myinfo.angle),    ptr, sizeof(myinfo.angle));    ptr += sizeof(myinfo.angle);
       	memcpy((char *)&(myinfo.distance), ptr, sizeof(myinfo.distance)); ptr += sizeof(myinfo.distance);
       	myinfo.isvalid = *ptr++;
       	p->setTarget(&myinfo);
	} 
 }

Tracker::Tracker(Solenoid *inner, Solenoid *outer, ushort port)
{
	m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
	m_tinfo.isvalid  = false;
	m_tinfo.angle    = BAD_ANGLE;
	m_tinfo.distance = BAD_DISTANCE;
	m_port  = port;
	m_inner = inner;
	m_outer = outer;
	m_needFree = false;
	m_task = new Task("udpDashInfo", (FUNCPTR)udpServer);
	m_task->Start((UINT32)this);
	packetsReceived = 0;
	return;
}

Tracker::Tracker(Solenoid &inner, Solenoid &outer, ushort port)
{
	m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
	m_tinfo.isvalid  = false;
	m_tinfo.angle    = BAD_ANGLE;
	m_tinfo.distance = BAD_DISTANCE;
	m_port  = port;
	m_inner = &inner;
	m_outer = &outer;
	m_needFree = false;
	m_task = new Task("udpDashInfo", (FUNCPTR)udpServer);
	m_task->Start((UINT32)this);
	packetsReceived = 0;
	return;
}

Tracker::Tracker(UINT8 inner, UINT8 outer, ushort port)
{
	m_semaphore = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
	m_tinfo.isvalid  = false;
	m_tinfo.angle    = BAD_ANGLE;
	m_tinfo.distance = BAD_DISTANCE;
	m_port  = port;
	m_inner = new Solenoid(inner);
	m_outer = new Solenoid(outer);
	m_needFree = true;
	m_task = new Task("udpDashInfo", (FUNCPTR)udpServer);
	m_task->Start((UINT32)this);
	packetsReceived = 0;
	return;
}

Tracker::~Tracker()
{
	semFlush(m_semaphore);
	if(m_task) {
		m_task->Stop();
		delete m_task;
	}
	if(m_needFree) {
		delete m_inner;
		delete m_outer;
	}
	return;
}

bool
Tracker::targetAquired()
{
	return m_tinfo.isvalid;
}

float
Tracker::getAngle()
{
	targetInfo_t t;
	
	getTarget(&t);
	return t.angle;
}

float
Tracker::getDistance()
{
	targetInfo_t t;
	
	getTarget(&t);
	return t.distance;
}

ushort
Tracker::getPort()
{
	return m_port;
}

void
Tracker::getTarget(targetInfo_t *t)
{
	CRITICAL_REGION(m_semaphore)
	{
		memcpy((char *)t, (char *)&m_tinfo, sizeof(*t));
		if(!m_tinfo.isvalid) {
			t->angle    = BAD_ANGLE;
			t->distance = BAD_DISTANCE;
		}
	}
	END_REGION;
	return;
}

void
Tracker::setTarget(targetInfo_t *t)
{
	CRITICAL_REGION(m_semaphore)
	{
		m_tinfo = *t;
		if(!m_tinfo.isvalid) {
			t->angle    = BAD_ANGLE;
			t->distance = BAD_DISTANCE;
		}
	}
	END_REGION;
}

void
Tracker::illuminateTarget(bool on)
{
	m_inner->Set(on);
	m_outer->Set(on);
	return;
}
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 12:52 PM.

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


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © Delphi and Pontiac Central High School