Sunday, April 3, 2022

Connecting Raspberry Pi Zero W with Raspberry Pi Zero 2 W via USB-Ethernet

Surprisingly it is easy!!!


In this article we will learn how to connect two raspberry pi zeros together. 

In this example we will use one Raspberry Zero W  & the other Raspberry Zero 2 W

you need to power only the Raspberry Zero W & the cable will power Raspberry Zero 2 W, of course you can provide a separate power source especially if you want to connect sensors and other hardware on the other board.

Same configuration can be implemented between two Raspberry Zero W.


Steps :


For you need to enable Ethernet over USB.

sudo nano /boot/config.txt



as in the above picture at the end of the file just add 

dtoverlay=dwc2


Now you need to edit cmdline.txt.  Look for rootwait and add modules-load=dwc2,g_ether after it immediately. 




Now we will assign IPs to both boards. One of the boards will be the gateway. In our case it could be any board. but if you are using a RPI-4 where you can connect many boards together then the main board should be the gateway.


sudo nano /etc/dhcpcd.config 

You need to edit the file by adding the following to the end

for the router:

 interface usb0

                static ip_address=192.168.11.1 # should be equal to router as this is the router

                static routers=192.168.11.1

                static domain_name_Servers=192.168.11.1

                metric 400 # determines priority of using this net compared to wifi and others





for other boards:

 interface usb0

                static ip_address=192.168.11.123 # can be any ip within the subnet domain.

                static routers=192.168.11.1

                static domain_name_Servers=192.168.11.1

                metric 400 # determines priority of using this net compared to wifi and others




After Restarting

sudo reboot now


Both boards you can see the usb0 section with the static IP







It is very easy and very useful. As serial communication will allows only one app to communication. But connecting via Ethernet means there is a many-to-many communication between applications on both boards in a reliable fashion.


Thank you....






Tuesday, January 11, 2022

3N+1 Problem

     This topic is very different than many of my previous topics. It is about math!! First I am not a mathematician and not a big fan of fan. It is hard.


The story started here when I saw this video. I love this channel and I have watched most of its video.


I was excited to know that such simple problem has not been solved. So I decided to start to play with it.
No No I have not solved it. But I came with nice graph that it might be useful.



First let's mention the rule here, take any positive integer number:

        if number is odd then multiply it by 3 and add 1. 

            N>3N+1

        if number is even then divide it by two.

  let us take some examples:

        7 :  22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
       11:  34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 

 easy !

  Now let us focus only on the last digit of any number not the whole number. Let's rewrite the above number again:

        7 :  22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
       11:  34, 17, 52, 26, 13, 40, 20, 105, 168421 
       

  

We can deduce a sequence here:

    A digit ends with 1 will always gives a digit ends with 4. because 



  We can apply same technique to other odd numbers and we get the following table:


1x3 + 14
3x3 + 10
5x3 + 16
7x3 + 12
9x3 + 18

   

  For even numbers; we can deduce them using the following tables:


2/21
126
2211
3216
4221
5226
6231
7236
8241
9246
10251


4/22
147
2412
3417
4422
5427
6432
7437
8442
9447
10452


6/23
168
2613
3618
4623
5628
6633
7638
8643
9648
10653


8/24
189
2814
3819
4824
5829
6834
7839
8844
9849
10854


0/20
105
2010
3015
4020
5025
6030
7035
8040
9045
10050


Now we have all the information we need to draw a state machine diagram for these numbers:





you can verify this with any number generated by the 3N+1 problem and the sequence will always be valid.


What can we see in this graph:

    1- The blue lines are where we divide by 2 hence reduce the value. The red lines are where we multiply by 3 and add 1 hence increase the value.

    2- Blue lines are 10 and red lines are only 5. That means overall the value should decrease even if it is increased for sometime.




    3- Loops with same number of arrows of different colors leads to increase in values. for example loop {6,3,0,5,6,3,0,5,6...} but the loop will break as divide by two at a moment will give a number ended by 8.

   4- Loops {6,3,0,5,6,...} can never be broken except at number 6 only. Loops {9,8,9,8...} can be broken at number 8 only. Loops can only be broken at even numbers only.

   
   5- Loop {1,4,2,1,4,2,....} there are two blue lines and one red line. The result is 1 when n =1 that is why it loops forever.

   


 6- If someone can prove that loop {6,3,0,5,6} can be repeated forever which I do not believe it is possible, as each time you multiply you get the following assuming you do not loop in the Zero loop.

which is
The number will either loop in Zero or get out in 6 to 8 because incrementing by 10/4 is 2.5 

 Similar rules is applied to {8,9} and {4,7,2,1} loops. remember only balanced loop is {4,2,1} when n is {4,2,1}.   



7- If you get out of a loop then to get in again you pass one or more blue lines. if you are in loop 6 and move to loop 8 and come back to 6 using worst path you need to move on {6, 8, 9, 4, 7, 2, 6} which is 2 red lines and five blue lines. The number is ALWAYS reduce.


So you have thre main rules guide the behavior here.


A- You cannot loop in one loop forever except {4,2,1}.

B- Jumping from one loop to another reduces the value.

C- Moving back and forth from loop 6 to loop 8 requires passing on loop 4. It is a Must.


So as value gets smaller and passes via 4 from 8 that explains the 4 ,2 ,1.




I know this is NOT a scientific mathematical proof. But it explains to certain extend the behavior of this formula.









Sunday, May 9, 2021

How to make your Own SD Raspberry Image

Why Do I need This ? 

Here is the problem. Raspberry Pi SD image is less than 4GBs and you copy it on SD memory card of size 8, 16, 32 GB. It expands and you can install and customize your raspberry pi.

Now you have your own SD card with your own software that you want to reproduce. If you use dd command only for your 16GB SD card you will get a 16 GB image file. You need a 16 GB SD card to restore it again. Although the software itself can be restored in 8GB card -assuming the card is not full- and the remaining size is unused space !!! 


Here are the steps to produce a compressed raspberry pi image version:


Steps:

1- Insert SD-Card you want to clone into your laptop.

2- Run lsblk

you should see something like:

    sdb\sdb1  \media\$USER\boot

    sdb\sdb2  \media\$USER\rootfs


3- Unmount partitions

    sudo umount \media\$USER\boot

    sudo umount \media\$USER\rootfs


4- Now copy the full sd-card:

    sudo dd if=/dev/sdX   of=~/my_large_image.img

  

5- Now Compress Image using pishrink

    pishrink.sh ~/my_large_image.img  ~/my_compressed_image.img


Further more  you can compress it to zip file.


How to Get pishrink


1- Download:

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh


2- Change Permission:

chmod +x pishrink.sh


3- Move it: 

sudo mv pishrink.sh /usr/local/bin

Saturday, September 26, 2020

Mavlink 3D-Map

 This is nice and easy. And you can expand it as you need.

Mavlink is communication protocol  used by many autopilot including the very famous ardupilot.

Three.js is a very famous  corss-browser JavaScript library used to develop animated  3D computer graphics in web browser using WebGL.

If we combine these two things together we get Mavlink3DMap, this is a simple static site that receives parsed mavlink  messages and create correspondent vehicles and set their position and orientation.

SITL or a real vehicle sends Mavlink messages received by a UDP listener socket. the  UDP socket redirects these messages to the static web page via websocket connection.

 

 The 3D Map create a vehicle with a camera attached to each vehicle. The main screen shows 4 view ports -that you can change easily- each view port has a world camera that you can move using mouse.

P & O buttons used to switch between cameras. If there is SITL connected to the map then it will have its own camera and you can switch to it using P & W. then you can control it using W, S , A , D. You can press R to reset camera orientation.

The image shows 4 view ports the top left is from a world camera, then the top right is from a plane camera, the bottom left shows both a plane and a drone, then the bottom right is the drone camera.

 

The next image shows a quadcopter with a camera pointing forward. while the image below it shows a plane. These vehicles are defined automatically in the world as mavlink messages are received by the static webpage. 

Different vehicles should have different SYSID_THISMAV    

 

 

Running the simulation is very easy. you need to run udp2websocket.js using node. and use http-server to run the static website. That is it.

Then you need to run simulators or redirect data from a real vehicles. running SITL  could be like the following sample:



sim_vehicle.py  -j4 -v ArduPlane  -M --map --console --instance 80 --out=udpout:127.0.0.1:14550 --out=udpout:192.168.1.139:16450


Port 16450 is used by the 3DMapView and port 14550 I keep it for QGC.

 



Your are welcome to join to enhance this simulator.