RS485 Hub

XPortHub is great if you need a combination of ports, but I often need a high density of one port type so I made RS485Hub with 6 x RS485 ports, 2 x CAN ports and 1 x USB.

This is basically a simple Hat where I take advantage of the high number of RS485 ports on F405. The 2xCAN is added because they are available.

AL – Abstraction Layer

The main objective with an Abstraction Layer is to protect user modules from changes between platforms. Code on user level should be portable and plug & play after you done a 10 minute task of soft-wiring. I decided on the name “Abtraction Layer” because HAL (Hardware Abstraction Layer) is missued to much and was not a good fit.

The figure above illustrate what I want to achieve. The interface on AL should be identical, while we know that each AL module will need to be specially adapted to the technology in usage. In this case I will point out that easyIPC’s version of SPI is not a generic SPI interface. The easyIPC version is a TDM protocol that needs to be tightly integrated with hardware.

Free Electricity – Windmill

I decided to try out a windmill costing ca 180.- USD that will arrive any day soon. It is quite windy here I live, so I should get a decent 500-1000W out of this and with a decent battery bank I should be able to drive a lot of out-door electricity – not connected to the grid. This is actually part of a larger project because I want to monitor the effect I get from this small windmill. It is 130cm in diameter, and I can hide quite a few of these in my garden if they give the expected effect.

We are offered contracts in this area meaning that you can sell electricity back to the power company. In my case I would save 120 ore (ca 0,20 USD) per KWH – so assuming this windmill generate between 500-1000W in average that is 8760 KWH per year. The actual rating is 1500W, but I hope for 500W in average which is 4380 KWH/year – so I could save from 500.- to 1000.- USD a year from an investment costing me 180.- USD. My yearly consumption is ca 20KWH, so that is 25% of my electricity bill. Basically with a few of these I could be selling electricity to the power company – what is not to like?

It is two numbers that I need – one is the average effect I get out of this windmill and two is the noise-levels. The windmill is stated to be noiseless, so lets see – or more correctly hear! The later is important because I can get away with a noiseless windmill, but if this generate noise the project is dead. To put up several of these I will need to get permission.

Yet Another 3D Printer

I feel bad about my 300.- USD 3D Printer collecting dust in my lab, so I want to do something about it. This was a Prusa i3 Gen 1 clone and it ha loads of issues that came with Gen 1 printers – that said, it actually work and do a decent job for what it is.

The control system is Atmega2560 based (Arduino) and does it’s job. The advantage of this is the wealth of open source alternatives running G-Code. The drawback is that the control system is “all in one board”, meaning it is hard to expand and experiment with, so I want to replace this with a modular and more capable one.

The block diagram above illustrate a possible system. The core here is that I use my RPI modules to create a modular control system.

Current slicer SW mostly operate through Serial USB and XPortHub is excellent as G-Code Server – it has USB, Flash/SD-Card, HMI port and can drive the rest of the CNC machinery.

I could have done G-Code from raspberry PI as well, but suggest that RPI is optional for Wifi/Ethernet and print spooling.

To control stepper motors I use 2 of my 12xPWM cards that can drive 4 steppers each. The 2nd card drives 2 steppers and PWM channels for heat. Each channel support 2A, but keep in mind that I can combine channels and add a 3rd board if I need to.

The last board is a 32xIO Hat to connect to analogue sensors and digital sensors.

But, what do I want to achieve? I am obviously interested in building this as a test-case for my control system, but I also have some objectives.

  • Rebuild the mechanics into a more robust printer.
  • Insert mechanical end-stops and use current sensors to detect them to avoid a lot of wires and failing detectors.
  • Add Z-sensor to automatically detect plate tilting.
  • Add a 2nd extruder for support material.



3KW 3-Phase Motor Controller Rev 1.2

It’s been a while since I designed this 3KW 3-Phase Motor Controller without doing anything with it so I decided to give it an upgrade. The size of this is 100x25mm. It is very smal for it’s capacity and Perfect for larger drones.

  • Removed 3rd current sensor.
  • Remove the output holes at right and take the output directly from between the MOSFET’s.
  • Added 2 temperature sensor.
  • Modify power input so one wire comes at top and one at bottom. This enables me to easier mount the capacitor directly on the wires.
  • Strengthen power paths to support 50A++. The MOSFET is capable of 160A and 400A phase currents, so it is all about heat dissipation and current paths.
  • Removed some electronic filters replacing them with SW.

This design is very different to the 4 channel version due to it’s DRV8301 that contains DC/DC, Gate Drivers and current amplifiers. From a functional perspective it is less capable, but it is also much smaller size.

It is also perfect for running the outrunner above. I really liked this motor (power, size, cost), so I might buy a few more to create a drone.

Raster Graphics Integer Errors

A common problem with plotting lines in raster graphics is visualization errors as we display the graphics. Most times we can just ignore these, but as I am drawing scrolling real-time plots I also receive the side effect of an integer error.

This show a line that due to integer rounding is a straight line in the left plot, cover two pixel rows in the second before it is straight again in the third. As you move this over the screen it don’t look good or smooth at all.

The solution is quite simple – the error comes because you calculate x/y from double or float. Depending on position the conversion to pixel xy end up different as a line moves – so you can pre-compute a x and scroll this by adding/subtracting offset. The concept is that integer errors stay constant and your line looks like is moves more smoothly – no jumping between positions.

C++ vs C# Performance – Part 1

I have so far never seen a performance test where C/C++ did not win an easy victory due to it’s nature, but I am very curious about what I see in front of me now. The failure I pointed out earlier is how .NET or Windows behave then you push it to it’s limits, but the plot above is still 10 x Oscilloscope level plots at 27 FPS on a 1980xx1080 screen. That is Heavy raster Graphics behaving with video quality update – it is amazing to watch and open some usage opportunities that interest me.

Most developers will agree that raster graphics like .NET Forms is not the way to develop heavy graphics applications these days, but it still have some good usage schenarios and I like to test boundaries for my own education. In this case I will spend a little time and do the exact same plot in Qt and C++/MFC to compare 1:1 + I have a few bits of code that I have written in C++ and converted to C# so they can be used for comparisons as well.

So will C/C++ win an easy performance victory yet again? I expect it will, but the answer is not given. C/C++ compiles into static code while languages like C# compiles into a virtual machine’s assembly code and run on top of an interpreter called a “Virtual Machine”. Java uses Java VM, while C# compiles to a format called “MSIL” and run on .NET. What you need to be aware of is that computers are not single cores anymore and these interpreters are getting smarter using various technologies. The VM’s might also have a better C/C++ library and better usage of multiple cores out of the Box and the interpreters on some of these VM’s do a JIT to native machine code.

I will state that you can always win a performance game using C/C++ if you are willing to spend the time, but the question is more and more what do you get straight out of the box? My expectations right now is that C# .NET 4.6.2 might very well outperform Qt and C++/MFC on 1:1 raster graphics, but it will still be far behind on algorithmic speed. In reality we will be comparing Qt, MFC’s and .NET’s implementations of C/C++ raster graphics libraries because guess what – .NET is still written in C/C++.

Stay tuned. 

C# HMI @ 64 FPS

I did a lot of work in C#/Forms a few years back and found it to be a bit to slow for actual usage. It was easy to get fancy UI graphics done, but you instantly started struggling with performance on the screen. I remember using ca 1 month to get the functionality done and several months to get the UI library to perform. Basically I gave up using C# on SCADA style Applications at the time for reasons I will demonstrate below. Friends of mine recently claimed that the x64 flag in C# was native compilation, so I just had to try it. I don’t know about native compiling, but the result is much, much better.

This little test consist of a small plot and a fast, real-time grid running (above) on 1920×1080 screens in full size. It is a brute force implementation where I redraw the entire screen as fast as I can and the result stare me in the face – 64 FPS (Frames Per Second) at 6% CPU load. I have tested on 3 full screens as well and the CPU load increase a bit, but I still have 64 FPS. Ok- this is not the most Advanced Graphics, but it is a very convincing proof of concept, so I decided to re-create a heavier test that failed before.

Testing with AnyCPU flag I notice that I achieve the same with 22% CPU load. I also tested with .NET 4.7.1 and performance dropped, so I am using .NET Framework 4.6.2. The .NET version I basically gave up was .NET 4.0, which is the first version that started introducing the changes that now have matured. I also do a programming trick involving updating the screen from timers.

The computer I have in front of me is an Intel Core i7-4710HQ CPU @ 2.50 Ghz. It is a 64 bit with 8 cores, an old Asus gaming computer that is ca 5 years old. The second computer I tested is a bit faster, but results was basically the same. And just to be clear – this is the CPU doing the work on classic Form programming, not the GPU. What I will do next is to invest in heavier HMI Graphics and test.

The next picture below show the heavier test. Here I have 3 plots and have populated them with random lines. This is sadly also where C#/.NET starts to fail.

The logic I use here is that I run 2 timers – one to feed data and one to send Invalidate() to the component. The three plots are updated in sync so they should have reached the same level on the x-axis, but as you can see they have not.

What happens is that as the plot get heavier the framework simply stop updating some of it’s components. In this case you can see that the top plot has fallen far behind, so have the next plot and it is only the bottom plot that get updated at this point. The CPU load is 20%ich.

So – a short question – how can you trust a framework with critical HMI if you can trust the screen being updated properly? The answer is simple – you can’t!

I admit that faster CPU’s, newer and better .NET Libraries and the x64 setting all make good performance improvements. But, at the end it only moves the threshold for this Critical error.

And yes – I know how to get around this – I can daisy-chain all components and force them to update.

3KW 3-Phase Outrunner

This pic is of a 6368-190KV while I just received a 6368-280KV – a 3KW Outrunner BLDC w/Hall Sensors that costed me 60.- USD all in all. This will be my 3-phase test motor on the 3KW Motor controllers thought while my controller is max 60V @ 50A these motors have a spec 36V @ 90A max. I will use it at 48V and max 30A. I am not able to test higher currents due to 2 reasons (1) rev 1.0 of the controller have a PCB error on heat dissipation, and (2) I will struggle with more than 30A from my Lab PSU’s – or well – maybe I can borrow equipment for testing later or switch to batteries on a real project.

This motor is very cool. 3KW is a very decent scooter and it has some weight to it. The challenge is however that it is an “outrunner”, meaning it rotate the motor housing. I need to fix the motor to a plate to be able to test it, but I will find a solution. The motor also have a nice hall sensor cable and some nice 3-phase connectors. 

I need to see if I can find the female version of those connectors because I really like that solution. The thickness of the cable is less than I expected for something rated to 90A, but lets see what happens as we start spinning it.

I still do not have a heavier stepper motor to test with yet. I have looked, but they cost to much so I will do without for now.

As for actual projects I am considering buying batteries and making my own electric bike/Scooter. 3KW is far to powerfully for a bike, but we can use it at 250-500W. 250W is restrictions for electric bikes in Norway. If I buy a registered Scooter and rebuild it to be electric it would be ok with 3KW. I probably can buy one cheaper than it will cost me to build one, but where is the fun in that?

Programmable DDS – Part 2


This illustrate the main functionality I want to achieve. The DDS can generate waveforms as well as more classic squares, triangles, sinus etc. I would like a signal that can be amplified to +/-20V and given a base offset anywhere within that range.

The blue line illustrate a more classic square signal starting at 0V, while the red line illustrate a programmed signal with any values between +/-20V.

I think +/-20V is a nice objective and I will settle for 100mA out. AD8008 offer me a max 12V signal, so if I use that I will have to settle for a 12V delta positioned between +/- PSU boundaries.

In fact – LM317/LM337 have +/-36V as a range, so I should be able to achieve a compromise if I can amplify the signal from the DDS to 12V Delta and adjust the base offset to -30 to +30 or something. I think this offset adjustment is straight forward, it is just that I have not done it before. I realize that I am a bit inconsistent as I draw +/-20V and talk about +/-10 to +/-30V. I will go for +/-30V if I can, but I will settle for +/-10V if I must. I will also go for 1A out if I can, but I will settle for 100mA out.

Selecting a DDS chip is something I will return to. I indicated AD9851, but it is a long range of chips with frequencies up to 1Ghz and various functions. Their pricing range from 5.- USD to 50.- USD, but these are key to generating functionality at decent frequencies.

Selecting amplifier is the same. AD8008 is a candidate, but I would like a chip with a larger voltage out and preferable digital gain and offset. The more I can get packed into chips the more realistic to get this all on one Hat.

Having this on one Hat is an objective, but I will settle with DDS on one and Amplifier on a second if I have to. In fact – I wonder if I could benefit from 3 Hat’s – One DDS with 3V out, One more capable low frequency amplifier and one high frequency amplifier. Having these as Hat’s have values of their own + the Hat system means I easily can add as many channels as I want on the actual function generator.

It is a lot of loose ends, so it will be some time before I can settle a design that I am happy with. I am also a bit on on the edge of my own skillset here so I have asked someone who consider this “babyfood” to assist me as we move forward.

One main concern is PCB layout. I think this needs to be my first project using 4 layer PCB.