Page 1 of 2

GS450H new firmware with web interface

Posted: Wed Dec 08, 2021 11:15 pm
by catphish
I've been working on a new firmware for the GS450H v2 VCU. This is based on Damien's firmware but uses Johannes's standard openinverter ESP8266 web interface. I am calling this v8. The major changes are as follows:

* Remove existing serial and web interfaces, replace with standard openinverter command set and web interface
* Simplified throttle mapping with configurable limits and regen
* Low ratio selection using a 12v input
* Refactored core code to simplify timing and add comments

This currently comes with some major caveats:
* This is untested! Individual parts of the code (inverter control, web interface) have been tested individually, but because my GS450H gearbox has now been installed in an unfinished vehicle, I have not had an opportunity to test the whole thing together. I hope to have have it running in the vehicle in a week or two, so will update this thread at that point, but because of the delay, I thought I'd go ahead and publish the code anyway.
* There is no CAN support. It would be great to add mappable CAN messages in the future, but these were not a priority for me.

This is still a work in progress, but I'm hoping that this version, which I intend to use, may be useful to others and can continue to be developed.

Source code available at https://github.com/catphish/gs450h/

Parameters:
precharge_voltage - when the inverter reaches this voltage, there will be a delay of 500ms then the main contactor is closed, set to your minimum expected battery voltage

max_torque_fwd - torque at full throttle - max is 3500, start testing at around 500
max_torque_rev - torque at full throttle in reverse
pedal_min - set this just above the value your throttle reads when not pressed
pedal_max - set this to the value your throttle reads then fully pressed
regen_factor - how much regen to apply per 100rpm of engine speed at zero throttle, start at around 10
regen_limit - maximum regen torque - start at around 100
throttle_exp - set to 0 for a linear throttle response or 1 for a curve, start with 0
oil_pump_pwm - the pwm value for the oil pump, seems to max out around 150, start at around 90

The throttle mapping is such that full throttle is always max_torque_fwd, and min throttle is zero torque when stationary, and increasingly negative torque as the motor speeds up (based on regen_factor).

As noted above, this is a somewhat premature release, but I will update as soon as I've been able to do more testing.

Re: GS450H new firmware with web interface

Posted: Thu Dec 09, 2021 9:34 am
by johu
Nice job! Obviously I can't test it here but I'm sure it's a good step towards usability

Re: GS450H new firmware with web interface

Posted: Thu Dec 09, 2021 2:02 pm
by PatrcioEV-ATX
I've been looking at your code for some time and refining mine in response. Your code is soooooooooo clean! I've gone a slightly different direction and done away with the web interface all together and am sending CAN back and forth between the VCU and a Nextion display, but I do plan on using good portions of your code once my car is running (at the body shop STILL).

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 6:29 pm
by evMacGyver
Really nice, thank you for sharing! Exactly what I was looking for, new web interface and I like splitting code to separate files. Tested shortly with my spare gearbox "on the table", will test more later.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 8:17 pm
by catphish
evMacGyver wrote: Mon Jan 24, 2022 6:29 pm Really nice, thank you for sharing! Exactly what I was looking for, new web interface and I like splitting code to separate files. Tested shortly with my spare gearbox "on the table", will test more later.
Thank you. I am still waiting to have access to my test gearbox again so haven't done any more on it since my original post. It should all work, apart from shifting gear ratios, which needs more work :)

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:30 pm
by xp677
Is line 135 valid?

if((mg1_speed>MAX_SPEED)||(-mg1_speed>MAX_SPEED)) torque=0;

I didn't think - could be used in this way, shouldn't it read:

if((mg1_speed>MAX_SPEED)||(mg1_speed*-1>MAX_SPEED)) torque=0;

Happy to be wrong!


Edit: For line 180, this is configuring USART0 on the related pins, otherwise these pins will default to their standard functions (digital in, etc). Arduino IDE defines all boards in the same way, we are just overriding it here since the standard Arduino config doesn't provide a USART.

Line 186 is telling the inverter its max charge / discharge rates, essentially a power limit. Since nobody has ever changed this, this could just go into htm_data[] array to save cycle times.

On line 265 you added a delay() which wasn't needed before and will slow down code execution.

On line 279, be aware that the inverter can (rarely!) slip out of init state, AFAIK you have no way to detect this and resend a _setup packet. Can be simple else{} to set that bit back to 0. important to make sure this is only checked on valid packet, as it will in this case.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:36 pm
by catphish
xp677 wrote: Mon Jan 24, 2022 10:30 pm if((mg1_speed>MAX_SPEED)||(-mg1_speed>MAX_SPEED)) torque=0;

Code: Select all

charlie@charlie-ThinkPad-T14:~$ cat negative.c
#include "stdint.h"
#include "stdio.h"

int16_t mg1_speed = 10;
int main() {
  printf("%i\n", mg1_speed);
  printf("%i\n", -mg1_speed);
  printf("%i\n", -mg1_speed < -9);
  printf("%i\n", -mg1_speed < -11);
}
charlie@charlie-ThinkPad-T14:~$ gcc negative.c -o negative
charlie@charlie-ThinkPad-T14:~$ ./negative
10
-10
1
0

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:39 pm
by catphish
xp677 wrote: Mon Jan 24, 2022 10:30 pm Line 186 is telling the inverter its max charge / discharge rates, essentially a power limit. Since nobody has ever changed this, this could just go into htm_data[] array to save cycle times.
That's a good idea. I might declare htm_data empty, then write an inititializer method that populates it one time with everything needed, to make its contents more understandable, and save cycles later.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:43 pm
by xp677
Sorry just to add, I've edited my post above many more times recently.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:47 pm
by xp677
Might be wise to clear mth_data on line 263

for (int i = 0; i < 100; i++)mth_data = 0;

At the very least I'd clear mth_data[1] (cycle times and all that) such that a valid packet in a run state can set it back to 1.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:48 pm
by catphish
xp677 wrote: Mon Jan 24, 2022 10:43 pm Sorry just to add, I've edited my post above many more times recently.
Thank you very much for all your input, I will update the code and comments accordingly. :idea:

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 10:49 pm
by xp677
These are just my suggestions, it's been almost 3 years since I started with this code, I've forgotten a lot and likely you're more experienced with it than I am.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 11:00 pm
by catphish
xp677 wrote: Mon Jan 24, 2022 10:47 pm Might be wise to clear mth_data on line 263

for (int i = 0; i < 100; i++)mth_data = 0;

At the very least I'd clear mth_data[1] (cycle times and all that) such that a valid packet in a run state can set it back to 1.
On like 263, a full 100 bytes of mth_data are *always* read from the port and overwritten in the array, so there's no reason to zero it. The monitor_inverter function exits early if there isn't 100 bytes of data available. No processing is done unless 100 bytes is received and the checksum is good.

It definitely makes sense to handle the non-zero state of mth_data[1] though and re-initialize, I'll add that!

My code operates under the assumption that data will always be received from the inverter, and if it isn't, there's not much we can do in response except to keep sending the clock, and hope it wakes up again.

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 11:01 pm
by catphish
xp677 wrote: Mon Jan 24, 2022 10:49 pm These are just my suggestions, it's been almost 3 years since I started with this code, I've forgotten a lot and likely you're more experienced with it than I am.
Still very much appreciated, thank you! I'm just recycling your old code anyway!

Re: GS450H new firmware with web interface

Posted: Mon Jan 24, 2022 11:03 pm
by xp677
catphish wrote: Mon Jan 24, 2022 11:00 pm On like 263, a full 100 bytes of mth_data are *always* read from the port and overwritten in the array, so there's no reason to zero it. The monitor_inverter function exits early if there isn't 100 bytes of data available. No processing is done unless 100 bytes is received and the checksum is good.
OK. I'm not familiar with that function, it's the first time I've seen it.

Good that the code is being developed.

Re: GS450H new firmware with web interface

Posted: Sun Jun 26, 2022 6:07 am
by evMacGyver
I did my first drive with this firmware as a base with added BMW control. Noticeable thing with web interface seems to bee that when I refresh web page, there might be too long delay for gs450h control and it feels like inverter torque drops quickly and returns. Does web module ask every parameter separately using get_config? Poll_inverter is used every 2ms, could that effect if its delayed? If I recall correctly, I did see this behavior on table test without BMW control.

I might added too much regen_limit and regen_factor as it made similar aggressive things and could not use it, but that is something to investigate later.

Anyway new web interface is awesome!! ;)

Re: GS450H new firmware with web interface

Posted: Sun Jun 26, 2022 8:23 am
by catphish
evMacGyver wrote: Sun Jun 26, 2022 6:07 am Does web module ask every parameter separately using get_config?
The web interface calls "json" which runs this rather chunk of code: https://github.com/catphish/gs450h/blob ... #L93..L118

It's definitely plausible that the inverter times out while this chunk of data is being sent (due to the low speed of serial comms), I'm not sure what the timeout is. The 2ms timing isn't critical, but it will time out after a while if there hasn't been a cycle of poll_inverter, monitor_inverter, control_inverter.

If this is happening, the simple solution is to move this block of code to a function:

Code: Select all

  // Poll the motor at regular intervals to request status frames
  poll_inverter();

  // Wait for a status frame from the inverter
  if (monitor_inverter()) {
    // Every time we receive a status frame, calculate torque demand, and send a control frame.
    calculate_torque();
    choose_ratio(); // Note the ratio switching code may override calculated torque
    control_inverter();
  }
And then call this function from within send_json_param so that the inverter gets controlled between each parameter being sent.

Re: GS450H new firmware with web interface

Posted: Sun Jun 26, 2022 8:32 am
by catphish
evMacGyver wrote: Sun Jun 26, 2022 6:07 am I might added too much regen_limit and regen_factor as it made similar aggressive things and could not use it, but that is something to investigate later.
Make sure you have the bugfix here: https://github.com/catphish/gs450h/comm ... 0d324f9aa8

Without this, enabling regen causes the motor to to to 100% torque, extremely dangerous bug!

Re: GS450H new firmware with web interface

Posted: Wed Jun 29, 2022 5:14 pm
by evMacGyver
catphish wrote: Sun Jun 26, 2022 8:23 am It's definitely plausible that the inverter times out while this chunk of data is being sent (due to the low speed of serial comms), I'm not sure what the timeout is. The 2ms timing isn't critical, but it will time out after a while if there hasn't been a cycle of poll_inverter, monitor_inverter, control_inverter.
Web interface part did some bad for car CAN bus timing, I was debugging very weird problem for quite some time and as soon as I disabled Serial2 receive all was fine. I think I need to replace Metro timer to interrupt base for critical stuff, so that slow serial for web and terminal does not affect. I did not yet verify was my finding of gs450h control delay true or not.

Re: GS450H new firmware with web interface

Posted: Wed Jun 29, 2022 8:14 pm
by catphish
evMacGyver wrote: Wed Jun 29, 2022 5:14 pm I think I need to replace Metro timer to interrupt base for critical stuff
Moving the control code to an interrupt makes sense too :idea:

Re: GS450H new firmware with web interface

Posted: Sat Jul 09, 2022 3:09 pm
by evMacGyver
catphish wrote: Wed Jun 29, 2022 8:14 pm
evMacGyver wrote: Wed Jun 29, 2022 5:14 pm I think I need to replace Metro timer to interrupt base for critical stuff
Moving the control code to an interrupt makes sense too :idea:
Now I think its mandatory to make everything happy and also to be safe. I removed wifi serial2 receive, but I still did have some USBserial debug prints, at one point it got too long and bad things happened to inverter control. Removed debug prints and everything was fine. Of course I could add inverter control between serial transmits as you suggested, but it would be uncertain when timeout will happen.

To get inverter control interrupt based, REQ is toggled every 2 ms, so that will make 4 ms signal of 250Hz? And as we are waiting for 100 bytes of return data, it should happen every 100 bytes*4 ms. After receiving 100 bytes we will send back 80 bytes of data. Is this even nearly correct?

Re: GS450H new firmware with web interface

Posted: Sat Jul 09, 2022 3:29 pm
by catphish
evMacGyver wrote: Sat Jul 09, 2022 3:09 pm To get inverter control interrupt based, REQ is toggled every 2 ms, so that will make 4 ms signal of 250Hz? And as we are waiting for 100 bytes of return data, it should happen every 100 bytes*4 ms. After receiving 100 bytes we will send back 80 bytes of data. Is this even nearly correct?
To clarify on the timing, REQ is indeed toggled at 250Hz, but req triggers a full status message, not just a single byte. We are expecting 100 bytes of data every 4ms. And yes, each time we receive 100 bytes of data, we respond with 80 bytes.

The inverter does not require this specific timing, I don't actually know what the maximum intervals for the req and the 80 byte control messages. I just use this timing it because it's extremely convenient to make everything event (or interrupt) driven if you do this.

If you want, you can actually make the code work with a single interrupt, because each stage drives the next...
1) Generate a 250Hz square wave output (req). This can be done with a hardware timer, no code or interrupt needed.
2) Interrupt when data arrives from the inverter. You don't need to care about the timing of this, just deal with it when it happens.
3) Within that interrupt, if 100 bytes of data have arrived, process it and send the 80 byte control data in response.

All other code like USB and WiFi can happen outside the interrupt, safe in the knowledge that they won't get in the way.

Re: GS450H new firmware with web interface

Posted: Mon Sep 19, 2022 12:45 pm
by evMacGyver
Does anybody have idea why web interface graph only shows maximum on 8 bit value? If eg. I select mg speed and start accelerating, graph will rise to 255rpm and start again from 0rpm, showing saw tooth. Just wondering if its VCU or wifi module code?

For interrupt base code, I did try it on summer and it was quite simple mod after all. But I got very weird behavior in vehicle side, so I did leave investigation of that later, summer is too short.

Re: GS450H new firmware with web interface

Posted: Mon Sep 19, 2022 2:50 pm
by catphish
evMacGyver wrote: Mon Sep 19, 2022 12:45 pm Does anybody have idea why web interface graph only shows maximum on 8 bit value? If eg. I select mg speed and start accelerating, graph will rise to 255rpm and start again from 0rpm, showing saw tooth. Just wondering if its VCU or wifi module code?

For interrupt base code, I did try it on summer and it was quite simple mod after all. But I got very weird behavior in vehicle side, so I did leave investigation of that later, summer is too short.
Thanks for testing! Just had a look, this is quite an obvious / silly bug. The function to fetch a value for the web interface would only ever return an 8 bit value. Not sure how I missed that in my own testing! :)

Try this: https://github.com/catphish/gs450h/comm ... ca9bfb6944

Re: GS450H new firmware with web interface

Posted: Mon Sep 19, 2022 4:55 pm
by evMacGyver
catphish wrote: Mon Sep 19, 2022 2:50 pm Thanks for testing! Just had a look, this is quite an obvious / silly bug. The function to fetch a value for the web interface would only ever return an 8 bit value. Not sure how I missed that in my own testing! :)
Oh it was that obvious and I did not see that, I was just thinking it must be very complicated bug :) Thanks!