BSA – Grid Alignement

The purpose of a design grid is to force symbols to align to a grid line at left, top, right and bottom so they are easy to position and size compared to eachother. This speeds up drawing/design as you use less time sizing and positioning symbols on the screen. The default design grid is 10 points which is fine for diagrams, but not for HMI.

The picture above show the issue with a grid of 10 pts – the space between fields become very large – for some apps this will be ok, but for many apps we want less space. I have two solutions to this (1) is to adjust the field margins on top/bottom – in the above example I have adjusted the top to demonstrate – If I do top and bottom on every component – basically add a standard Margin I can put them edge to edge and they will still look like separate fields. This is one of the usages for the property Margins that specify offset left, top, right and bottom. (2) I can switch to a smaller grid of 5 points. The easy way of doing this is by having two separate grid buttons in the menu – one for 5 and 10. Actual grid size can be set by user, but 5 for HMI and 10 for diagrams are good values. The same goes for default Margins (that can be overrided).

The same screen layout with 5 pixel grid can be seen below – we use far less space:

Using less space can also be done with margins. Doing diagram alignment on 5 pixels is however a bit more fiddly than with 10 pixels which is why I want both of them.

This last one uses Margins, but I do now get an issue with the height of my edit box compared to font I use. This was with 2 pixel margins top and bottom. The next picture is the same with 1 pixel margins just for demonstration. But, even this will cut some characters at bottom so with a fontsize of 12 I believe 20 pixel height is optional. If you really want to use margins like this you need to downsize font as well.

The good thing about these details is that an user should never be forced to think about them at all – it will be cases where you sit an fiddle with visual details, but the objective is to have an automated path that is straight forward and sufficient for 99% usage cases – simply yet another issue I don’t want to spend time on once BSA is up running. You can set grid sizes, turn off grid or set default margins to have your own automated path.

C# assign behavior

     double h = Height = newSize.Height;

Height is a property with set and get functions in C#, so what I expected was that set Height was called first – which it does and then that get Height was called secondly – which it does not.

In fact the assign operation will execute the following:

    double h;

    Height = newSize.Height;

    h = newSize.Height;

What I need is this;

   double h;

   Height = newSize.Height;

   h = Height;

The difference is that I coded height limitations into the set function and was not aware that this cascade statement skipped get.

BSA – Text Alignment

This show a classic label and an associated edit field. Notice that the text “label” and “Text” are not aligned. In my case I force the controls to align to the grid and in this case I get the side effect that these two texts do not align. One solution would be to switch off the grid, adjust the label and switch the grid back on – thay would work, but it would also require us to do this on every label and checkbox etc. So what I am about to do is to add a property called “Text Alignment” and make sure that these align to the same with the grid switched on. All Windows controls contains a property called “VerticalAlignment”, but it don’t work in a consistent way between controls.

This is one of the reasons I use a grid – to make it easy to align controls in size and position and it needs to work straight out of the box so we don’t use extra time on these details. My challenge is however that I will need to force  all Windows components I use. The picture below demonstrate why.

Notice that label and checkbox text are aligned on top, while edit field and combobox text are aligned center. This just look unprofessional and is a detail that will annoy everyone if it is not fixed. This is also a common issue in some GUI designers as it is not the first time I observe the need to sit and adjust this manually. My challenge is that I will need to fix this in my own HMI designer, but also in every target platform SDK.

The easy solution is to just offset label 3 points from top to compensate for the fact that grid space is 20 and font size is less. I would expect VerticalAlignment to do this, but the ressults using that property vary from control to control.

The adjusted result can be seen above – the actual top adjustment needs to be calculated based on font size used. In the final code generated we just add Y + YOffset and the result should be fine. This is basically an adjustment I have been doing manually in every GUI tool I have used – it is usually “solved” with a smalle grid, but using a 10 point grid means things align easy in position and in this case I automate the adjustment that I otherwise would be sitting doing manually.

BSA – Mini Text Editor

I just copied text from an earlier blog into my mini text editor for demonstration –  and this works out very nicely. The capability to edit text is usually a dinosourus in these apps, so I am very happy to see that this one actually works out well and are very functional. I need to code up tools to get rich text and images, but I could actually do with this alone. This one is a big win, so I am considering ditching the full view editor – not needed,

Engineering is a lot of try and fail, so it is very nice that some ideas actually work out as planned because I have plenty of those who don’t.

BSA – HMI Controls

Many of the HMI controls use actual Windows Controls as base. In the example above I draw a simulated design form as a symbol and use various build-in controls to get the result as real as possible. The challenge is that these controls are designed to be in control – not be controlled by a designer – so getting them styled correctly and to behave inside the designer actually is a bit of work.

At the end of the day I probably will need to create a SDK with my own controls for each platform I support. I will look more into that as I start generating source code from BSA.

Looking at the edit fields you also see that the corners are not drawn as a complete line – this is the control and a detail I seem to lack control over.

Another issue is the color of the check box – I can control text color, but not the check box itself etc. This is very classic Windows Build In Controls – that said – I did discover styling capabilities capable of changing more than I believed I could, so might be  afew tricks I need to learn – but, I still think a lot of custom controls will be needed to add features.

BSA – HMI Border Colors

Just to prove my point with borders being to dark I encreased them from 0x32,0x32,0x32 to 0x64 and it makes a big difference as you now actually can see the outline of the control. Keep in mind that these border lines are design help, not actual borders on the real Form. In design mode we are more interested in design parameters than exact look & feel – that said it is nice if it is close.

BSA – HMI Designer

The HMI Designer part is progressing, but I must admit that it is a large project and I don’t always have time to work on it. Finally started to wrap up the Property Editor and styling up HMI controls that still have light style. The example form above got a bit dark and close to the background so I need to visualize the borders a little more.

Upgraded the project to Visual Studio 2022 and intend to make a test port to Avalonia later. Avalonia is WPF for Windows, Linux and Mac with iOS and Android coming. This is open source with MIT license and contain a few components that interest me.

I have complained about QML making design over-complicated in the past, but I must admit WPF also has it’s tweaks. That said I have never regretted moving from QML to WPF – C# integration into Visual Studio makes it worth it and I am exited to learn about what Avalonia can do – later.

This first version will be able to design HMI with a datasource and read/write data from/to that, but it will not be able to do much intelligent processing before we add PLD and the rest. It will however generate code we can continue on. My first datasources will be Modbus, CANbus and some database – I will probably wrap up a SQLite module, and maybe MySQL – lets see.

I recently bought myself a 14″ Asus VivoBook – great laptop for the price. I only have the standard laptop mouse and a single screen on this – which is a reminder that I will need to adapt the user interface for a more – keyboard only – option later. The current interface is dpending on mouse and 3rd mouse wheel which is not that available on a laptop only. Myself I prefer a desktop with 2 screens as a minimum then I work, but the laptop enable me to work more from my living room.

Looking at the example above I typically have five events I need to process:

  • Starting the dialog
  • Updating the dialog  with data from a datasource
  • Three button events.

In reality there will be more, but lets keep it simple – the challenge with a HMI only designer is how do I process those events? I have two options (1) I can generate code where the user add the missing part, or (2) I can add in a minimum of UML State Diagrams, meaning that a HMI only design will have little purpose except for being a milestone. I need to think about this one a little. I do however have a secret agenda because I work on QML from time to time and Qt’s HMI design tend to crash + I want to autogenerate a lot of the code to speed development up. This is why Qt/QML is on my target list.

Don’t take me wrong on QML – WPF/QML is the only two technologies I have found worth working with. QML is very capable and some parts are better than WPF, but I stick to WPF because it is much faster and more productive to work with – that said – my next system will be made in BSA.

ESP-M1 Model Train Control System – Part 1

ESP8285 is a smaller version of ESP8266 and comes in three different beakout modules – ESP-M1, ESP-M2 and ESP-M3. The attractive part of this is the size of only 12.3 x 15mm. This being a Wifi module with several IO options it is ideal for my old Model Train Control System.

This picture is of a ESP-M1 which is the smallest module measuring only 12.3 x 15 mm. I selected the M1 because of it’s IPEX antenna and total cost with antenna is ca 3.- USD.

In the earlier prototypes I struggled with size as well as lack of positioning system. The four things I want are Wifi, H-Bridge, a positioning system and an expansion port. With some help of some friends that are big on modelling trains I actually got an idea on a positioning system that is low cost, small and most likely will work – I will return to that later.

ESP-M1 needs an external adapter to be programmed, but luckily those can be purchased in two versions – one that I can use for testing modules and another one I can use for programming the module while mounted on the final system. So, how to program this is already solved by the community. lets have a look at M1:

The content on a ESP-M1 is basically the same as the largest ESP8266 modules – ESP12 have more pins, but those extra pins are already used. The picture above do not show all the capabilities, but it gives an idea:

  • 1Mb Flash, 80Kb RAM
  • Tensilica L106, 80Mhz & 160Mhz, 32 bit MCU
  • Ultra small 12.3 x 15 mm
  • Wifi
  • Build in TCP/IP protocol stack
  • Uses ca 85mA with radio on.
  • Large range of IO – GPIO, ADC, HSPI, UART, I2C, I2S, IR, PWM
  • Support for 802.11 b / g / n / e / i
  • Support Station, SoftAP, SoftAP + STA mode
  • Support Wi-Fi Direct (P2P)
  • Support CCMP (CBC-MAC, counter mode), TKIP (MIC, RC4), WAPI (SMS4), WEP (RC4), CRC hardware acceleration
  • P2PDiscovery, P2P GO mode / GC P2P mode and Power Management
  • WPA / PA2 PSKAnd WPS
  • 802.11 iSafety features: pre-certification and TSN
  • 802.1h / RFC1042Frame encapsulation
  • Seamless roaming support
  • AT Support remote upgrade and cloud OTA upgrade
  • Support Smart Config function (including Android and iOS devices)

I have only used the Arduino toolchain yet, but I will set up the proper toolchain later and dig deeper into the modules capabilities.

The block diagram above illustrates the new system.

PSU My earlier design was limited to 16V and 500mA. I have this time changed a few components and can manage 36V @ 2A. Most modern H0 trains use around 300mA, but some older ones can be above that. The PSU is classic – a rectifier so we can take any polarity and even AC. a supercap to keep MCU alive and a Current Sensor to measure motor current/torque. Increasing the capacity means I also can use this as a small Wifi power switch or PWM driver.

MCU/Radio is the ESP-M1 module itself. This is Wifi, but it also have some extra features I want to dig into. I have only started playing with this.

Supercap I managed to find a smaller SuperCap that is 4.8 mm in diameter and with a current usage of ca 85mA that should be sufficient to keep the MCU alive 1-2 seconds, but I need to test this one.


New Motor Controller

This is a Dev kit from ST that is a complete Motor Controller in ESC format with 3 current sensors, 3 BEMF, 3 discrete Gate Drivers, Hall sensors, temperature sensors and 28V@120A Solution. The MOSFETS are rated at 120A with 480A pulse, but heat dissipation causes them to be limited to max 40A with hard fan cooling. The kit cost around 20.- USD so I bought 2 of them for testing.

Using ST’s Motor Profiler I got motor data that I imported into their motor workbench, generated a project for STM32CubeIDE, Compiled it and was running the large drone motor in an evening. The generated code include a serial protocol that match their Motor Pilot application and voila – the motor span up.

The Motor Pilot application is shown above running. In this case I have only connected the USB that act as a serial port and I have not switched on the driver power, so the motor driver report and undervoltage alarm as it should.

You receive 90% of the source code. The remaining 10% is in a library that ST preffer to avoid sending out since it contains patented algorithmes, but questioning ST I got an answer that they could send me the code if I signed a NDA with them – so all code is available free of charge.

What impressed me was that I had the large 1,2KW drone motor spinning and controlled without writing a single line of code and still had all required code I needed to work on it myself. I have friends that have tried and not been successfuly in using this kit, and I had problems myself as well. As mentioned before – you need to be willing to try & fail a bit with source code from ST as they have an improving, but far from perfect SW quality. ST is however improving a lot. I have had far less issues with STM32CubeIDE and drivers lately than earlier.

The autogenerated settings worked, but I did not like the ramp-up logic. I managed to tune it so the motor started ok, but I suspect they use a Trapzoidal algorithm to spin the motor up in speed and then take over with FOC as current readings are safe. As they switch the motor over compensated and span forth and back before speed regulation was correct. I managed to fix that without any coding by changing their PID regulator.

The MCU on the kit is STM32G431CB that runs at 170Mhz, 128Kb Flash and 32Kb SRAM. I only use ca 40Kb Flash in the code, so plenty left to play with. G431 have analogue components on-board that makes current sensing easy and fast. Programmable Op Amps allow you to adapt analogue signals to the motor you use etc.

I mentioned this was an ESC, but it also comes with 5Mbps CAN-FD Tranceiver and the total size of the kit is 17 x 41mm – half a finger in size. It has a ST-Link 2.1 attached that you can break off. ST-Link 2.1 have also comes with an UART crossed into the driver and connected to the USB, so by default ST-Link enable a UART as a Virtual Com port that is great for debugging. Another great feature is that I can connect and work on SW without powering the MOSFET drivers – using USB only.

And best of all – I have all schematics, so it is easy to adapt this kit to a more sensible form factor. The dev kit is to small for my usage – I want to change DC/DC to 60V so I can run with 48V + I also want to attach more heatsinks to get more currents out. The small PCB I got have 8 layers and is very dence. Small is not always good in this case.

The picture above show the Wotkbench that basically is a diagram over the SW logic. You click on different parts to get a screen allowing you to edit parameters and then press a button to generate a Cobe project with code.

All in all – if you wanna work on motor controllers this kit is a must in my opinion – it will get you started with full schematics and all source code in a matter of hours. Well done ST!