RaspberryPi - check state of disconnected pins - raspberry-pi

I want write script for detecting which pin is connected with which one.
For example (BCM mode) I connect GPIO2 with GPIO3 and GPIO4 with GPIO5. My idea was to check each with each if there are connected.
for i in range(2,6):
for j in range(2,6):
check(i,j)
If all pins are connected it works, but if same of them are disconnected code print same crazy for me output. I think all input should return 0, but as you see same of them are in HIGHT state.
output:
1 2 3
0 2 4
0 2 5
1 3 2
0 3 4
0 3 5
1 4 2
1 4 3
0 4 5
1 5 2
1 5 3
0 5 4
code:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
for i in range(2, 6):
for j in range(2, 6):
if i == j:
continue
GPIO.setup([2, 3, 4, 5], GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(i, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(j, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
sleep(0.1)
if GPIO.input(j):
l.add(tuple(sorted((i, j))))
print(l)

I think the problem You have is related to pull-up and pull-down resistors. They are used to ensure the "known" level of digital value (1 or 0).
http://www.resistorguide.com/pull-up-resistor_pull-down-resistor/
There are many solutions to this "problem" -here is one for Arduino but actually every digital IC has these "problem" and datasheet should say how to deal with unconnected pins and what to expect in case of leaving them dangling.
https://electronics.stackexchange.com/questions/35704/arduino-has-an-input-value-in-disconnected-pin

Related

What algorithm is used when rows in tables are sorted?

Let's assume that we have a table with two columns. The table contains data and our goal is to sort that table.
Assume our data looks like this, where y1 and y2 are the data in the columns.
You can produce that plot with MATLAB or GNU Octave.
% Simulate the model
[t,y] = ode45(#odefunc,[0 20],[1; -2]);
% Plot the simulation
close all
plot(t,y(:,1),'-r',t,y(:,2),'-b')
title('Solution of van der Pol Equation (\mu = 1) with ODE45');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
grid on
function dydt = odefunc(t,y)
dydt = [y(2); (1-0.1*y(1)^2)*y(2)-y(1) + 1];
end
If we look above the plot, we are going to se the data like this:
You can create that plot with this code:
% Plot 3D bar
figure
imagesc(y)
colorbar
Here we can see that the plot have a very much like a "table-look". My question is what algorithm is used when sorting the rows in the table so every row that looks almost the same, have it's own unique position in the table.
For example, if we have a table like this.
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
The code if you want to create that table.
j = 0;
rows = 20;
for i = 1:rows
disp(sprintf("%i %i %i", j, j+2, j+4))
j = j + 1;
if(j + 4 >= 10)
j = 0;
end
end
We can see that there are four rows of 0 2 4 and three rows of 5 7 9.
I want all rows 0 2 4 close to each other and all rows 5 7 9 close to each other. And.... 0 2 4 cannot be after 5 7 9 because then the plot would look terrible.
For example, assume that we begining with row 1, the first row 0 2 4. Then we are looking for the same rows of 0 2 4 and let's say we found four rows 0 2 4. Then we sort them.
0 2 4
0 2 4
0 2 4
0 2 4
Now next row would be 1 3 5 and we find two rows of 1 3 5. We sorting them.
0 2 4
0 2 4
0 2 4
0 2 4
1 3 5
1 3 5
After we have sorted for a while, we are going to have a table like this.
0 2 4
0 2 4
0 2 4
0 2 4
1 3 5
1 3 5
2 4 6
2 4 6
2 4 6
2 4 6
3 5 7
3 5 7
3 5 7
.
.
.
.
5 7 9
5 7 9
5 7 9
And now, we found 1 2 4, which is very similar to 0 2 4. So we need to place 1 2 4 close to 0 2 4, perhaps between 0 2 4 or 1 3 5 or after 0 2 4 or before 0 2 4. How do I even know that 1 2 4 should be placed close to 0 2 4? That's the issue!!!.
How can I sort that?
I need to do that in C-programming language because speed is most important here, but I think I will start to do it in GNU Octave. I'm pretty sure that there is a SQL-sorting algorithm I'm looking for.
Notice in practice, there are numbers, integers, 10-bit e.g values between 0-1023.

KDB/Q how do we calculate the moving median

There are already moving average in kdb/q.
https://code.kx.com/q/ref/avg/#mavg
But how do I compute moving median?
Here is a naive approach. It starts with an empty list and null median and iterates over the list feeding in a new value each time.
Sublist is used fix the window, and this window is passed along with the median as the state of into the next iteration.
At the end scan \ will output the state at every iteration from which we take the median (first element) from each one
mmed:{{(med l;l:neg[x] sublist last[y],z)}[x]\[(0n;());y][;0]}
q)mmed[5;til 10]
0 0.5 1 1.5 2 3 4 5 6 7
q)i:4 9 2 7 0 1 9 2 1 8
q)mmed[3;i]
4 6.5 4 7 2 1 1 2 2 2
There's also a generic "sliding window" function here which you can pass your desired aggregator into: https://code.kx.com/q/kb/programming-idioms/#how-do-i-apply-a-function-to-a-sequence-sliding-window
q)swin:{[f;w;s] f each { 1_x,y }\[w#0;s]}
q)swin[avg; 3; til 10]
0 0.33333333 1 2 3 4 5 6 7 8
q)update newcol:swin[med;10;mycol] from tab

Nested loop and conditional statement (Matlab)

If you have a random matrix, for example a 5x5:
A(i,j) = (5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1)
And a second array:
B(1,j) = (4 5 6 7 8)
How can I then assign values of B to A if this only needs to be done when the value of B(1,j) is larger than any of the values from a certain colomn of A?
For example, B(1,1) = 4 and in the first colomn of A it is larger than A(1,1), A(3,1) and A(5,1), so these must be replaced by 4. In the second colomn, nothing needs to be replaced, etc.
Thanks already!
You can do this without any explicit looping using bsxfun:
A = [5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1];
B = [4 5 6 7 8];
A = bsxfun(#min,A,B);
Result:
A =
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
In later versions of MATLAB (2016b and later) you can even omit the bsxfun and get the same result.
A = min(A,B);
Matlab "find" may be of use to you.
https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html
If you aren't concerned about speed or efficiency, you could also set up a two nested for loops with a condition (i.e. an if) statement comparing the values of A and B.
If you only interested in column wise comparison to B, you could use the increment of the outer loop in the inner loop.
for i,...
for j,...
if B(1,i) > A(j,i)
A(j,i)=B(i,j)

Understanding moving window calcs in kdb

I'm struggling to understand this q code programming idiom from the kx cookbook:
q)swin:{[f;w;s] f each { 1_x,y }\[w#0;s]}
q)swin[avg; 3; til 10]
0 0.33333333 1 2 3 4 5 6 7 8
The notation is confusing. Is there an easy way to break it down as a beginner?
I get that the compact notation for the function is probably equivalent to this
swin:{[f;w;s] f each {[x; y] 1_x, y }\[w#0;s]}
w#0 means repeat 0 w times (w is some filler for the first couple of observations?), and 1_x, y means join x, after dropping the first observation, to y. But I don't understand how this then plays out with f = avg applied with each. Is there a way to understand this easily?
http://code.kx.com/q/ref/adverbs/#converge-iterate
Scan (\) on a binary (two-param) function takes the first argument as the seed value - in this case 3#0 - and iterates through each of the items in the second list - in this case til 10 - applying the function (append new value, drop first).
q){1_x,y}\[3#0;til 10]
0 0 0
0 0 1
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
So now you have ten lists and you can apply a function to each list - in this case avg but it could be any other function that applies to a list
q)med each {1_x,y}\[3#0;til 10]
0 0 1 2 3 4 5 6 7 8f
q)
q)first each {1_x,y}\[3#0;til 10]
0 0 0 1 2 3 4 5 6 7
q)
q)last each {1_x,y}\[3#0;til 10]
0 1 2 3 4 5 6 7 8 9

Strided copy between HOST and DEVICE clEnqueueWriteBufferRect

I search for a means to transfer data from two HOST buffers into a single DEVICE buffer in the following strided way:
Below are the two host buffers
Host_buffer_1 = [0 5] // copy to device with a stride equals to 5
Host_buffer_2 = [1 2 3 4 6 7 8 9] // each region of 4 numbers copy with a stride
I need the resulting Device buffer to be
device buffer [0 1 2 3 4 5 6 7 8 9]
Do I have to realize it on the HOST first and then a normal transfer to the device, or do you know a means to achieve this using clEnqueueWriteBufferRect function for instance, but this function does not have any stride parameter, right?
Thanks
You can use cl calls to do the rectangular patch copy on the fly. However, performance wise, I am not sure if this is the right approach.
If you shape your data as a 2D:
0 1 2 3 4
5 6 7 8 9
Then the buffers map like:
Device Host1 Host2
1 2 2 2 2 1 2 2 2 2
1 2 2 2 2 1 2 2 2 2
Therefore the copy rect commands should be:
clEnqueueWriteBufferRect(queue, buffer, CL_FALSE,
{0,0,0}, //buffer_origin
{0,0,0}, //host_origin
{1,2,1}, //region
5*sizeof(type), //buffer_row_pitch
0, //buffer_slice_pitch
1*sizeof(type), //host_row_pitch
0, //host_slice_pitch
host1, 0, NULL, NULL);
clEnqueueWriteBufferRect(queue, buffer, CL_FALSE,
{1,0,0}, //buffer_origin
{0,0,0}, //host_origin
{4,2,1}, //region
5*sizeof(type), //buffer_row_pitch
0, //buffer_slice_pitch
4*sizeof(type), //host_row_pitch
0, //host_slice_pitch
host2, 0, NULL, NULL);
But be VERY careful with the row_pitch and slice_pitch, as well as the offsets and regions. Since it is quite easy to get messed up. (And please check my code if you use it)
clEnqueueWriteBufferRect