Giter VIP home page Giter VIP logo

robotics-networking's Introduction

ROBOTICS NETWORKING

Robotics networking stuff.

To install, run:

python2.7 setup.py install --user

This will make a user install. Make sure your environment sees executables in the local path.

If you're developing, use the following command:

python2.7 setup.py develop --user

Any changes you make will take effect immediately. No need to reinstall.

Then you can start the server using the following command (this is the executable that comes along with this library):

roboticsnet-server

And if you want to give a few things a shot manually, you can test with the client executable. You can run help to see what kind of commands are currently supported manually.

usage: roboticsnet-client [-h] [--forward FORWARD] [--reverse REVERSE]
[--forwardLeft FORWARDLEFT] [--queryproc] [--graceful] [--host HOST] [--port PORT] [--testall] [--startvid] [--stopvid]
optional arguments:
-h, --help show this help message and exit
--forward FORWARD
 send forward command, given a value from 1 to 63
--reverse REVERSE
 send reverse command, given a value from 1 to 63
--forwardLeft FORWARDLEFT
 send forwardLeft command, given a value from 1 to 63

--forwardRight FORWARDRIGHT send forwardRight command, given a value from 1 to 63 --reverseLeft REVERSELEFT send reverseLeft command, given a value from 1 to 63 --reverseRight REVERSERIGHT send reverseRight command, given a value from 1 to 63 --queryproc send query about what is currently running --graceful shutdown server gracefully --host HOST specify an alternate host to default localhost --port PORT specify an alternate port to default 5000 --testall sends a command of each --startvid request video to start running --stopvid request video to stop running

So after running the server, with the above command (roboticsnet-server), you can send a packet this way:

$ roboticsnet-client --forward 23

Using port: 5000

Using host: localhost

Send move command...

Done!

And on the server side you should get the following:

Send things to motors

Received: 0x1 0x17

Make move command

Send things to motors

Overall Setup

For the moment this is how things should look like:

doc/sc-dep-diag.png

Server Hooks

It is possible to use this module without actually altering it. The way that this is done, is by providing hooks to the server's commands. The commands execute whenever there is a request that is received and processed. After the request is processed and matched against the proper command, the command is executed. Each command is associated with a hook. So after the command (pattern) is executed, the hook is executed right after.

There exists an example you can read in 'roboticsnet/examples/hook_example.py'.

We will go over segments of the above piece by piece in order for the reader to have an easier time understanding what is happening.

import roboticsnet
from roboticsnet.command_hook import CommandHook
from roboticsnet.rover_listener import RoverListener

def _forwardHook():
    ...

def _turnHook():
    ...

def _queryProcHook():
    ...

def _reverse=_reverseHook():
    ...

def _startVideo():
    ...

# First you would need to define your hooks using CommandHook
cmd_hook = CommandHook(
        forward=_forwardHook,
        turn=_turnHook,
        queryproc=_queryProcHook,
        reverse=_reverseHook,
        startVideo=_startVideoCount
        )

l = RoverListener(hooks=cmd_hook)
l.listen()

The above example starts a listening server with hooks. The 'def's prefixed with '_' are our cutsom hooks. We can provide any method we want in order to get this to execute arbitrary code. So for example, each time a 'forward' command is received, then the '_forwardHook()' method will actually execute once the request is done processing. This is how you attach your added, wanted behavior.

To do this we need an extra structure which stores this information (what hooks to execute whenever a particular command is received). We use an object called 'CommandHook', and set each of these hooks individually. You can omit hooks, and that will be fine - it simply means we do not want to bind any more behavior to a command.

You could also create classes, and pass their methods as hooks as well. Here is another example which is located in 'examples/hook_with_params.py'.

import roboticsnet
from roboticsnet.command_hook import CommandHook
from roboticsnet.rover_listener import RoverListener

forward_count = 0

class Counter:
    def __init__(self):
        self.count = 0

    def incr(self):
        self.count += 1

    def get(self):
        return self.count

def _forwardHook(params):
    print "This is my custom forward hook!"
    print "And in my custom forward hook, the params I receive are: ", params
    print "And I extract the value of interest: ", params['value']

def _turnHook():
    print "This is turn hook, where I don't care about the params (even though"
    print "we actually do receive params"

myCounter = Counter()

cmd_hook = CommandHook(
        forward=_forwardHook,
        turn=_turnHook,
        startVideo=myCounter.inrc
        )

l = RoverListener(hooks=cmd_hook)
l.listen()

print "The startvideo command was received this many times: ", myCounter.get()

That should conclude most of what you need to know about hooks!

Polling Services

Polling services have been added to the networking library. What this means is that you're able to pass code blocks, which will then be spawned as monitoring services.

The way this is achieved is by creating a monitoring service object which contains the code block you pass. It waits every specified time unit, and will then invoke the code block.

Essentially the code block you pass must return the value you need. How this ties all together in the end is that when the client will request for sensor info (see sensinfo in PROTOCOL), the networking library will go over all the monitoring services, and get the last value obtained from the polling. Then all of that information is packaged appropriately, and sent back to the client for consumption.

A sample file for these monitoring services may be found in

robotics-networking/examples/service_example.py
import roboticsnet
from roboticsnet.command_hook import CommandHook
from roboticsnet.rover_listener import RoverListener

forward_count = 0

def _forwardHook():
    global forward_count
    print "This is my custom forward hook!"
    forward_count += 1

def polling_service():
    """ Returns the same number all the time; for testing purposes """
    print "polling service 1 is executed!"
    return 42

def polling_service_2():
    """ Just another service """
    print "Service 2!"
    return 24

def cat():
    """ somehow a cat made it into the software! """
    print "MEW MEW MEW MEW"
    return 'cat'

# You don't need hooks in this case, but just to show that you can use them
# anyway.
cmd_hook = CommandHook(
        forward=_forwardHook
        )

l = RoverListener(\
        hooks=cmd_hook,
        # And again we bind the polling services here
        monitorProcs=[\
            polling_service,
            polling_service_2,
            cat])

print roboticsnet.__appname__, " ",  roboticsnet.__version__
print "Starting command dispatcher..."
l.listen()

print "The server is completely oblivious to the following information:"
print "  - forward commands received: ", forward_count

robotics-networking's People

Contributors

psyomn avatar msnidal avatar doomfest avatar

Stargazers

 avatar

Watchers

 avatar  avatar James Cloos avatar  avatar Bruce B Bakshi avatar yeshwanth avatar  avatar  avatar Miguel Jimenez avatar  avatar Eric avatar Kishanlal  avatar  avatar Mila Roisin avatar  avatar Claudia Feochari Eng. avatar Aleb avatar Cedric Martens avatar  avatar Artem Mikhalitsin avatar

Forkers

sichitong

robotics-networking's Issues

Monitoring Services

This is something that needs to run in the background, and is mainly used for monitoring different sensors or other parts of the system. These kind of need to be generic, run in the background, and set a variable with the particular information we're interested in.

Make Tests, and add Continuous Integration

Well, this is going to be funky. I'm going to need to figure out how I can test client/server communications using some sort of testing framework for python. I think this would be pretty good to have.

Once that is done, I'm going to add more automated services which can help (like Travis CI, and maybe something more python specific - we'll see).

Need to check all the movements and the values they send.

We need to take a look at all the values the client sends to server and if the server properly gets them and sends them to the micro manager properly. It seems like the forward, reverse, turn and sotp comamnds do nothing because hte c;lient sends valeus anyway.

Protocol needs to be updated.

The protocol needs to be update to:
For data sent to arduino:
command + val2 + val1 + reserved.

Each part is a hex number. The whole packet will be 4 bytes.

command will be a hex table with all the commands for the rover.
val2 and val1 are values needed to be sent to the sensors + motors.
reserved is a revserved byte for extra options in the future.

For data sent from arduino back to the process:

[we need to figure out which format will be coming back to process]

This is a good start might be changed a bit in the near future when we get more information on the sensors.

Figure out/fix LAN client-server connection

Right now we're not sure if we can connect the client and the server across LAN (sputnik router) since something funky is going on there. We should double check ports and test the connection again, and try to debug where the problem is.

Sensor information packaging and relaying

This should allow for the following facilities:

  • Allow a programmer to import a data structure, where different sensors can be added
  • Different values/magnitudes may be set on these sensors
  • Turn on and off different sensors
  • Deserialize, and Serialize this data structure over communications

Depends on #44

Protocol review

The networking code uses Python's multiprocessing library to send messages over the network. We should do more research - are these messages sent with TCP or UDP? We should compare our requirements for the networking library to the expected performance and decide whether any changes are necessary.

Hooks

Ability to include this module as a library, supply callable hooks such that when requests are processed, we can execute some code.

Logger

Write a logger for the networking stuff. For this, it would be nice if we make it store the logs in common directories.

For linux, make it store the logs in: $HOME/.config/roboticsnet/log.txt

For Windows it should store it in: %APPDATA%/roboticsnet/log.txt

I think this is a good beginner task for anyone that wants to give a try at python.

You should be able to log the following under the categories:

  • Information
  • error
  • warning
Log.i("I'm logging information")
Log.w("I'm logging a warning")
Log.e("I'm logging an error")

Which should return something like:

[DD/MM/YYYY HH:MM:SS] [INFO] I'm logging information
[DD/MM/YYYY HH:MM:SS] [WARN] I'm logging information
[DD/MM/YYYY HH:MM:SS] [ERRO] I'm logging information

Pinging Service

There should be a pinging service that checks whether the service is connected or disconnected. This will also give us information about latency etc.

Process Hierarchy

Need an organization on processes - who is child of who, and what can be killed or stopped.

Provide comfortable, and changable IPs/Ports

Just make the manual client, be able to take in changing information about IPs and ports. In the future, this information might slightly change as we're not going to be running everything on localhost :P.

Add reverse to protocol

The rover (apparently) needs a reverse functionality.

cc: @Doomfest we talked about this but I don't remember why we chose this organization. Can you elaborate?

Close sockets on server shutdown (graceful)

If you try and restart the server after having already run it and shut it down via the --graceful command, it produces this error:

rover@rover-desktop ~/programming/robotics-rover/roboticsrov $ roboticsrov-test
Connected to port. /dev/ttyACM0
Waiting some time for microcontroller's serial to startup...
Robotics Rover   0.0.1
Starting command dispatcher...
Listening on port:  5000
Traceback (most recent call last):
  File "/home/rover/.local/bin/roboticsrov-test", line 10, in <module>
    execfile(__file__)
  File "/home/rover/programming/robotics-rover/roboticsrov/bin/roboticsrov-test", line 26, in <module>
    server.listen()
  File "/home/rover/programming/robotics-networking/roboticsnet/rover_listener.py", line 33, in listen
    l = Listener(address)
  File "/usr/lib/python2.7/multiprocessing/connection.py", line 132, in __init__
    self._listener = SocketListener(address, family, backlog)
  File "/usr/lib/python2.7/multiprocessing/connection.py", line 256, in __init__
    self._socket.bind(address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

https://gist.github.com/msnidal/9b32639bd42335c03e9e

Right now I'm not sure if this is a network or a rover problem, but it looks like the sockets aren't properly closed somewhere. We should identify the exact problem and fix this so we can restart the server if we need to.

Client side human usability.

I am thinking about the values on the client side being rate of change in degrees. Like the client can tell the rover to turn left at a rate of 20 degrees/s. It will make it easier for humans to use.

What do you think?

Get Basic Client/Server interactions working

Need the foundations laid out for simple communications between a client and a
server. After we stub a few commands and test them out, we'll be able to iterate
through each one and add more functionality.

Investigate distributed timestamps

As a driver, I want to make sure that when the rover executes a move command that I've sent, the command was very recent. Furthermore, the rover motors should stop if there hasn't been a command recently.

The best way to achieve both of these in a reasonable way is with timestamps integrated into the networking code. We should look into distributed timestamps/lamport timestamps and then add them to robotics-networking commands.

Make simple UML diagrams of the system

This should help developers that are to join the project + the research students which want to give a shot at introducing self healing to the system.

@msnidal Help is appreciated, but I know you don't have much time (so don't feel bad if you can't make it).

Going to start getting this out of the way tonight.

Fix command delay lag.

We will also need ot see if the commands are delayed in networking code.

This issue is related to robotics-rover#25

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.