Need help understanding lookup tables

ol boy

Member
I need a better handheld explination of how to right and understand the way a lookup table is used in programing.

I've wrote a code for the 20M to control a airride system in a car. I would like to have 4 set ride heights based on the speed of the vehicle. I've started with the following statements

if b7 < 40 then flash_2
if b7 > 40 then flash_3
if b7 >100 then flash_4

and so on.

Can I make a 1x10 table to reflect which flash to use at different parts of the speed range? Would this save space or should I just stick with what I got?

Thanks Ryan
 

Rickharris

Senior Member
Picax support 2 command - Lookup and Lookdown Page 115 and 116 in the basic command manual that do what you want. You may not save a lot with just 3 options though.
 

MartinM57

Moderator
...also, have a think through your code with:
- b7 = 40 ... what happens?
- b7 = 101 .. what happens? ...which label do you want your code to go to - and where does it go?
 

Coyoteboy

Senior Member
...and considering it's ride height adjustment, I'd want to make damn sure my code was bomb-proof and fail-safe to prevent "misadventures" in corners. Also beware that changing ride height by speed means you'll affect things like braking and accelerating handling too.
 

hippy

Ex-Staff (retired)
I would like to have 4 set ride heights ... Can I make a 1x10 table to reflect which flash to use at different parts of the speed range?
It's hard to see why you'd need a 1x10 table to handle 4 settings, and you only have 3 in your example code, so a clearer specification on what you actually want will help.

The most elegant solution would be a SELECT CASE statement, but that may also take up the most code space. Using LOOKUP or a traditional lookup table READ/EEPROM may be shorter but needs your index into the table to be from 0 and incremental. That may be possible but depends on what values b7 actual has.

I would also add some hysteresis to determining b7 otherwise ride height will likely be continuously changing when your b7 moves around value 40 which you probably don't want.

If the problem you are having is lack of program memory, given that this is a component which will probably be in use for the life of the vehicle, trying to save a few cents in using a low price PICAXE is false economy and creating unnecessary constraints. You would be better off using a PICAXE with more programming memory.

Due to the nature of the system, adverse effects on cornering etc, this is a safety critical system and must be treated appropriately.

You need to be totally sure that your code is perfect, no silly mistakes like letting a b7=40 slip through the net. If not sure and experienced with what you are doing it is often best to leave well alone.

Whether cornering or not, during emergency braking or other situations, if the system causes change in ride height which makes avoiding action less effective or ineffective you could be held liable in law for that. Plus you have to live with knowing it was your mod that turned an avoidable accident into an unavoidable one and live with the consequences of that. On public roads you are never putting only your own life at risk.
 

westaust55

Moderator
Picax support 2 command - Lookup and Lookdown Page 115 and 116 in the basic command manual that do what you want. You may not save a lot with just 3 options though.

Note that although you only show a few tests cases, that you in fact do
firstly mention:
I would like to have 4 set ride heights
and towards the end state:
"Can I make a 1x10 table to reflect which flash to use . . ."

SO question back is exactly how many flash routines do you want.

notwithstanding the safety issues of modifying car elecrics and controls which I will let others detail,

presuming you can create a failsaif scheme, another PICAXE BASIC command to consider is:
ON...GOTO (or ON...GOSUB) - see manual 2 (Rev 6.9) page 120 onwards

IF you can take the speed, divide by a number to get 10 values, then the program line format is:

ON speed_factor GOTO flash1, flash2, . . . flash 10

think some further information:
1. confirming the exact number of suspension settings
2. How many bits of data are required per setting
3. maybe what are the data for each setting

to help others here help define the best way to store/manipulate/calculate/lookup the required data.


Which PICAXE do you intend to use?
How big is you overall program?
these answers might help decide what EEPROM and other memory you have to store data in.
 

ol boy

Member
Thanks for the replys.

I have 4 flash areas for 4 different ride heights. B7 is an ADC input. I'm going to use a Ferq to Voltage converter to generate the 0 to 5 volt signal. I aslo put into the code when the brakes are applyed make input 0 high which flashes to make all outputs stay low until speed is below 10 ADC. Once below this speed the ride height will then adjust to that speeds height.

The 20M had 4 ADC inputs so I'm just controling front, rear, speed and air tank pressure.

I also used the remaining 3 inputs to use as a maunal setting for ride height.
ie... if 5 high then flash_4

I can also just switch the controller off and go manual mode with normal toggle switches.

My project car is by no means a race car, just a cruiser. 1964 Ford Falcon Wagon. I added IRS out of a T-Bird and have always wanted to an air ride.

Thanks Ryan.

P.S. I'm by no way a code writer but a machanic by nature but I'm seeking computer science and Electrical Engenering once I get out of the Army next year. Trying to get a head start!
 
Last edited:

Andrew Cowan

Senior Member
What sensors are you going to use to monitor the speed, tank pressure and front and rear?

You mention a freq to voltage convertor - it what is the actual speed sensor? You could use the count command of the PICAXE to measure freq if needed - this may simplify things.

A
 

Dippy

Moderator
Good for you ol boy.

Take note of martin's comments above.

With just 4 options IFs are fine.

But DO have a look at your structure of code and 'role play' a number.

You have:
if b7 < 40 then flash_2
if b7 > 40 then flash_3
if b7 >100 then flash_4

As Martin asks, what happens if b7 is 40?
In your code it will pass right through the IF tests.
And if b7 is 120 it will go to flash_3 as it is greater than 40. It will never get to ".. >100 then..."

You'll need to re-arrange to ensure it works.

if b7 >100 then flash_4
if b7 > 39 then flash_3
if b7 < 40 then flash_2

or you could use >=.

Always a good idea to run it through your mind....
 

ol boy

Member
Can I, in one statement set ranges of speed to a ride height?

0 to 39, 40 to 80, 81 to 90, 91 to 255 flash_2, flash_3, flash_4, flash_5?

I just need a better example of how write and understand lookup and lookdown. The manual sometimes doesn't break it down far enough for a newbe to figure out.

Sensors will be automotive style TPS for ride height. Mechanicly I need a way to tie left to right movement and avarage them to figure the ride height in the center of the car fo rthat axle. I'm thinking of hanging a small arm off of the sway bars and makeing sure the suspension travel doesn't exceed the movement range of the TPS. TPS are nice.. 5 volt in 0 to 5 out. easy.

Pressure will be a standard 0 to 150 psi resistor type pressure thingy. I'll apply voltage divider to figure what the bias(Ri) resistor needs to be to create the 0 to 5 volt range. Have not got that far yet.

Speed was going to use a VR sensor mounted to eighter the drive shaft or back side of the hub and use wheel studs to create an AC wave to the LM2709 Frq/Volt converter. Changing the resistor and caps alows you to change the v/hz range to match what your working with.

I'll read up on the count command and see what thats about.

I can post my code if you all want. give me some pointers on the subject. Later Ryan
 

hippy

Ex-Staff (retired)
SELECT-CASE is by far the easiest ...

Select Case b7
Case 0 to 39 : Goto flash_2
Case 40 to 80 : Goto flash_3
Case 81 to 90 : Goto flash_4
Case 91 to 255 : Goto flash_5
End Select

This is safer, because it traps any mistakes if editing the numbers later...

Select Case b7
Case <= 39 : Goto flash_2
Case <= 80 : Goto flash_3
Case <= 90 : Goto flash_4
Else : Goto flash_5
End Select
 

Dippy

Moderator
Lookup n, (xxxxxxxxx) , result

Get the nth byte in xxxxxxx and stick it into <result> variable

xxxxxx can be a string in quotes or a pile of bytes.

Lookup 4, (0,1,2,3,4) , result

= Find the 4th byte (starting with ZEROth) and put it into variable <result>.
In this case result = 4



Lookdown target,(xxxxxxx), result

Search and see if target value is in xxxxx and place the position into <result>

Lookdown 2 , (0,1,2,3,4) , result
=
See if 2 is in the list and if so put its position into variable <result>
In this case 2 is the 3rd byte, but as we start with Zero then result = 2.
If it can't find the target value then result is left unchanged.

Summary.
Lookup is used to plonk byte n from a list/string into <result>
Lookdown is a simple search to see where the 'target byte' occurs in the list (or string).


In your case you are looking at ranges.
I don't think you want to use Lookdown or LookUp.
Lookdown and Branch would be slick but Lookdown can't search on ranges.

If you don't want to use IFs then use Select case etc which can look at ranges - and it looks prettier.
 

ol boy

Member
Oh I C.. Very easy to understand that. How does the count command work? What do I do with the measured count? Give it a name or value and a range for each flash?

ie...
count 7, 1000, w1
symbol freq = b7
select case freq
case 0 to 39 : flash_5
case 40 to 79 : flash_4
case 80 to 120 : flash_3
case 121 to 180 : flash_2

the count 7, is input 7.. 1000 would be counting the pulses in that 1 second time frame? what is w1, I see it all through the manuals.
 

eclectic

Moderator
@Ryan
"what is w1, I see it all through the manuals."

Look at Manual 1, page 67, then

Manual 2, pages 10 and 42.

It's basically the difference between counting up to

255 or 65535.


128, 64, 32, 16, 8, 4, 2, 1 for the bytes.

I'll leave you to do the Words.

e
 
Top