Espruino in power converter applications

Posted on
  • My interest in Espruino started as I was looking for an alternative to NETMF for programming STM32F4 micro controllers. I am a switch mode power converter guy and micro controllers are used in high power converters. I am not myself a programmer but I am convinced that by using a high level language the task of writing code for prototyping purpose will be much faster. I can do some basic programming.
    A year ago I developed test equipment for my own use for solar powered applications using VB. net and NETMF on an STM32F4 Discovery board and was pleased to see how quickly I could write the code and how compact and manageable it was compared to the C language. But the development of NET Micro Framework seems to have stagnated, so I am not sure this is the right path to follow. Right now I am looking at Espruino as an alternative. I am working with solar powered MPPT chargers and other power converters.

  • Sounds like it would work quite well... From my understanding you'd just be modifying a PWM waveform depending on things like analog values? If the algorithm was simple enough you could be getting around a 1ms response time - it could be a bit less if you had more code though...

  • One of the functions that I need for my project is to capture two analog signals and time stamp, and at least 70 samples. I tried to use a Beaglebone Black and wrote a simple Python program. I then tried STM32F4 Discovery and wrote the same function using Espruino. I was pleasantly surprised to see that the sample rate was almost five times higher with this approach. It is almost fast enough for what I am doing, but it would be nice to get a factor of 10 improvement in sampling rate. Right now the measurement takes 35ms to complete, that is reading two analog signals and the time 70 times and saving it in an array for transfer to a PC later. If anyone has insight into how I can speed up my measurement, I am very interested to hear more. My little program is:

    function capture() {
      digitalWrite(C0,0);
      setTimeout(read(),2000);
      send();
    }
    function read() {
      tick1=getTime();
      for (var i=0; i<samples*3; i=i+3) {
        data[i]=(getTime()-tick1);
        data[i+1]=(analogRead(C1));
        data[i+2]=(analogRead(C5));
      }
    }
    function send() {
      for (var k=0; k<samples*3; k=k+3) {
        print (data[k],data[k+1],data[k+2]);
      }
    }
    var samples=70;
    var data=new Float32Array(samples*3);
    capture();
    
  • That's great! Some quick ideas:

    • Try using a 'normal' array and use data.push(...) to append, rather than referencing individual elements (this may be faster but I'm not 100% sure)
    • Define data locally as a single character variable name (d)
    • Count down towards zero (which will marginally speed up the loop)
    • Don't do the subtraction of time in the main loop

    Something like this might be faster:

    function read() {
      var t=getTime();
      var d = [];
      for (var i=samples; i>0;i--) d.push([getTime(),analogRead(C1),analogR­ead(C5)]);
      for (i in d) d[i][0]-=t;
      return d;
    }
    ...
    var data = capture();
    

    It's all a bit nasty though. I'm actually working on a Waveform class which will allow you to capture and play back analog values. Initial tests look like it'll be able to do it at ~100kHz.

  • I modified my code according to your example and the time required to capture 2*70 readings with time stamp dropped from 35ms to 21ms. This is a significant improvement, but I am still interested in reducing the time required for this measurement. The function that I am working on is a simple maximum power point tracking for a solar panel powered charger where partial shade occurs frequently and rapidly. The faster I can capture the data, the more accurately the charger will track the maximum power point. This allows the charger to harvest more of the available energy from the panel.

    function read() {
      var t=getTime();
      var d = [];
      for (var i=samples; i>0; i--) d.push([getTime(),analogRead(C1),analogR­ead(C5)]);
      for (i in d) d[i][0]-=t;
      return d;
    }
    function send() {
      for (var k=0; k<samples; k++) {
        print (data[k]);
      }
    }
    var samples=70;
    var data= read();
    send();
    
  • I've just made a new version of Espruino - 1v53. See http://www.espruino.com/Waveform - it might help you capture your readings a lot more quickly (although only in 8 bits)

  • Thank you! Very interesting, I have to try it out.
    In my case I really need the 12 bits for measurement accuracy, and I need to capture two signals practically at the same time as they are related. Maybe I could multiplex the input so that I capture the two signals on the same pin but the 8 bits resolution is a bit harder to work around. I have to try it out.

  • Update: My project is moving along. I am using Espruino boards for my test setup. One project is now at the stage where I am building circuit boards that contains a power converter that handles input and output voltages up to 80V. the power stage is able to control power flow in both directions and can be paralleled for high power applications.
    the circuit will interface to an external micro controller, and that is where I am using Espruino boards. more details to follow.

  • Great - thanks for keeping us updated. Can't wait for the 'more details' :)

    If you're making a PCB then you can always use the Espruino Pico Part Outline for Eagle to mount the Pico straight on when it comes out! :)

  • a circuit board will certainly help if I build more than a few of these gadgets, I spent long evenings with this home project, soldering all the wires. the control box contains the Espruino board in addition to the status LEDs, rotary encoder, some buttons and a 4x20 serial display. the box is used in bench testing and for showing the different functions of a power circuit that sits on a 6x6 inch demo board (not shown).

    Espruino allows me to easily interact with the system while the power converter is running. I can adjust set points and functions using the box, or connect over Bluetooth to monitor a a number of analog signals and take control during test sequences controlled by my PC.
    I use the SD card to store setups and probably will add a way to upgrade and modify the software over email or internet, so that customers can evaluate the power converter after customizing it for their special requirements.


    1 Attachment

    • controlbox.jpg.jpg
  • Wow, that's great... This definitely looks like one of the more polished Espruino projects that I've seen! Is the box something 'off the shelf'?

    I don't know if you're interested, but the LCD display has 8 user-definable characters. It'd mean that you could have some kind of bar graph/history display if that were useful. It should be relatively easy to set them up as two rows of 4, and to then use the Graphics class to get a little 20x16 pixel area where you could display whatever you wanted.

  • Wow! Nice job @tage! That looks awesome :)

  • the enclosure is a PPLX box from http://www.pactecenclosures.com. somebody at work was giving away unused boxes so I got a few for free. the NHD‐0420D3Z‐NSW‐BBW‐V3 display fits after I milled away part of the standoffs that hold the screws for the lid, I just used a drillpress with a router bit. when cutting the opening I make it only large enough that the display can be inserted with some force, then it requires no mounting screws.
    The idea with bar graphs is definitely interesting. I am measuring four analog signals (V and A on input and output) and also two temperature signals and for diagnostics half a dozen other signals.
    I am having trouble with the HC-05 bluetooth connection, it does not seem to be reliable enough for downloading or reflashing. perhaps it is a noise related issue, but I have tried many different bluetooth dongles on my pc and come to the conclusion that bluetooth drivers on Windows 7 64 bit is a mess. this is the one problem I must solve before the control box can be used as intended. it is out of the question to connect a USB cable to the Espruino. I really need the bluetooth connection to work. or perhaps I should go for wifi. I would like to include a relatively large operators manual on the SD card and also make it possible to upgrade the data on the card.

  • I guess if you had WiFi you could have the ability to update the card's data and serve up the manual.

    Bluetooth should be quite reliable though - what kind of errors do you get? Have you tried turning on throttling in the Web IDE?

  • I tried enabling and disabling throttling in the IDE settings and the conclusion is that the problem only appears when I do not have throttling enabled. I will keep using the bluetooth connection for a while with throttling enabled and see how it goes.

    the problem was that I received garbage when downloading code into the espruino, or I got only part of the code. sometimes garbage would be printed on the computer screen (left window in the IDE) while I was downloading.
    sometimes I would not be able to communicate using bluetooth at all, even if it connected. finally the espruino was so messed up it did not respond at all. I had to reflash with the latest espruino version (I already had the latest version, but this was the only way to restore contact, using bluetooth or USB cable).
    after reflashing over USB, unplugging and then powering from external 5V it as possible to again communicate, also with bluethooth connection. now the IDE will stay throttled until I find out if this is the solution. the thing is that I just replaced the bluetooth dongle (again) it could be part of the solution. I have to try other dongles when I get time.
    thanks for the help!

  • That's a strange one... could it be some kind of grounding problem? Perhaps if transmit and receive happen at the same time, data gets lost/garbled?

  • update:
    I have kept the web IDE throttled and only once have I experienced the problem since then. the problem appeared when I was downloading code to the Espruino. garbled text appeared in the left pane of the IDE. I repeated the download without any more problems.

    I am wondering if perhaps my wireless mouse that was only inches away from the HC-05 may have been part of the problem. the desktop computer with the bluetooth dongle is about 1m away from the HC-05.

  • Hmm, I'm not sure. I imagine bluetooth does a lot of error correction with the data that gets sent, so it almost seems as if the garbled characters are getting in there because of the physical connection to the bluetooth module.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino in power converter applications

Posted by Avatar for tage @tage

Actions