Monday 30 April 2018

RC-Car using Flask

WebIOPi is dead. It doesn't work with the latest raspbian.

[2018-10-15]
I have been trying other IOT programs without success.
In Linux Format October 2017 LXF229 there was an article about using Flask with an example.
sudo apt-get install python3-flask.
mkdir rc-far
cd rc-car
mkdir templates
save app.py in the rc-car directory
save index.html in the templates directory

[Need to install filezilla and transfer files to the Pi. Need to debug.] - leave for now. Copied and pasted contents of files using nano.

to test: browse from another device to 192.168.1.8:5000

[2018-10-16] created script with two lines as below and it worked.
Problems. Cannot stop forward/backward.
2. formatting of HTML.

[2020-8-26] Latest version of Raspian -Buster does not contain gpiozero

sudo apt install python3-gpiozero  needs to be done.


[2018-4-30]
It didn't work because the author Les Pounder forgot to add one or two steps. He was very quick to reply to me and we got it sorted.  The lines that I needed were:
export FLASK_APP=/home/pi/rc-car/app.py
flask run –host=192.168.1.8

I have now put these into a shell script file that I run to start things happening.

My file structure is
/home/pi/rc-car/template/jquery

It made use of GpioZero libraries that are different to those I was using before.  The main problem was that his example only seemed to use one motor, and I have two motors.  I did get it working to drive the main wheels.

A search found an example using flask that did have two motors, but this is one on each wheel, whereas my configuration is one to drive forwards and backwards, and one to turn the front wheels.
This is from a github example by Kevin Olson https://github.com/olsonk/web-robot
I put this on the Pi and it failed. I have sorted out one of the problems. In the app.py program the debugging print statements are in the wrong format. They have now changed to a function call and need the parameters in ().  Sorted, But it still fails.
I have found an email for kevin Olson and asked him. We shall see.
The Html file calls a javascript that is in a certain place. I don't have it. I tried to download  jquery and apt-get said I had the latest version.  I have now found a version on-line at https://code.jquery.com/jquery-3.3.1.js
 and created a folder 'jquery' and put this file in there, edited the html, and am ready to test.

Aside: For what it's worth, I have replaced the motor drivers by the Deek-Robot  board using the L293D chip. http://www.deek-robot.com/productshow.asp?id=17&small=4



I had changed the text in the index .html file from {{ Title}} to {{ RC-6 Olson model }} . It didn't like that. I think Title within {{...}} is passed over from the app.py?

Also it failed because it didn't like the file name index.html. I changed it to main.html and it worked. The browser sees it. Unfortunately the LEDs do not work. I always have this problem with identifying them, and I wonder if GPioZero Libraries use different numbering than I am used to.

Tomorrow?
2018-05-02 Not touched code. I bolted the Raspberry Pi zero down to the car chassis. It required making two flanges from an old plastic box and drilling the holes in it to attach it via the Meccano bolts and some very fine bolts that I found in my junk box. The RC car was a Meccano toy originally. I also cut down a piece of dowel to act as a mast for the Wireless dongle as the R-Pi zero has a 4 inch converter cable from micro-USB to USB.

GPIO Pins                   Wire Colour             Deek-Robot
6                                    Black                         GND
12                                  Orange                       In1
16                                  green                          In2
17                                  Red                            In4
21                                  Yellow                       In3

This seems to work all backwards using the WebIOPi program Left = right, FWD = Backwd.



2018-05-08
Catching up. I got it working  using Les Pounders programs. New problem is that forward backward only stay on for a short period.  I got rid of the Internet stuff by using the jscript stuff that i installed following the Olson instructions.

Here are the motor controls.   the sleep lines are commented out.
led = LED(25)
motor = Motor(forward=18, backward=23)
turn  = Motor(forward=10, backward=11) 


In LP's stuff the html is tidier, but doesn't put nice boxes. This is all in one line across the page with wrapping.

   Click on the buttons to trigger the action
                                                               

   

                                                                                       
    Motor Forward    
    Toggle LED       
    Motor Backward
    Motor Left   
    Motor Right
   


Here is the Olson code: 

                                  
               

               


                       
                       
               

               


                       
               

                


which does have separate lines for the Left/right 'buttons' 

Need to think about how I do it for the first one. 
Note. This writeup doesn't show the code, it shows the result. How do I fix that? 

--> No. LP stuff does have boxes, Forward/LED/Backward on the first line, and Left/right on the next line. It does use an external bit of code.  Problem is, I can only turn off forward by pressing Backward. LED Toggle works, and because I have forward/backward selected, I can't do Left/Right. 
Hmm.

2019-04-26

Update, I now have another car, labelled '2' It has the Raspberry Pi with SD card #3 in it which is the Flask code, but still requires an access point. Card #4 and #7 have the access point in the R-Pi so are not tethered to an Access point.

Anyway, yesterday I tried this car and noticed the ground wire between the R-Pi and the driver board started smoking and melting. Today it doesn't work, now that the wire has been rerouted. The batteries are good on both parts.  The R-Pi has been tested on my test rig and is OK.  The main difference since I 'built' this car some months ago are that the the wires now go from the R-Pi to the board through two 16 pin Dil Headers, one with the wires soldered into it and one on the board where the IC was.

OK, I tested without the driver board powered and just cheking the outputs of the R-Pi on to the board, and they were all coming out as -4.56V. Whoo!. What I discovered is that the ground wire to teh R-Pi was on pin4 (+5V), not pin 6 as it should be.  Fired it all up again and it sort of works, except everything is backwards. Forward on the web page makes it go backward, and Left turns Right!. Easy to fix.

Lets make the following changes:
GPIO pin 12 = brown  --> Red
GPIO pin 16 = red   -->Brown

GPIO Pin 23 = White --> Black
GPIO Pin 19 = Black --> White

One pin on the IC on the driver board does not make a connection so needs resoldering.