Well, isn't that a surprise!?... no, not really
Back in mid-December 2023, I made a statement, more for myself than anything in an attempt to "force myself" to put aside some time for "me" and do something I want/need to do.
Did I do it? Did I b*ll*x. Why not? well, you guessed it. Yep, those underpaying, whip cracking, slave masters that I am forced to serve insisted that I dedicate my time to earn them more mulla. sigh. So, I wrote the beginnings of an application that will become a COrE system that was just enough for me to hand-over to 3-4 other people to continue with. I think they'll take it further with minimal guidance from myself and that will "free me up" to at least get some "me" time.
Saying that, it took until 6pm on a Saturday in mid-January for me to get around to starting what I wanted to do - and boy was it really small BABY steps, but at least I'm forcing the issue on myself. Why did it take so long? well, I got a tiny bit distracted by the dimensional blurs going on in the good old USA atm, it's not in mainstream media so you could very well have missed it & I would say 90% probably have, I'll just say, bayside mall miami, and leave you to your own sleuthing.
Right, back to what I was doing. K9 using a PICO W driven by JavaScript. YES, Java-F*cking-Script, none of that Python b*llsh*t that we are being force fed by everyone, I initially setup the Pico W to run on top of a Robo Pico board and that forced me to use CircuitPython, which was "fine", I got it tested and proved out the point of what I wanted to do.....but y'know, as I've always said, Python is cardboard - it's great for testing / proving a point, but it has now become the lazy option for deployment and real production use. I actually wanted to (and will do) write the code in C. like it should be done.
However, I reference a while back a project called, KALUMA, that is a JavaScript engine that runs on the Pico and Pico W and you can write good old JavaScript code to interface with the GPIOs and devices connected to your Pico. Yes, you could argue that I'm just swapping one bad thing for another, however, I've been coding JavaScript since the mid-90's, both client side and server-side (many many years before noodeJS was even a twinkle in someones eye), it has served me well, it IS scalable (don't believe the Java naysayers on that one) and it is "closer to C", so easier to port over when I need to do that & that's the whole point. Yes, Python has C libraries underneath it & a lot of people like to point that out to me - however, it's the coders who then write the Python code BADLY that destroys any form of performance or credibility, they'll use libraries like pandas to load dataframes and leave 5-6 of them floating around in memory - rather than write a proper piece of code that uses a SQL statement to just get the data that is needed and then put it into an ARRAY. yes, an ARRAY. that thing we've had since the 1970s, that is lovely and does just what we need it to do.
I digress.
So, Kaluma. Great idea, much appreciation out to the creator. respect.
I then hooked up the K9, figured out that it had CircuitPython on it, and then extracted the "test code" that I had on it - it was not genius. It was super basic. I seem to have "lost" the motor control code that I was using for controlling the motors for FWD/RWD and LEFT/RIGHT - but as it's just GPIO pins, it'll be easy-peasy to just output what is needed.
As you can see, simple. I have an HC-SR04 ultrasonic sensor strapped to the front of the K9 to detect objects in front of it, I just wanted to then write super simple code that then powered the K9 forward and then calculated how much to turn left or right to avoid objects and see how long it was before it drove off the top of the stairs to a clunky / smashy death at the bottom. Well, hopefully that won't happen, but you know & I know, it will.
So, back to Kaluma.
I downloaded the .uf2 file from HERE:
Did the old [bootselector] button press and drag and dropped it over and kaboom. That bit was done.
Right, how do I morph the code from above to now do what I need in JavaScript? Well, first things first, we need to "HELLO WORLD". it's the law.
It smoke tests the environment / setup and makes sure everything is tickety-boo.
a quick,
$ mkdir kaluma
$ nano index.js
setInterval(function () {
console.log("hello from pico, every 1 sec!");
}, 1000);
Then it was time to upload it. You DID install the CLI, didn't you? Go HERE.
and as it says, of course you need NodeJS - as I said earlier, it is needed nowdays, go get it.Now you can push that index.js file onto the Pico to execute.
$ kaluma flash index.js
connected to /dev/ttyACM0
flashing .
[85.00 Bytes] flashed
NOTE: okay, a little bit of an assumption here, you may or may not appreciate this to start off with, the code above is set in a setInterval() function, what does that mean? It means that it will loop that function every X milliseconds (the value you put at the end), it also means that once you have flashed it and it has uploaded it will execute straight away and execute every X miiliseconds, in our case above, every second.
Why is this important - well, we'll get to that in the example in a minute and you'll see you can have finer control, if you want, but will also stop you from scratching your head wondering why the code is not running.
Now, to "see" the console.log, you need to have installed the CLI as mentioned above, in linux we'll just use screen to take a look:
$ sudo screen /dev/ttyACM0 115200
there we go. we know it's working as it should. btw - it is ctrl+a, k, y to get out of screen! You need to exit from it everytime you flash upload otherwise the port is locked.
Right, so now onto the good stuff....
I was about to write my own code to do the code that was hidden behind the scenes in the "sonar" Python library, when I thought, there's bound to be a nodejs package for Kaluma that does this.
Yep, here's the page that lists the current packages:
and here is the one we want:
https://github.com/niklauslee/hc-sr04https://github.com/niklauslee/hc-sr04
Right, let's setup a new folder for this:
$ mkdir hcsr04
$ cd hcsr04
$ npm init -y
This creates a package.json file - this will be familiar to nodeJS coders. you can see how this works now. Now type:
$ npm install https://github.com/niklauslee/hc-sr04
This downloads the node_module library into the current folder. Now we can create an index.js file, let's take a variant of the example:
const {HCSR04} = require('hc-sr04');
Now, uploading the code here is a little bit different. If you just do the same command as before. Nothing happens. You scratch you head for a bit, you think you did something wrong, you re-upload the hello-world, that works, you come back, you then go into the $ screen and then you type $ .help you have a dig around and then you finally go back to the website where they explain you need to use the BUNDLE command to upload libraries, it's simple and combined into a single command:
$ kaluma flash ./index.js --bundle
bundle.js [2.11 KiB]
connected to /dev/ttyACM0
flashing ...
[2.11 KiB] flashed
and then you switch to $ screen .......... and oh, nothing. hmmm....just a flashing cursor.
press [Enter], type: $ .help
> .flash -s
>2158
Oh, so that kinda matches, you did upload everything okay, but why is it not running.
Remember what I said earlier, the code is NOT set to auto-run, you have to tell it to run!
> .load
I put my hand infront of the ultrasonic sensor and the value it shows it how far away it detected my hand from it. awesome!
Now... one thing that IS nice is that from that $ screen session you can actually type code directly, so you can TEST things too. The HC-SR04 sensors are Analog pins and the Motors are Digital Pins, so we'll use different commands in the code to interact with them, which I'll do tomorrow.
However, I can do a quick test to see if I can turn the Pico W onboard LED on and off (well, it's actually the Robo Pico blue LED as that overrides in my case), to do that...
> pinMode(0, OUTPUT);
> digitalWrite(0, HIGH);
turns on blue onboard LED
> digitalWrite(0, LOW);
turns off blue onboard LED
We can add that into the code now as we've tested that we have the right GPIO values. I'll probably do the same for the Motors as they are GPIO 10,11 & 8,9 - I've also noticed that on GPIO 4 I have a BIG blue LED connected too, as well as some other things connected inside the K9 too - again, it's all just GPIO INPUT or OUTPUT settings, so should be easy stuff to get this up & running tomorrow.
Yep, a quick test proved that lights up nice and BLUE!
Okay, so this is all lovely and dumb and stuff... but it's not really genius is it? agreed.
I have ordered a couple of these devices though. I have a week off in a few weeks time & guess what I'll be doing.
The device is REALLY small - and it packs a punch for having Machine Learning capabilities onboard - now the K9 will be able to not only "see", it will "detect" what the obstacle is and then determine the course of action it wants to take, ie. if it recognises the CAT, chase it... but also detect the stairs, to prevent a smashy messy death.
I purchased my 2 device from The PiHut - why? cos they are reliable and are really helpful if you every need assistance or need to do a return from any reason, used them for years. great people.
Why 2? you have to ask!?!
Now, when I say this is SMALL, I mean it. Packs one hell of a punch. I should be able to hook up the GPIOs between this & the Pico W so that I'll also be able to stream the video too... if I really want / need to.
Once I've proven out what I want to do with this setup.... I'll move onto the REAL ROBOT. hint hint nudge nudge wink wink, say no more.
Comments
Post a Comment