mirobot / mirobot-py

A Python library to control Mirobot
MIT License
4 stars 10 forks source link

Mirobot throws Jason parse error after first command. #2

Open janislejins opened 7 years ago

janislejins commented 7 years ago

I'm a student at the royal college of art looking to extend the core functionality of Mirobot for a project.

Unfortunately after sending the first command (like mirobot forward, pen down etc) I get a Jason error and the whole process stalls. Is there a work around(?) can post code if required.

janislejins commented 7 years ago

*Json error (sorry on phone)

orgicus commented 7 years ago

@janislejins Having a read through the library I spotted this section. For every message sent, if there are any errors, they will be raised/thrown.

One option could be to wrap each call within a try/except block. The other is to setup an errorNotify callback so you can simply print these errors to the console instead of halting the program execution:

e.g.

from mirobot import Mirobot

def onMirobotError(x, msg, timeout,mirobotInstance):
    print x,msg,timeout,mirobotInstance

# Connect to Mirobot
mirobot = Mirobot()
mirobot.autoConnect()
# pass error notifications to the callback above instead of halting execution
mirobot.errorNotify(onMirobotError)

# Move forward 100mm
mirobot.forward(100)

bare in mind the above is Python 2 syntax, use print() for Python 3 (or simply pass if you don't really case :P)

tobiasbp commented 6 years ago

I also get errors when using this library (And I don't know how to make code pastes like right here?).

tbp@vaio:~/Documents/opfinderklubben/mirobot/python$ ./ok.py {u'devices': [{u'id': u'Mirobot-d2f5', u'updated': u'2018-05-03T13:32:55.254Z', u'name': u'Mirobot-d2f5', u'address': u'192.168.1.239'}, {u'id': u'Mirobot-e6ef', u'updated': u'2018-05-03T13:30:24.240Z', u'name': u'Mirobot-e6ef', u'address': u'192.168.1.161'}]} {"msg":"2.0.9","id":"cCgm0001","status":"complete"} {"msg":"JSON parse error","status":"error"} Traceback (most recent call last): File "./ok.py", line 14, in <module> mirobot.forward(10) File "/home/tbp/.local/lib/python2.7/site-packages/mirobot/__init__.py", line 107, in forward return self.__send('forward', distance, distance/20) File "/home/tbp/.local/lib/python2.7/site-packages/mirobot/__init__.py", line 144, in __send return self.__send_or_raise(msg, timeout) File "/home/tbp/.local/lib/python2.7/site-packages/mirobot/__init__.py", line 176, in __send_or_raise raise IOError("Received message ID (%s) does not match expected (%s)" % (rx_id, msg_id)) IOError: Received message ID (???) does not match expected (cCgm0002) {"id":"cCgm0002","status":"accepted"} {"msg":"JSON parse error","status":"error"} {"id":"cCgm0002","status":"complete"} Terminated

This is the programme I'm running: from mirobot import Mirobot mirobot = Mirobot() mirobot.autoConnect('Mirobot-d2f5') mirobot.forward(10)

orgicus commented 6 years ago

@tobiasbp for now, can you try something like this:

from time import sleep
from mirobot import Mirobot

def onMirobotError(x, msg, timeout,mirobotInstance):
    print x,msg,timeout,mirobotInstance

# Connect to Mirobot
mirobot = Mirobot()
mirobot.autoConnect('Mirobot-d2f5')
# pass error notifications to the callback above instead of halting execution
mirobot.errorNotify(onMirobotError)

# try to move forward 10mm
mirobot.forward(10)

#wait 3s
sleep(3)
#try again
mirobot.forward(10)

Hopefully the program won't crash but display the error message instead and maybe work on the following commands ¯_(ツ)_/¯