Definition of a brain twister.. And a test on live tuning

Started by Rx7man, May 19, 2015, 10:05:00 PM

Rx7man

I'm at a loss on how to make this work in C++... I'm not good enough with pointers, and I'm getting all the same problems I had when I had a 2D array...

So I'm making a library for it, and perhaps you can give me a hand.. I am attempting to code it line-for-line the same as the VB version posted above...

So how do I declare an array in the class that I can assign a size to in the constructor.. or do I set up a pointer in the class that I create in the constructor, and then set that pointer to the array?  either way I'm not having much luck.. so here's what I have



#include "Arduino.h"
#include "Map2d.h"

  byte* Map1d; //pointer to the 1dimensional map array
  byte Rows;
  byte Columns;
 
  Map::Map(byte Columns, byte Rows){
    this->Columns = Columns;
    this->Rows = Rows;
   
    byte TheRealMap[Columns*Rows-1]; //Create the array
   
    Map1d = &TheRealMap; //this doesn't work... what do I need to do?
   
   
  }

'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

It has been eon's since I've used pointers.

If map is going to be dual array, I think you need to declare it like, byte* Map1d[][];
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

The map will appear to be a 2d array, but behind-the-scenes it's a 1D array that is converted.. I actually found it easier to do that way.

I bet it's been longer since I've used them.. I took C++ programming 101 in about 1999 and haven't used it since.

I had a whole whack of ebooks (most of the For DuMMies books) but somehow the PDF's seem to all have gotten corrupted.. grrr.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

I think I'm getting it!!! woohoo!

I will have extensive testing to do yet though, I had to change my return arrays that were 2 dimensional to unidimensional, but that's not a big thing, I do think I kept the logic the same.


I was wondering about Progmem()... How does the access time compare to RAM? is it about the same?  I should have enough memory, but if I was short, the first logical stuff to go into program memory space would be large tables right?


Here's my code so far.. not the prettiest, but it compiles, which is a good start!  I do however have a major bug...
I declare my map, and set everything in it, including the row and column labels, I double check it in setup(), and my data is there, and good.. then I run through the interpolation, and all my data is gibberish and changed up.

I thought it was something with passing the arrays, so I rewrote the part that I was doubtful of, but no change in the behavior.. I've attached it as a zip... I've dome some commenting, and left all the debugging Serial.Prints so you can see what's happening
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

Good to know.. and I'm going to assume reading from flash memory is darned slow too.


Still trying to work out that bug... will post an updated version of the program with better debugging soon.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

FOUND IT.

I declare the arrays I assign to Boostmap in Setup().... then I keep checking that my data is valid in Setup.

As soon as it exits Setup, they went out of scope and got deleted, so in Loop() they were empty arrays pointing to garbage memory.  Declared them as Static in Setup() (pretty much the same as global) and my problems went away.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

OK, so the basic tests show it's working, though there's some 'jitter' in it and I attribute it to using an integral value rather than a float,... but it will do well enough for now, and once I get more into it and see what sort of memory I still have available I may reduce the map size but increase the resolution.. logic will stay the same, just have to change a few variable types.. it will also depend on processing speed evidently as well.

I made it return a float value, I think I can afford that... it's good for testing anyhow, as the the increased resolution helps debugging...

So here it is attached, I have a pot on A0 and A1 to simulate RPM and TPS, though I remapped the RPM values to 2000-3000 so I had a bit finer control.

There's lot of debug serial prints, so you can see what it's doing, I commented it relatively well
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

More progress.. Got it to read and write to EEPROM now.. each 9x14 map takes 174 bytes using BYTE types for the map, and INT types for the row and column labels... since I think this is plenty of resolution, I will put each map on a 256 byte boundary, leaving room for additional features if I need them.. Got a while before any of this is set in stone though
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Cool, lets clean up the loop and setup timers. I'll just post the ino

Don't need to setup the values as static in the loop, if you want to make a while loop. Also I'm really really used to X then Y for maps, any reason you did y then x ?

Lastly, incorporation of rolling RPM / Boost value, wouldn't be a bad idea. Maybe run it on a 1ms timer, so then you can average the last nth^2 ms , (2, 4, 8, 16, etc) I would average the last 8ms, and run everything on a 10ms timer for final calc.

**updated file, sorry put old one i was working on in.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

That's another way of doing it.. it comes to the same in the end though I think.. Your way Loop() never ends, so you don't need to set it as static, my way loop() does end, so keeping the variables requires them to be static. 


Did I comment the Mapping and interpolation part of it well enough for you to understand?  The "four corners" part can be a little confusing... without the enumerators it would have been impossible for me to do it!

I updated my VB version to reflect the changes, I think better in VB where the syntax is natural to me and I can concentrate on code logic.. Now that I'm a bit more familiar with C++ and it's fussiness about certain things I can hopefully code my VB to be easier to port.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

I haven't read your library yet, just making the assumption that it works, haha!

Make sure to double analogReads so you get that pin, sometimes you get the last pin, especially with another so close to last code wise.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

I do remember that, but from something I read somewhere the problem is mainly when you read analogs from 2 different ports...   reading it once seemed to work on this, but I will definitely keep it in mind when I make a 'real' sketch that has to function correctly.

I will also be reviewing the last sketch I posted, and will try to get the arrays internal to the class.. I'm just learning about malloc() now so I can do it.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles