"curiosity killed the bird", cc-by-sa
Kai Schreiber via Flickr https://www.flickr.com/photos/genista/
… for all the obvious reasons.
Since then, I have noticed that I had addictive behavioural patterns. Given the relative smallness (is that a word) of the Fediverse, I hope I will do more sensible stuff with my time.
I had noticed this before, when I ditched Facebook. So, bottom line: “social media” isn’t good for me.
Situation: I’ve taken about 50ish measurements on an FSH4 spectrum analyser saving a dataset of each using the save data function of the spectrum analyser.
Turns out: The R&S InstrumentView application does not install on Wine. So, how do I get the screenshots embedded in the saved datasets? Right: Search for the PNG magic number and a chunk end.
This is really sketchy but worked on all measurement files. If you’re in the same situation, just check out my repository.
There are various possible ways you can lose access to your Gitea instance. In particular, if you are getting user accounts via LDAP, you should always have a key to get back in: a “local” admin.
In case you haven’t got one (totally not like I did definitely only once 🤫), deleted it or lost its password, here’s the procedure in to variants: running on plain Linux, and running within Docker.
Plain Linux
List the available (local) user accounts (you may need to run this with sudo):
$ gitea admin user list
ID Username Email IsActive IsAdmin
1 user1 me@home.earth true false
You may not get a single user listed here. In order to add a local administrator, do this
$ sudo gitea admin user create --username local_admin --email admins@email.earth --admin --random-password
generated random password is 'IzgvOwv1M9EG'
New user 'local_admin' has been successfully created!
$ sudo gitea admin user list
ID Username Email IsActive IsAdmin
1 unpriv_local bowfingermail@gmx.net true true
3 local_admin admins@email.earth true true
Especially on shared machines, I’d strongly recommend not setting a password via the command line. It’ll stick in bash_history and may be visible in the process list. Hence the --random-password option here. Use the generated password upon first login in the browser.
Docker Container
In order to get to the “plain Linux” field, you simply run a bash in the Gitea Docker container.
What’s the container’s name? Get it via docker container ls -a. Let’s say it’s called gitea. To start bash in that container, do
user1@linux:$ docker exec -it gitea /bin/bash
Now, you basically do the same thing as in the section above.
docker # gitea admin user list
ID Username Email IsActive IsAdmin
1 user1 me@home.earth true false
docker # gitea admin user create --username local_admin --email admins@email.earth --admin --random-password
generated random password is 'ikV6xzPTiH7B'
New user 'local_admin' has been successfully created!
docker # gitea admin user list
ID Username Email IsActive IsAdmin
1 unpriv_local bowfingermail@gmx.net true true
3 local_admin admins@email.earth true true
Conclusion (and Other Platforms)
The Plain Linux approach works equally well on other platforms. I hope this helps.
Back in uni me and control systems never really clicked. So I took a dive into the topic as I had the time. Long story short for all who know equally little or even less about the maths of control systems as I do:
Often, you model control systems using the Laplace transform. That’s nice and good for continuous systems.
Once you’re going to the discrete (digital) world, you want to use the Z-transform.
There is a (complicated) relation between Laplace domain and Z domain.
There are approximations, using a complex maps to map from Laplace to Z domain (a bilinear transformation, Tustin’s method, being the one used here).
This is a post on serendipity, because what this is about is actually neither control systems, Z-, or Laplace transforms but a numbers game. Bare with me.
Many systems can be modelled using a rational function of the form
You can easily put this into sympy and play around with it.
import sympy as sy
s, z, Ts = sy.symbols("s, z, T_s")
a0 = sy.S.One
a1, a2 = sy.symbols("alpha_1, alpha_2")
b0, b1, b2 = sy.symbols("beta_0, beta_1, beta_2")
G = (b0 * s**2 + b1 * s + b2) / (a0 * s**2 + a1 * s + a2)
G # use `print(sy.latex(G))` to get the LaTeX representation
Nice. Let’s generalise this a little bit:
import sympy as sy
s, z, Ts = sy.symbols("s, z, T_s")
N, M = 2, 2
a = sy.symbols(f"alpha_1:{N+1}")
a = (sy.S.One, *a)
b = sy.symbols(f"beta_0:{M+1}")
G = sy.Add(*[b[i] * s**i for i in range(M+1)]) / sy.Add(*[a[i] * s**i for i in range(N+1)])
By playing with the values for N and M we can now generate any order of rational function we wish. Again, not the thing this article is about.
Then there is the bilinear transform called Tustin’s method that approximately maps the function G from the Laplace domain into the Z domain.
Sweet. Now, I want to have the representation in two polynomials in z (numerator and denominator) to extract the coefficients (because that’s what I wanted to hack into code):
num, den = G.as_numer_denom()
num = sy.Poly(num, z)
den = sy.Poly(den, z)
num.coeffs()
den.coeffs()
… but my eye spotted a pattern here. Do you see it? It’s easier to see in matrix representation, and I’ll only look at num (i.e. the expression with βi in it, because I set É‘0 to 1):
Here is the resulting matrix for N = M = 3 and for N = M = 5:
Here are the resulting matrices for N = M = 4:
Here are the resulting matrices for N = M = 6
The odd values for N and M produce matrices with all non-zero values, so I’ll look at those first.
What I find stunning is the fact that there are so many patterns and yet I fail to think of a pattern that creates the whole matrix. For example, the first column clearly follows the binomial distribution:
where k is the row number starting with 0.
The first and last rows of the matrix are power series of 2 and -2 respectively:
where l is the column number starting at 0.
Every column has a different sign pattern starting at all positive on column 0 and alternating, starting with + on the last column.
Ignoring the signs, the columns are related like this
N
#0 ↷ #N-1
#1 ↷ #N-2
#2 ↷ #N-3
#3 ↷ #N-4
3
྾ 8
྾ 2
5
྾ 32
྾ 8
྾ 2
7
྾ 128
྾ 32
྾ 8
྾ 2
What multiples relate the lth column from the left with the lth column from the right, ignoring the sign.
Maybe this follows from the power series mentioned earlier. But looking at the coefficients in each column the binomial pattern is lost as you move to the inside.
And last but not least wild zeros appear when you look at the even values for N and M:
Apparently, in the middle row as well as the middle column, every other value is zero.
All right, I’m absolutely sure mathematicians and control systems theoreticians have looked at this back in the day and this is all solved and done. Still, I found it interesting to look at.
If you want to create those values yourself, you can use this little script, and modify N and M. But beware, at N = 10 the script runs for 10 seconds.
import sympy as sy
s, z, Ts = sy.symbols("s, z, T_s")
N, M = 3, 3 # <-- Modify here
a = sy.symbols(f"alpha_1:{N+1}")
a = (sy.S.One, *a)
b = sy.symbols(f"beta_0:{M+1}")
G = sy.Add(*[b[i] * s**i for i in range(M+1)]) / sy.Add(*[a[i] * s**i for i in range(N+1)])
tustin = 2/Ts * (z-1)/(z+1)
G = G.subs({s: tustin}).simplify()
num, den = G.as_numer_denom()
num = sy.Poly(num, z)
den = sy.Poly(den, z)
for l in num.coeffs():
p = sy.Poly(l, Ts)
for i, B in enumerate(p.all_coeffs()):
try:
coeff, _ = B.as_two_terms()
except:
coeff = 0 if B is sy.S.Zero else 1
print(f"{repr(coeff):6s} & ", end="")
print()
Oh, and of course you can change N and M independently from each other, but I could not be bothered to look at this as well.
I’ve come across a situation where out of three docker containers that should be running, only one was shown by docker container ls -a. Weird, hu?
The missing two were clearly running their processes because the web service they provided was active and reachable.
After a lot of forth and back, it turned out that Ubuntu 20.04 with snap came with a docker snap – and had a recent Docker CE installed via the apt package manager. Don’t do that.
Once the snap docker was removed, it worked and showed all the containers again.
So, I have this Samsung NP900X3A laptop that I used for about 4 years. It started to have random glitches when I was about to start some very important work, so I decided to get a new laptop. I never sold or scrapped it, and recently I took it out of its neoprene bag, cleared the disk, installed a clean Ubuntu 20.04 and it worked like a charm…
…ish. Well, obviously it’s old, the battery had seen better days, but for work when it’s on power supply, it is a very decent device. Until recently:
I plugged it in, opened the lid, pressed power aaaand: it won’t boot.
Okay, my first though: The SSD died. Nothing catastrophic, a 120 GB mSATA costs about 45 €. So I booted into a Linux live system, quickly installed smartmontools (smartctl), opened my favourite smartctl reminder wiki page (seriously, man pages are still the best, but they’re so long!), and found that all tests passed. Well, the drive was old, yes, but still intact. fsck did not complain either.
But it still wouldn’t boot.
So, I opened the BIOS menu. First suspect: time and date were off by years. Aha! The BIOS battery, or actually, the battery for the real time clock (RTC), had died. And that set everything to defaults. Including: UEFI [DISABLED]. Well, you could’ve printed something, you nob…
Anyways: Enabled it, it boots again.
Here’s the culprit: Empty, unmarked
So, what about the battery? It’s obviously a coin cell on two wires with a plug. Nice idea, very accessible. Really. But at least some documentation would have been nice. No label, no print, nothing.
Measuring reveals: diameter 16 mm, thickness maybe a little below 2 mm. Hard to tell exactly while still in the rubber protection. So, could it just be a CR1616 or CR1620 cell with some spot welded soldering pins enclosed in some heat-shrink tubing?
No way to get the original part anymore. Samsung stopped selling laptops for quite a while in Germany (or even the whole EU, though it may have recently restarted again). Ebay has some offers. Anything from 5 to 150 € (seriously, it’s a coin cell, who charges more than 10 € for that?).
I decided to get one full package (cell, heat-shrink, wires and connector) for 5 € and a bare cell with spot welded solder pads for 2 €.
Got the wrong cell for 5 € (for a Samsung P20). Would’ve fit if it wasn’t for the connector.CR1616 for 1,59 € to the rescue!
So, where’s the rant? Here:
COMPLEX ELECTRONIC DEVICES “BREAK” BECAUSE OF < 5 € COMPONENTS AND YOU NEED A F****ING PhD IN BATTERIES AND EBAY TO FIND REPLACEMENT PARTS BECAUSE NOONE THOUGHT OF PRINTING “CHECK UEFI SETTINGS” OR OF PRINTED LABELS ON A BATTERY OR A SIMPLE REMARK IN THE USER MANUAL (“YEAH, AND BTW, THE BATTERY IS A CR1616 FOR 84 CENTS WITH A MOLEX CONNECTOR, YOU’RE WELCOME.”)???
By now, this laptop would’ve been thrown out twice by every single average capable consumer in the world. And honestly, I cannot blame them. Right to repair will have a loooooong way to go.
So yeah: Turns out the coin cell actually was a CR1616.
Cut off the wires, placed heat-shrink, soldered to replacement cell (not to the cell directly but to the solder tails), pack all in heat-shrink and voilà : a 1,65 € (+some solder and heat-shrink) replacement for a 10 year old laptop.
That’s it. Now, for example, pulling D3 high (i.e. applying 3.3 V) will switch on the red LED. Likewise for D4 on the yellow and D5 on the green LED. Let’s introduce some human readable names instead of D2 through D5:
To control the traffic light logic, I scribbled a Moore machine on a piece of paper, starting with a pedestrian traffic light (only red and green).
Moore machine with two states (red and green), initialised in the red state. Looped arrows indicate idle non-transition.
The machine starts in the red-light state. It remains there, unless the button is pressed. Then, it instantly switches to green (a pedestrian’s dream) where it remains until the button is pressed again.
This behaviour can be implemented by putting a simple switch case statement in the main loop() and implementing a state variable:
// States
enum states{ ST_R,
ST_G
}
void setState(bool red, bool green) {
digitalWrite(LED_RED, red ? HIGH : LOW);
digitalWrite(LED_GREEN, green ? HIGH : LOW);
}
void setRed() { setState(true, false); }
void setGreen() { setState(false, true); }
void loop() {
switch(machineState) {
case ST_R:
// Serial.write("State: Red");
setRed();
break;
case ST_G:
// Serial.write("State: Green");
setGreen();
break;
}
Why use this enum thingy, you say? You could just use an int and call your states 0, 1, 2, etc., you say? Yes, you could also sign up for the biannual global brainfuck contest.
Now that’s a simple and pretty useless traffic light (well, I still like that I can switch it to green at will, this really should be the default pedestrian crossing light!).
But it is easy to extend. You want a yellow state? Add a yellow state ST_Y to your state machine (the switch case statement). Or you want it to show yellow before switching from green to red? Add a yellow-before-red state ST_Y_R. Or you really like to let people wait? Add a wait state. Oh, and of course you can call your states whatever you want.
Here is my full implementation of a non-pedestrian red-yellow-green traffic light as it is stated in the German traffic rules:
From red to green, with wait periods, go through
Red
Red+Yellow (yes, red+yellow, yellow meaning “get ready” and red meaning “not too fast, it’s still red, Freundchen!”)
Green
From green to red, with wait periods, go through
Green
Yellow (yes, only yellow. The meaning is disputed. Reasonable people say it means “Prepare to stop, if you can’t make it in time, it’s okay.” while idiots say it means “STEP ON THE GAS!”)
Red (This is also sometimes disputed, I won’t go into this.)
Is this the best way to write this? Hell, no! Is it even a good tutorial? You tell me. I think it’s fairly simple and shows some evenly common concepts:
Using IO pins
Wiring LEDs without burning them or the output pin
Wiring a button*
Using #define statements to make code more readable and lives more easy
Using some abstraction (setState(), setRed(), etc.)
Using a state machine as an extensible concept
Using an interrupt (Why use an interrupt? Because it interrupts your code exactly when the button is pressed. It makes your little circuit very responsive and – at least in this example – it is even easier to write.)
*) You may encounter a problem that goes by the name “bouncing” (or in the beautiful German language “prellen”). That’s when your button press toggles multiple interrupts in a row. Try putting a capacitor in parallel with the resistor, or as a bad software hack, add a 5Â ms delay (delay(5);) to the end of your interrupt function.
Anyways: My girl loves it. Ha! You thought she’d be a boy because it was a traffic light. Shame on you!