go away spammer

Author Topic: Monitoring a telnet I/O socket for messages, while running a script?  (Read 2423 times)

0 Members and 1 Guest are viewing this topic.

Offline SciTech02Topic starter

  • Robot Overlord
  • ****
  • Posts: 136
  • Helpful? 3
  • Life's but a green duck with soy sauce. -MegaHAL
My robot works by means of a telnet connection; from the robot's software, to my scripts (an API session).  I send commands to the socket that control the robot's software, or I watch the socket for any sent messages (from the robot's sensors).  For the most part, this works alright; if you know what message you want to wait for (a move completion notice, for example).  However, for monitoring sensory data, it does not work so well.  Basically, the Python object library contains a module for controlling a telnet session (telnetlib), and all of the functions for reading data from the socket work the same.  It basically waits, until a defined message is received.  Otherwise, it does nothing else (works good for movement, since the movement cannot be interupted).  What I am trying to do is monitor the telnet socket for sensory data, while my script runs.  Here's a simple pseudocode example of what I'm trying to do (This code makes the robot rotate, unless sensory data is detected by and sent from the robot's software; which makes it stop moving and the program ends):

DO:

sendCommand('rotate')

IF (sensory data detected) THEN:
   sendCommand('stop moving')
   sendCommand('speak "I have detected something"')
   end program

LOOP

In order to implement this, it seems like I have to use multiprocessing/threading techniques (one script endlessly monitors the socket, and the other script uses this data at the same time).  However, this shouldn't be necessary; other robot scripts I've seen monitor sensory data and make decisions accordingly, without multiprocessing/threading.

Any help will be appreciated, since this problem is above my programming abilities at the moment (I've never encountered a problem like this). :-\
« Last Edit: December 24, 2008, 10:28:35 PM by SciTech02 »
Check out the Evolution Robotics, ER1 robot, and ERSP Resource Page: http://www.societyofrobots.com/member_tutorials/node/336

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: Monitoring a telnet I/O socket for messages, while running a script?
« Reply #1 on: December 25, 2008, 09:14:21 AM »
You are implementing your logic wrong.
You don't need to wait for a message, you use a state machine, and depending on the received message, you alter the state machine's value.

For example:
you want to go forward -> set state machine value to 153 -> send go forward command -> resume whatever happens
[some other stuff happens, user clicks, cpu halts, etc]
'i am going forward' reply is received -> you check if state machine is 153 (you don't want the robot to go forward unless you orderd him, otherwise, he might be going down a slope, and you need to send some other command perhaps?) -> if it is, and the robot is going forward, you set the state machine to 189

basically, a state machine algorithm is made o variable (state variable) and a lot of decision blocks.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline SciTech02Topic starter

  • Robot Overlord
  • ****
  • Posts: 136
  • Helpful? 3
  • Life's but a green duck with soy sauce. -MegaHAL
Re: Monitoring a telnet I/O socket for messages, while running a script?
« Reply #2 on: December 26, 2008, 06:48:44 PM »
I see what you mean.  That idea has been in my head for several days, but I was thinking of using it for keeping track of what type commands are being executed (not using it to keep commands-in-progress from being interrupted).  I wrote a quick, throwaway program that utilizes this technique and it worked perfectly.  Another reason I was having trouble was because I haven't worked with monitoring sensory data for a long time (in the last few months, I was mostly creating algorithms and learning how to use my robot).  I remembered from my old BOE-Bot days that you checked your sensors first, then you decided what to do; depending what they told you (obstacle, bright light, etc.).  With this in mind, I made the script watch the socket for messages for a second, check if any "if..." conditions were met, and then check the state value.  As I said, it worked perfectly.  :)

Thanks for helping me resolve this problem.
Check out the Evolution Robotics, ER1 robot, and ERSP Resource Page: http://www.societyofrobots.com/member_tutorials/node/336

 


Get Your Ad Here

data_list