Python 3 variants for the DiddyBorg and PicoBorgRev examples

Hi,

I've done a quick search but it didn't turn up anything on the subject and before I get to work on porting, I would like to know if there are any variants for Python 3 for the DiddyBorg and PicoBorgRev code examples. I'd need the basic driving code, not necessarily the whole example list.

Best regards,
Mihai

piborg's picture

We have a Python 3 version of the main PicoBorgRev.py library here:
https://piborg.org/downloads/picoborgrev/PicoBorgRev3.py.txt

We do not have any Python 3 versions of any of the examples, but it is likely the only things which need to be changed are the calls to print. This should mean the conversion is fairly simple.

GeekyTim's picture

This needs updating to the latest version to match the new python version.

piborg's picture

This version of library has new been updated to support both old and new Raspbian :)

GeekyTim has also shared a Python 3 version of the sequence example here: diddySequence3.py

GeekyTim's picture

I'm actually GeekyTim now - was using an old handle here.

Hi,
I understand from a recent Forum post, that you will be making available all your software in Python 3. This is great new and timely for me. I have been converting my Diddyborg software to python 3 for the past 3 weeks. I am making good progress (Webserver starts, some functions work) but have run in to a show stopper, I cannot display any web pages on my browsers. There are two lines of code (in Green-original diddy code, Red my p3 code) I have made dozen(s) of iterations on from many web suggestion to no avail. The best result I have achieved is a blank Web page (with no errors on web page or terminal log). Interestingly I can enter (192.168.1.154:8081/Headlight) which works, and on one code variation (long forgotten) actually displayed “'HeadLights On” the Web page in addition to several other similar commands “Borglights On”. So, I’m asking for help on those two lines of code to P3 conversion (Red) while I wait for your P3 code conversions (hopefully coming soon).
Thank You,
John D
Essentially from the original diddyWeb code extract:

     httpText += '//-→</script>\
    httpText += '</head>\n'
     httpText += '<body>\n'
     httpText += '<body onLoad="setTimeout(\'readDistances()\', %d)">\n' % (displayDelay)
     httpText += '<div id="readDistances"><center>Waiting for first distance    reading...</center></div>\n'
     httpText += '</body>\n'
     httpText += '</html>\n'
     self.send(httpText
        else:
            # Unexpected page          
            #self.send('Path : "%s"' % (getPath))       #original diddy cod 
    self.send(('Path : "%s"' % (getPath)))  #My P3 version  
    print(('Path : "%s"' % (getPath)))          #P3 - now printing “/Favicon.ico”
    def send(self, content):
            #self.request.sendall('HTTP/1.0 200 OK\n\n%s' % (content)   #original diddy co  
    self.request.sendall(("HTTP/1.0 200 OK\r\n"+(content)).encode('utf-8'))     #One of many            of my P3 versions. Blank screen no error messages
    print(('HTTP/1.0 200 OK\n\n%s' % (content)))   Note: Is  printing source HTML code on terminal like                                 above extract

`Note: Browser screen goes Blank but no errors, the actual code keeps running can enter:
92.168.1.154:8081/Headlight #turn headlights on/off works
/servo #no effect, no errors
/distances #no effect, no errors
1.

piborg's picture

It looks like a few of the lines have some errors, but they might just be cut-and-paste issues putting the text on the forum.

Line 1 is missing the ending ' symbol, it should be:

httpText += '//-→\n'

You seem to be placing lots of entries in extra ( ) symbols, which may be causing them to become tuples. For example line 12 should probably be:

self.send('Path : "%s"' % (getPath))

Finally I think the original send function should work fine based on this page: https://docs.python.org/3/library/socketserver.html

What I would suggest is when you get the blank page in the browser see if the view source has any HTML code or not. If it does paste it into the forum and I will see if there is anything wrong with the HTML output. If it does not then there is probably a logic fault in the script causing the code to either not get to the send or failing to send.

We have a fair number of code examples we need to go through to make them Python 2 and 3 compatible, so it may be a while before we get around to them all. I expect robot code will be higher on the list though ;)

Code from original source file diddyWeb.py

    344	 def send(self, content):
    345    >>>  self.request.sendall('HTTP/1.0 200 OK\n\n%s' % (content)) <<<< This is original code line

Changed to :	>>> self.request.sendall(bytes(content,"utf-8")) <<<

Web Browser page now displays/functional most of the content (Forward, Reverse, Stop...) but not the streaming data.

The Streaming data does not work because (I believe) the following line of code has eluded my efforts of converting it to Python 3. Various error messages

174          >>>      self.send(sendFrame.tostring())    <<<<This is original code line

So, if you could conjure up the P3 version of this line I would be eternally. Note: I have tried many iterations to no avail.

Extract form diddyWeb.py

    168       if getPath.startswith('/cam.jpg'):
    169          # Camera snapshot
    170            lockFrame.acquire()
    171            sendFrame = lastFrame
    172            lockFrame.release()
    173            if sendFrame != None:
    174   >>>      self.send(sendFrame.tostring())    <<<<

P.S.

I just read your response. The extra "((" was because at the very end I use 2to3 python converter program which added the extra ((. I image this was because I had alreay made a convertion pass of the program, and it just went ahead and added them again anyways. So, I'll be going back to my original conversion. I did run the program before and after 2to3 and it appeared to work the same and didn't add anything new.

Thanks for the feedback

piborg's picture

I am glad to hear you have made some progress :)

My guess is that it is confusing the binary data from the image with text and trying to interpret it rather than sending it as-is. I would try converting the image data to bytes directly without the tostring call:

self.send(bytes(sendFrame))
Subscribe to Comments for &quot;Python 3 variants for the DiddyBorg and PicoBorgRev examples&quot;