#PCF8591 create command for WiFi

1 messages · Page 1 of 1 (latest)

red anvil
#

For a shipmodel project I'm trying to program my Raspberry Pi controlled transmitter. After having established a WiFi connection I'm now trying to make the step to send a command with a value coming from a joystick (15 joysticks in total, each with 6 different functions controlled by a yet to be added additional parameter), with an add-on for the propulsion of the model.

I can't seem to figure out how to translate the raw values and then write them into the parameter command. Each of 15 joysticks has a 2-digit ID to make sure the correct joystick controls the correct function. How can I make this happen?

delicate basin
#

To post code, see #welcome message. You can edit your existing messages to add the backticks.

#

I'm confused about the joysticks. These are analog-pot joysticks, not I2C, right?

red anvil
delicate basin
#

So here you are trying to read the analog value, I think?

            if joystick[key].value != 0:
                print(f"{key} is at a high level: {joystick[key].value}")

red anvil
#

That is correct, it was an attempt to get the value into the "commando" parameter. This code was actually suggested by AI in VSCode because I had a list of print statements, 1 for each value, below it

delicate basin
#

this is just checking whether the value is 0 or not, as opposed to the full analog value, which can range from 0-65535. Because these are essentially analog pots, it may even be the case that the range does not start at 0, but a little bit above. Instead, print out .value in a loop, as you are doing, and move the joysticks to see what kind of range you are getting. You can take out the call to main() for now so you can just see what's going on with the analog values.

#

you are interested in the range, right? (figuring out where joystick is on the full range of movement)

red anvil
delicate basin
#

even when they were in the same position (e.g. both in the middle?)

red anvil
delicate basin
#

the joysticks are wired with ground at one end and _3.3V at the other, is that right?

red anvil
delicate basin
#

and the slider pin is connected to each PCF8591 analog input pin?

delicate basin
#

the joysticks should be wired in parallel

red anvil
delicate basin
#

what is the part number of the joysticks?

#

our product ID number

red anvil
delicate basin
#

use 3.3V for everything so that the I2C signals are at the 3.3v

red anvil
red anvil
delicate basin
#

the Xout and Yout pins should each go to a separate PC8591 analog input pin. Vcc should be 3.3V and GND should be GND

red anvil
#

That is indeed how I wired the joysticks

delicate basin
#

at rest (not pushed), you should be getting a middle range value on Xout and Yout, I think, because the springs will bring the pot to around the middle.

#

so AIN0, 1,2,3 should all be around the same, at rest

#

sounds like something might be weird with the wiring

red anvil
#

These are the values from a test yesterday. There are a few PCF modules which are on the limit of what the wire can reach, the onboard LED of those PCF's is often completely off. Most of the PCF's are actually working fine for as far as I can tell

delicate basin
#

i assume one PCF board is connected to two joysticks?

red anvil
#

The voltage after the TCA9548A I2C multiplexer is 5V indeed

delicate basin
#

a picture of the wiring of one of the PCF's and its connected joysticks would be helpful.

#

So you are not using the STEMMA/QT connectors?

#

do not run this further until you fix that, as you might be damaging the RPi I2C pins by driving them at 5v

red anvil
#

The joysticks are hidden below the plate, but can be accessed if needed, the plate is not permanently fixed in place

red anvil
delicate basin
#

those are not our PCF8591 boards. Please give a link to the supplier

red anvil
delicate basin
#

is it our TCA9548A board or another supplier?

red anvil
red anvil
# delicate basin those are not our PCF8591 boards. Please give a link to the supplier
delicate basin
#

to be clear, this is a Raspberry Pi, not a Pi Pico, is that right?

red anvil
#

It's a Raspberry Pi 4 model B

delicate basin
#

that PCF8591 module has extra stuff on it: photoresistor, temp sensor, etc. If those are connected to the ADC pins, then you will get bad values. Maybe removing the jumpers disconnects those?? There is no schematic! Ugh

#

also there are various resistors on the bottom, doing I don't know what

#

there are dozens of these listed on Amazon, but I can't find any schematics

#

ok, I think this is generically known as a "YL-40" module

red anvil
#

It's a good thing that the issue with the high voltage is addressed, however my question is about the program. I want the value that is changed (IE I move a joystick) to be added to the 2 digits from the input into the parameter "commando" so it will be send to the shipmodel. How can I make that happen?

delicate basin
#

... so in any case, wire 3.3V to the TCA multiplexer, yes

#

you can convert the 0-66535 value to the range you want, and then use str() to convert the integer to a string that you can send in the URL to http://192.168.178.80:8000/cmd/{command}. I don't know the protocol that is being used there, though

#

do you have any spare PCF8591 modules? If so, connect one directly to the RPi, remove the jumpers, as you have, and try various voltages on the A0-A3 pins to see if you are getting reasonable values. I am still suspicious of the wiring.

#

Also, the PCF8591 can be configured as differential or single-ended ADC, and if it's configured wrong then you'll get weird values

red anvil
delicate basin
#

our library is very simple

#

you could just use the 3.3V from the RPI terminals?

red anvil
delicate basin
#

make sure you tie the ground connections of everything together

#

fastapi is very general. I would need to see the webserver script to see how it's receiving things

red anvil
#

That is currently also work in progress. I think I should be able to control the propulsion if I can somehow get it to either read the Board and Busio libraries from Circuitpython or if that doesn't work in the same program that is creating a FastAPI server then I'd need a way to get the command from the server in the program controlling the main propulsion

delicate basin
#

So the essence is

    srv = commando[:3]
    angle = int(commando[3:])
red anvil
#

Exactly

#

And that is what I'm trying to create from the transmitter side where my question began

delicate basin
#

the single command is the servo id plus the angle. You haven't converted to angle on the sending side yet. Maybe you don't want to do that? Maybe convert on the receiving side. That is more flexible, I would think. Just report the raw values to the receiving side, and have it figure out what to do (denoise, adjust for individual controls, etc.)

#

I would think there's a way in fastapi to send a command with args, instead of stuffing it all together into a single string. But I don't know anything about it

red anvil
delicate basin
#

I think it will be easier to code.

#

so are there two separate computers here? The RPi reading the control panel, and the receiver on the "ship"? Is this a remote-control model, or is it some game board? The reason to use the web interface is because the control panel is separate from the thing being controlled.

#

where did you get the "cmd" example for the FastAPI?

red anvil
# delicate basin the single command is the servo id plus the angle. You haven't converted to angl...

My general idea is to have as much generalised as possible a command and have the boat figure out what to do with it. So a range of 0-255 is pretty general and then have the boat rescale the value to 0-180 with some means of adjustments to both range and midpoint as well. This all considering that each joystick axis has about 6 different functions which are to be controlled by a selector on the yet to be made UI on a 10" touchscreen

delicate basin
#

so the boat is separate, battery powered, on the pond or whatever?

#

advantage of putting the specialization on the boat is that a single control panel can control different kinds of boats

#

disadvantage is that you don't specialize the control panel with labels for what that particular boat needs

red anvil
delicate basin
#

so I'm not sure about that division of labor after all

#

it depends on where, whether you need flexibility

red anvil
delicate basin
#

so then it doesn't matter so much

#

what kind of computer is on the boat? Another Raspberry pi or something weaker?

red anvil
delicate basin
#

now I'm thinking that the boat specialization should be done on the control panel side, because it's probably easier to deal with that when setting things up and debugging.

#

it's a lot easier to connect your control panel to a laptop on shore on a table than bring back the boat each time to edit its program

red anvil
delicate basin
#

of course, you could remotely login to the boat and edit its program too

red anvil
red anvil
delicate basin
#

still maybe easier to tweak the controller than the boat. "Oh, I need to adjust the servo angle a bit."

#

though there is calibration too. Two identical boats might still need slightly different servo angles to turn a certain amount due to slight mechanical differences.

#

I will leave you to decide that 🙂 . In any case, next steps here are to document the wiring with a schematic, check it, and maybe check a standalone PCF8591 + joystick to understand what is going on with the weird values

red anvil
delicate basin
#

a config file sounds like a good idea. Basically it is scaling, but it might also be reversal (use a negative scale). You could use JSON for that

red anvil
delicate basin
#

is this a one-person project?

red anvil
delicate basin
#

also think about making various things table driven from the config file or elsehwere. Any time you see repetitive code like this:

out16 = port1.get_pin(0)
out16.switch_to_output(value=True)
out17 = port1.get_pin(1)
out17.switch_to_output(value=True)
out18 = port1.get_pin(2)
out18.switch_to_output(value=True)
red anvil
# delicate basin is this a one-person project?

For 99% it is, my father has a lot more knowledge of programming then I do, though tailored to data rather then robotics. I started this project 18 years ago with this incredibly high goal and very little experience even building a shipmodel

delicate basin
#

... it is crying out to be described as data and to use arrays instead of separate names. out[port_number] instead of out16, out17, etc.

red anvil
# delicate basin also think about making various things table driven from the config file or else...

That's a good plan. Will this also work if those outputs are not logically connected to a command? I can see it work perfectly if output 1 is called with command 1 and 2 for 2 and so on. That is not the case here. I started making a list of all the functions, devided by 4 sections of shipmodel, then started appointing commands and joysticks to them. In terms of control I chose to keep things as intiutive as possible so 1 crane has functions controlled by 3 joysticks spread over 2 PCF's. Not because that is the minimum amount needed, but to keep up/down on the X axis of the joystick and left-right on the Y axis which is very intuitive considering the way the joysticks are mounted

delicate basin
#

well, in FastAPI everything does not have to be one kind of URL that is just cmd:

http://x.x.x.x/servo?change=<ii>
http://x.x.x.x/servopair?change1=<i>&change2=<j>

#

you could also decide to do something like:
http://x.x.x.x/rudder?angle=...
or
http://x.x.x.x/rudder?change...

#

q is whether to send the incremental change or the value you want. You use the joysticks to nudge the rudder one way or the other (unlike a ship's wheel!)

Don't know if you want to name the objects you are controlling, or just speak in terms of servos

red anvil
#

I now connected the 3,3V regulator succesfully (I checked the voltage) and now the numbers are more clear. I noticed that once I started moving a joystick the values dropped to an almost useful scale of 215,512 and 768 for as far as I could see.

delicate basin
#

those values still look weird, because if you are not touching the joysticks they should all be centered and you should be reading about 65536/2 (roughly) at each analog input

red anvil
delicate basin
#

is your father a software professional

delicate basin
red anvil
red anvil
red anvil
delicate basin
#

so he would have a lot of ideas about this, and how to structure your code, the config files, the FastAPI URL's etc. In other words, the abstractions you want.

red anvil
red anvil
delicate basin
#

also use the voltmeter to read the A<n> values at the PCF boards yourself as you move the joysticks

red anvil
#

I will

red anvil
#

The first test results are in, it would help if the pots actually work. On the first 6 axis, 2 are giving 0V, the others range from 0 for maximum in 1 direction to almost 3,3V in the other, some come close to this but don't reach 0V and don't reach 3V. Actually, all joysticks are broken on at least 1 axis

delicate basin
#

Vcc on all the pots should be connected together, whether by daisy-chaining or by going back to a common point. GND should similarly all be wired together. Then each Xout and Yout go to separate Ain pins.

#

did you solder the joysticks to the boards? We are talking about genuine Adafruit 480 512 boards?

red anvil
#

These indeed are the genuine Adafruit boards and the joysticks are soldered to the board. I now also measured the resistance of the pots (+ wire from the multimeter to the Xout or Yout and the - to the ground), this gives wildly different values for various pots. I appear to have 1 working pot which measured 3K-ish Ohm with the pot in the center, 0 at 1 end and 5K-ish Ohm on the other and that on both axis. All other pots had these values at best on 1 axis and some even measured close to or at 0 Ohm regardless of it's position.

#

Should the pots reach 10K Ohm?

delicate basin
#

can you unplug the pots from the PCF boards, so that there is no connection to anything other than your ohmmeter? Are the Vcc's all tied together and the same for the GND's?

#

I don't want anything on the PCF boards to affect the measurements

#

do you have any unused ones you can take a picture of?

#

and measure resistance on unused ones

red anvil
#

I have now taken the PCF's away and got halfway through testing the resistance with nothing attached to the joysticks. 1 joystick passed the test taking 1 loose joystick as baseline (highest resistance of all), but most don't reach the 5K Ohm on the 1 end and also don't quite reach 0 Ohm on the other end. Multimeter is set to 20K Ohm

delicate basin
#

so you are measuring resistance between Xout and GND or VCC and also between Yout and GND or VCC?

#

I would expect them not to quite get to 5k or 0

#

I don't have a sample of this to test, unfortunately

#

could you take a sharp picture of one of the "bad" ones from the bottom and top? Take off the knob

red anvil
#

These shoepw per axis the neutral, high end low value

delicate basin
#

the silkscreen does not match what's on our boards. And there is no Adafruit "star"

#

soldering of the upper left one and the upper middle one could be better 🙂

#

I have to go out now for a couple of hours. I'll check back later

red anvil
#

This is the top side

#

It does match yours, you designed them to be top mounted, that would leave them exposed to rain in my case

delicate basin
#

ah ok, well, I really have to go, will check back

red anvil
#

It seemed best not to change too much, I might have accidentally connected things the wrong way around

delicate basin
#

make sure you are not using the SEL pin by mistake. But it's ok to mount the headers on the bottom

red anvil
#

That is good to know

delicate basin
#

I found I had one of these after all. Some measurements:
between VCC and GND: 4.8k ohms
between VCC and Xout: 46 - 4.8k, 3.65k mid position
between VCC and Yout: 50 - 4.8k, 3.75k mid position

red anvil
delicate basin
#

These are pretty much exactly what I would expect. The basic pots are 10k each, so between VCC and GND they are in parallel, hence the 4.8k.
The othermeasurements are kind of as expected, though I might have expected the mid position to be more like 2.4k than 3.6k

#

you might try touching up the soldering on some of the "bad" ones

#

the unassmelbed ones should be similar. If all the unassembled ones test "good", then I suspect soldering

red anvil
#

That sounds reasonable indeed. I'll touch the soldering up as soon as I can

red anvil
#

It's not the soldering, the value remain way of. Also, when I start moving a joystick slowly it's not scaling like I would expect with resistance even going towards 6K Ohm at about 3/4

delicate basin
#

i see a small "dead zone" in one direction but not the other on each axis

#

this I think is the spring mechanics

red anvil
#

Do you mean that a fysical spring can affect the resistance? And would that be repairable?

delicate basin
delicate basin
#

maybe try measuring the resistance directly from the pins on the back of the breakout board

delicate basin
#

this is the upper left joystock in your photos. There should be ~10k across the outside pins in each boxed set. The middle pin is the slider

#

the one I marked with an arrow has questionable soldering, but it's hard for me to see

red anvil
delicate basin
#

ah, you're right, so 5k

red anvil
delicate basin
#

I searched the support threads in https://forums.adafruit.com with 512 joystick. There are some complaints about dead spots, but I didn't see anyone complain that the potentiometers were bad.

#

you could look at those in detail

#

it's possible they went bad over the years, especially if there was moisture. pot wipers can become erratic. I have noticed this in much older electronic equipment. They become scratchy

red anvil
#

Even if they've been inside all the time?

delicate basin
#

the middle top looks quite corroded??

#

or is that just solder flux?

#

it's true, mine is probably the same vintage and works fine

#

did you use electronics solder (not plumbing solder) on these when you solder them up?

red anvil
delicate basin
#

that's good. did you try these joysticks at the time and they were ok?

red anvil
#

Unfortunately no, I'm only recently starting to get a bit to grips with the programming side of the project

#

Behind the scenes I have tested a PCF using your test program and a 10K pot and proved that at least 1 PCF is giving proper voltage readings.

#

Though it appears that something is off with the TCA9648A in between the PCF and the Pi. I tested with a new PCF connected straight to the Pi and got properly changing values both in voltage and in raw value (basically 512 through almost 65K) but switching this setup (so the 2 10K Ohm pots) to a PCF connected through the TCA9648A gives mostly constant values though at times it does jump around

#

The GND of the TCA9648A goes directly to the Pi which has a Geekworm X735 V3.0 hat for handling power. From it's 5V pin runs the power (including GND) for all the devices including those connected behind the TCA9648A. On the X735 board the GND is connected to the DC connector powered directly (fused) by a 4S LiPo battery of 11000mAh. Can this cause the problems with the readings?

delicate basin
#

the I2C multiplexer should be supplied with 3.3V, not 5v, so that its connections to the RPI will be 3.3v compatible

red anvil
#

I'll be working on that shortly

delicate basin
#

getting late there; I'll be around tomorrow

red anvil
#

I have tried to combine the GND of both the I2C systems (straight from the Pi, the other being all behind the TCA9548A) and then tried to see what addresses are connected to the TCA9548A (your example code) but then the I2C couldn't find anything attached anymore. Then I tried to remove the devices one by one and then reconnecting one by one and suddenly the i2cdetect found devices again. Ran the TCA9548A example code again and then all devices were lost again. At this point I'm considering taking all the wiring out and starting all over again. I'm thinking of routing the power lines across all PCB's so the 3,3V and the GND all come from the Pi rather then using ribbon cables and then combining all like 23 wires in 1 place causing a huge cluster of wires

#

I don't have any pullup resistors added and I haven't removed any on the PCB's

delicate basin
#

It is still strange that you were seeing bad resistance values on the disconnected joysticks. If they don't look rusty or damaged or whatever, I am at a loss to explain that.

red anvil
delicate basin
#

easily available at a hardware store

#

I think they will hold dupont jumper ends

#

or just bare wires if you have already cut the jumpers in half

red anvil
# delicate basin disadvantage is that it's harder to disassemble due to the soldered terminals. S...

That's true, on the other hand, the connection will be rock solid, even when the wire is at the limit like I have now for 2 PCF's. Added advantage is that the the power wiring is easily recognizable and takes up much less space. Even the Wago's (of which I have a few laying around) will make a mess again. And if something would be wrong with the power cables it's quickly visible where it goes wrong, while even the Wago's it will be a search to find the wrong connection. So trouble shooting will be much more easy. This then also applies to the SDA and SCL lines because those are not obstructed anymore by a huge cluster of power lines.

red anvil
red anvil
delicate basin
#

sounds good; you have a better idea of how you want to arrange things.

red anvil
# delicate basin Our I2C breakouts are all redesigned now to have chainable STEMMA/QT connectors:...

I am considering this option as back-up for if the current PCF's prove to be faulty as well. I'd need to look into the STEMMA/ Qwiic options, these would be the only devices that have them so that might be a challenge. The ADS7830 would actually be cheaper then replacing all the current PCF's so that is a plus and it would seem it's hardly bigger but I'd need half the amount which is a big advantage.

delicate basin
#

I know it's traditional to use analog spring-loaded joysticks, I think partly because of how old radio-controlled models worked, but since this could be whatever you want, have you considered simple digital joysticks for some things, or rotary encoders, or potentiometers that don't spring back to center? For instance the rudder wheel could be a potentiometer. The crane control could be just a digital on/off joystick, etc.

red anvil
#

A hall sensor in the place of the potmeter would indeed improve reliability. For the main propulsion I use AS5600's to measure the angle of the propulsion (360 degree rotatable azipods) and potmeters for their fysical resistance for the throttle lever on top. This helps to keep the lever in place, taking away the challenge of designing a proper spring system which would also make controlling the model a bit more difficult considering I can indepently control of total of 3 (normally 1 will be retracted into the hull, so normally 2) azipods and 2 bow thrusters. The same Adafruit joysticks will be used for controlling the bow thrusters.

#

Replacing these joysticks wouldn't make sense because with a press on a button on the touchscreen that joystick goes from controlling a crane to putting a lifeboat overboard, tick that button again and now it's controlling the lifeboat. In total I have 6 functions groups for the joysticks so I can have basically all PWM controlled functions (except for the fire pumps, they'll have a slider on the touchscreen) will be controlled by these 15 joysticks.

delicate basin
#

much more complicated than I realized! 🙂

red anvil
#

I can understand that. For reference, in the model itself we're talking 8 16 channel servo modules, 4-6 16 channel MCP23017's (I replaced a few by TLC5947's), 2 LMS9DS1's (1 for back-up and error detection), a couple of TCA9548A's to make it all controllable. A MCP2221A makes a second I2C network so that if a non propulsion device (IE 2 PWM modules, a PCF for registring how far the retractable asipod is retracted and 1 LSM9DS1 to solve the adress issue straight away) I can still steer my model back to shore. This actually fits very well in the spirits of the real ship which has it's engine rooms devided such that fire in 1 area on the ship can never make it loose control

red anvil
#

Can one use each channel on the TCA9548A as an individual I2C network? So can I connect several devices, each with a unique I2C address, to the same channel on the TCA9548A? In the example code I see sensors being called upon simply by the channel which they're attached too, can I add an I2C address there?