.NET implementation
Forums:
Apparently I'm a sucker for punishment so I'm trying to port some of your software over to .NET IoT with C#. My issue is that the library I'm using - UnoSquare's rather good interface - has provided me only so much. I can get your wheels turning, and I can get the speeds... But that's it. The reason appears to be because the .Read(int length) function is returning only an array of one byte that is [length] long.
I figure it has to do with how the library is reading the stream. But my question to you is: has anyone here, or that you've heard of, tried to do this yet? If so, what library(ies) did they use to interact with the I2C bus on the board?
I'm so close it's irritating.
Thanks.
-J
piborg
Wed, 03/17/2021 - 10:05
Permalink
Reading multiple bytes from the I2C
This sounds like what I would expect, the library reads back as many individual bytes as it was asked for. This is because the data travelling across the I2C bus is just raw bytes, the library has no way of knowing what they mean.
If you are trying to read a value which is stored in multiple bytes (such as
GetBatteryReading
), you will need to re-combine the values into a single larger value once they have been read. Some values (like the battery voltage) also need a bit of decoding to get the values into standard units.Here is an example for
GetBatteryReading
that should do the trick:crazycga
Thu, 03/18/2021 - 02:08
Permalink
So, this was awesome, thank
So, this was awesome, thank you for the response! It doesn't, however, actually solve my problem. It's probably the library that I'm using, because here's what's going on:
And the problem is, it produces this:
The specific library that I'm using is this one:
https://github.com/unosquare/raspberryio
Frustrating as heck, because according to your Python code (assuming you were the one who wrote it) a hit on 0x99 should produce a response of:
0x99/0x15/0x00/0x00/0x00/0x00
But as you can see, it just responds 0x99/0x99/0x99/0x99/0x99/0x99.
I'm still too new to this part of things that I don't know if it's ME, or if it's the library I'm using.
But either way, any help is IMMENSELY appreciated.
Thanks.
-J
piborg
Thu, 03/18/2021 - 14:52
Permalink
I2C reading limitiation
I think you are right, it looks like the library is reading one byte per transaction instead of reading many bytes as a single transaction :(
We have only tried .NET on the Raspberry Pi when using Windows IoT, which has the
Windows.Devices.I2c
library available. That library reads all the bytes requested in a single transaction .Looking at the https://github.com/unosquare/raspberryio library I think it may be a limitation of how the underlying WiringPi code it is built on works.
I had a quick look around and this library looks quite promising: https://github.com/mshmelev/RPi.I2C.Net
It implements its own native library for talking to the I2C driver which looks like it behaves the same way as our Python code :)
crazycga
Thu, 03/18/2021 - 15:50
Permalink
Oh, you ROCK! I'll look at
Oh, you ROCK! I'll look at this after my work day. Cheers, mate!
crazycga
Fri, 03/19/2021 - 02:32
Permalink
The latest
Hey. I don't... actually know what to call you other than @piborg.
So I checked out that repo at the link. It's older than dirt, but it "sort of" works. I've got some really, really weird crap going on with it that I can't yet explain, but overall, we've got FORWARD MOTION! LOL And we can stop! LOLer.
I'm getting some strange things happening, though, that I've had to confirm through your Python code. Namely, when setting 0x08 to 0x80, my A side wheels are going forward. Great. The next step - even if I send an all-stop first - is to send 0x11 a 0x80... and BOTH my A and B wheels are now going forward. I don't understand this yet.
Seen it before?
*IF* you're interested (or anyone else here) I'll throw my code up on GitHub along with a fork of the original code.
Let me know.
Thanks!
-J
piborg
Fri, 03/19/2021 - 10:35
Permalink
Simple misunderstanding :)
This is actually just a simple mix-up between decimal values (base 10) and hexadecimal values (base 16).
The commands involved here are:
For the first command both 8 and 0x08 are the same value - COMMAND_SET_A_FWD.
For the second command 11 and 0x11 are different values - 11 is COMMAND_SET_B_FWD, 0x11 is COMMAND_SET_ALL_FWD.
Any number starting "0x" is interpreted as hexadecimal (uses digits 0-F) instead of decimal. You can mix and match between the two, just make sure each one has the right value :)
Most calculator programs have some tool to convert between decimal and hexadecimal. There are also online tools such as: https://www.rapidtables.com/convert/number/decimal-to-hex.html
Sharing the code would be great, I am sure it would be helpful for others who want to use .NET as well.
My name is Arron :)
crazycga
Fri, 03/19/2021 - 13:40
Permalink
Oh dear Lord...
...was I REALLY that tired?
Dang it, I thought I checked that. Twice. Ugh. I'll try that, thank you for putting up with me, Arron. I'm James.
And I'm good at this, I swear to God....
I mean, I do this as a DAY JOB... *smdh*
-J
crazycga
Fri, 03/19/2021 - 21:41
Permalink
C# repo
Hey:
My repo is here, but it's Mono based, not Dotnet, just yet. I'm building this out and hoping to migrate it over to .Net Core soon.
https://github.com/crazycga/RPi.I2C.Net
It'll have a different name. But so far, I have code that runs motors, gets values, etc. I'm having some problems with setting LED values, but I'm working on them.
Thanks!
-J
crazycga
Sat, 03/20/2021 - 04:09
Permalink
And soon to come...
...I will have a native .Net Core 3.1 implementation. Stay tuned!
piborg
Sat, 03/20/2021 - 16:41
Permalink
Looking good :)
Looks like your library is coming along nicely :)
The LED colour control should be fairly simple, but as a summary:
crazycga
Sat, 03/20/2021 - 17:40
Permalink
Thanks!
Thanks, man. That's what I'm trying. I've validated that your Python script to wave the LEDs is working. But man, I'm getting some weird results here. I'll try putting in a delay, but what's happening that I just observed is that using a loop to go through colors 10 at a time, the LEDs are flashing... But only intermittently, and only when the LED monitoring is ON!!! Which has me puzzled. When I turn it off - and CONFIRM IT - it simply doesn't want to play fair with me.
I'll try the delay thing and get back to you.
crazycga
Sat, 03/20/2021 - 19:38
Permalink
Do you mind taking a look for me?
Hey Arron:
I hate to be a PITA, but I can't figure out WTF is going on... This code is the exact same code used to pass values to the wheels, etc., but for whatever reason it is NOT working for the LEDs. Would you mind taking a quick look and see if I've missed something obvious, again? (note that I've collapsed some of the code to make it look right here, with the _ tag.)
And the code for ThunderBorg.SetLEDs:
The value for COMMAND_SET_LEDS:
The rest of the code is in my repo at https://github.com/crazycga/ThunderSharp
What I'm observing happen: the LED value seem to go though being SET, but does not appear to "TAKE":
I kind of hate asking, but I'm running out of inspiration.
Thanks, man!
-J
piborg
Sun, 03/21/2021 - 17:54
Permalink
Might be an interop problem
This is a bit of a guess, but this might be related to the passing of arrays between .NET and C.
You can get .NET to "convert" the array to and from a C style array for you using the
[MarshalAs(UnmanagedType.LPArray)]
attribute.For example:
You may need this on both the read and write functions, I don't remember for sure.
There is an explanation the attribute here:
https://docs.microsoft.com/en-us/dotnet/standard/native-interop/type-mar...
crazycga
Sun, 03/21/2021 - 20:27
Permalink
I just tried it...
...and that didn't seem to make a difference, BUT, you've sent skittering off on a different tangent. I observed that the underlying code (C) for this library is accepting
and perhaps when it take that pointer and works on it, it might be losing something? I'm reaching, too, but I might just give it a try and see.
You da man, though! I can't express enough how much I appreciate your input, sir.
Thanks.
-J
crazycga
Sun, 03/21/2021 - 21:15
Permalink
GOT IT!
I'll be go to hell, but I GOT IT!
You were absolutely (and accidentally) correct!
The I2C library that I farmed was a skootch too fast. It was written as:
So I changed it (because I wanted to enumerate the number of bytes actually written) and expanded it into an if-then clause:
And I'll be damned but it started actually working! So you were right the first time, the code was moving too fast for the I2C response. But it was WAY deeper than in my C# code, it was way down in the C library I used. (LOL Told you I knew what I was doing. HAHAHAHA) I'll be updating my repo, testing it, then calling this the first ThunderSharp release. I haven't implemented all of the functions you have in the ThunderBorg.py library yet.
Which leads me to a question. Why - of all things - does that library contain output for the LED strip? (SK9822 / APA102C)
Thank you, thank you, thank you for your help, Arron! You da MAN!
-J
piborg
Tue, 03/23/2021 - 12:31
Permalink
Great stuff
Glad to hear that you managed to find the problem :)
The reason the delay is needed is that the ThunderBorg's PIC takes a short period of time to decode the command and load a response buffer with the reply. Without the delay the response buffer is read from before it is ready.
As for the APA102C code (COMMAND_WRITE_EXTERNAL_LED) it was added towards the end of development so we could add some RasPiO InsPiRing strips to a MonsterBorg for a bit of fun (see here for connections). Originally it was only intended for our internal use, but we left it in so others could use it if they wanted to. I would consider your library complete without it being implemented.
crazycga
Tue, 03/23/2021 - 13:41
Permalink
Cool beans!
Thanks man. I've got ... two? more functions to implement and I should be "done". I haven't implemented the change address function (scared to, LOL) and ... okay, maybe one. I have to see. Unfortunately my day job interferes with my hobby.... Then I'll clean up the code, make the error handling consistent and then release.
Again, many thanks for the help!
-J