Wednesday, 15 July 2020

Potts Model simulation code (Python)



import
numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import random
import copy

##################################################################################################
nx = 300
ny = 300
Temp=1

n_levels=3

fig = plt.figure()
data = np.zeros((nx, ny))
data=[[random.randint(0,n_levels-1) for i in range(nx)] for i in range(ny)]
data=np.array(data)
im = plt.imshow(data)
def init():
global data
im.set_data(data)



def animate(i):
global data
print(i)
data=next_state(data)
im.set_data(data)
return im

def next_state(data):
for i in range(5000):
n=random.randint(0, nx-1)
m=random.randint(0,ny-1)
next=random.randint(0,n_levels-1);
a=(data[m][(n-1+nx)%nx]==next)*2.0-1
b=(data[m][(n+1)%nx]==next)*2.0-1
c=(data[(m-1+ny)%ny][n]==next)*2.0-1
d=(data[(m+1)%ny][n]==next)*2.0-1

d1 = (data[(m + 1) % ny][(n+1)%nx] == next) * 2.0 - 1
d2 = (data[(m + 1) % ny][(n-1+nx)%nx] == next) * 2.0 - 1
d3 = (data[(m - 1+ny) % ny][(n+1)%nx] == next) * 2.0 - 1
d4 = (data[(m - 1+ny) % ny][(n-1+nx)%nx] == next) * 2.0 - 1

E=a+b+c+d+d1+d2+d3+d4

if E > 0:
data[m][n] = next;
else:
r=np.random.uniform();
if E <= 0 and r < np.exp(E) / Temp:
data[m][n] = random.randint(0,n_levels-1);

return data



anim = animation.FuncAnimation(fig, animate, init_func=init, frames=800,interval=1)

plt.show()



Thursday, 26 March 2020

Ising Model simulation Code

MATLAB code for the Ising model simulation is given in this post. Try Yourself.

You can access ready-made simulation of this in your phone with the following Android app https://play.google.com/store/apps/details?id=com.gaurtavm.randomprocesssimulation


MATLAB CODE:

a=200;  %height
b=200;  %width
T=1;    %Temprature
steps=10000000;
img=2*(randi(2,a,b)-1)-1;  % Random 2D array containing +1 and -1
for i=1:steps
    x=randi(a,1);
    y=randi(b,1);
    tmp=(img(mod(x-1-1,a)+1,y)+img(mod(x+1-1,a)+1,y)+img(x,mod(y+1-1,b)+1)+img(x,mod(y-1-1,b)+1));
    E=-img(x,y)*tmp;   %Energy
    r=rand;
    if E>0
        img(x,y)=-img(x,y);
    elseif E<=0 && r<exp(E)/T
        img(x,y)=-img(x,y);
    end
   
    if mod(i,10000)==0   %update image after 10000 steps
        imshow(((img+1.0)/2))
        fprintf("%d\n",i)
    end
end


OUTPUT:


Monday, 16 March 2020

My Android Apps on google play store

In this post, ill tell you about android apps I published and their use

Photo Noise Editor:  With help of this app you can add various kinds of noise to your photos. It is different from other apps in a way that you can pause the noise addition process at any time and save your photos. A small demo of app is given in the GIF image below.
App Link:  https://play.google.com/store/apps/details?id=com.gaurtavm.photonoiseadder




Virtual Line follower: This app lets you create a line follower inside your phone. This Line follower can have as many sensors as you want in a straight-line fashion.
The main purpose of this app is to make complex line followers easily. For example, it is a very hard task to make a PID line follower and tune Kp, Ki and Kd values but this application makes it easy. You can try those things in this app that are out of your budget for a practical line follower.
You can draw a path of your choice for the bot. That is a very time consuming and expensive task in practical line follower.
Blog page: https://crackeconcept.blogspot.com/2020/03/virtual-line-followerline-foll.html
 
The app is available on google play store. Link for the app is: https://play.google.com/store/apps/details?id=com.gaurtavm.virtuallinefollower








Probability Simulations: This app contains a number of coded simulations that are mostly related to probability theory. The animation helps to understand the process well. There are adjustable parameters for each simulation you can play with the values.  My motivation to make this app is to let the user visualize these processes easily through these simulations.
Currently Available simulations in the app are
- Discrete-time Markov Chain (DTMC)
- Ising Model 2D
- Random Walk 1D
- Random Walk 2D
- Diffusion-limited aggregation (DLA)
Link: https://play.google.com/store/apps/details?id=com.gaurtavm.randomprocesssimulation

Flexible Whiteboard: This app lets you draw on a small area on screen and view drawing on full screen. It is very useful for tablets where screen size is big. You don't have to take your hands everywhere to draw, just draw on a small area which is also adjustable.
Features:
- Zoom-In and Zoon-Out
- Instant sharing of drawing
- Adjustable drawing area
- ARGB brush control
- Brush width control
App Link: https://play.google.com/store/apps/details?id=com.gaurtavm.flexiblewhiteboard


Sunday, 15 March 2020

Virtual PID Line Follower

When you are making a PID line follower there are a lot of problems. How many sensors to use? What should be coefficients? What should be the distance between sensors for your problem? One cannot afford to buy so many sensors and changing distance between sensors is a very time-consuming process.
For best performance, you have to tune Kp, Ki, and Kd. Tuning these parameters of a PID line follower is a very hard task. You have to keep changing value and burn the code again and again to the microcontroller. With the help of this app, you can learn easily how to tune these values.


PID Algorithm:

Initialize I=0,error=0,last_error=0
1. Read all sensors output s0,s1,s3...,sn which can be either 0 or 1. 


2. Calculate current Position using.
 if (s0+s1+s2+...+sn>0) then current_position=(s0*b0+s1*b1+s2*b2+..+sn*bn)/(s0+s1+s2+...+sn)

else current_position=s0*b0+ s1*b1+ s2*b2+...+ sn*bn
Where b0, b1, b2,..., bn are sensor coefficients. These sensor cofficients are decided by us. For example, for 5 sensors cofficients can be 
b0=10, b1=20,b2=30,b3=40,b4=50 ,For these cofficients our set point shoud be 30 which is the middle sensor cofficient value.

3. Find error using,  
error = set_position- current_position
set_position contains the value where the error should be zero.

4. Set I=I+error

5. pid_error = kp*error + ki*I + kd*(error-last_error)

6. Remember current error in last_error variable. last_error = error
7. Set motor speeds:  
if (pid_error<0) then left_motor=max_speed, right_motor=max_speed+pid_error

else left_motor=max_speed-pid_error, right_motor=max_speed

8. Limit output speed. if(left_motor>max_speed) then left_motor= max_speed
if(left_motor< -max_speed) then left_motor= -max_speed 
if(right_motor> max_speed) then right_motor= max_speed 
if(right_motor< -max_speed) then right_motor= -max_speed

Saturday, 14 March 2020

Virtual Line Follower

This post is about the Virtual line follower android app. This app lets you learn about line follower without any complex circuit. In this app, you can adjust parameters like the number of sensors, the distance between sensors which is a very time consuming and costly task to do with a practical line follower. You can write your own logic and run the robot.
If you don't have an android device then you can download any Android emulator available and run this app.

Let us look at the geometry of the robot.
Following parameters of line follower bot are adjustable
1. The number of sensors. You can use as many sensors you want in a straight array fashion.
2. Distance between sensors (x): Distance between sensors can be chosen in terms of pixel length. The distance between consecutive sensors is the same for all sensors.
3. Distance between motors (L) can also be changed.
4. The distance of the sensor array from the motor axis can be changed.
5. Sensors are arranged from left to right as shown in the figure below.
Line Follower Geometry


The sensor state is written from the leftmost sensor to the rightmost sensor. For example for two sensors S0, S1 possible states are 00, 01, 10, 11.
Motor output can be from -1 to +1. Where -1 is for maximum reverse speed and +1 for maximum forward speed.

Examples to try inside the app:

1) Two sensors:

for "00" state go forward by left motor=1 and right motor=1 
for "01" state go right by left motor=1 and right motor=0 
for "10" state go left by left motor=0 and right motor=1  
for "11" state go forward by left motor=1 and right motor=1 

Inside App, the following should be input
Sensor state:
00
01
10
11

Left Motor:
1
1
0
1

Right Motor:
1
0
1
1

2) Three sensors:

for "001" and "011" states go right by left motor=1 and right motor=0 
for "110" and "100" state go left by left motor=0 and right motor=1  
for "010" state go forward by left motor=1 and right motor=1 
Otherwise, go forward in simple robot. 

Inside App, the following should be input
Sensor state:
001
011
100
110
010

Left Motor:
1
1
0
0
1

Right Motor:
0
0
1
1
1

For PID go here https://crackeconcept.blogspot.com/2020/03/virtual-pid-line-follower.html


the app is out! Try now.

Monday, 10 February 2020

How to make dead Lead Acid Battery work again

In this post, I am going to share my experience of making a dead lead-acid battery work again. Maybe trick in this post will help you to recover your lead acid battery.
First, let me share my experience. I had a 12 V lead-acid battery whose voltage went down below 9 volts and when I try to charge this battery its voltage suddenly goes above 14 volts. If you're facing this problem than the solution in this post might help you. I made the same battery work 2 times using the method mentioned in this post and the battery is still in working condition when I am writing this post.
So what should we do in such a situation? Logically thinking current going inside the battery is too much and this too much current makes the voltage go high suddenly. So how to reduce this current? How much current should we provide for charging the battery?
To solve this problem without using a complicated electronic circuit. We can simply place a suitable load parallel to the battery. This load should maintain a voltage between 12-13 V across the battery. So what will happen from this? The battery will take the current it needed and the load will take extra current. This load can be a dc motor or any resistive load.
As the battery start recovering it will take more and more current and current through load will automatically decrease. It may take from half an hour to a few hours for the battery to recover. Once the battery start taking more current you can remove the load and let the battery charge.

Thursday, 6 February 2020

Hanging problem and back Emf

In this post, I am gonna share a hanging problem of a microcontroller that is related to back emf. I faced this problem when I was using the Arduino microcontroller in a high-speed switching circuit. The microcontroller stops working a few seconds after switching on the circuit. No damage to the microcontroller in my case, after reset microcontroller works in the same way.

 When we use devices like relay or motors with microcontrollers and operate them through a driver IC. During switching on and off there is back emf generated. This back emf can cause damage to the microcontroller.

When the switch opens at t=0 the current 'I' sees high resistance. This high resistance path can be any insulator or air itself.



What is back emf? Inductor has a property that current cannot change instantaneously through them. Due to this property when we remove supply from inductor the current flowing through it needs a path. When it finds no low resistance path large voltage spike is produced. Why? It takes the high resistance path which can be insulator or air. This high voltage spike can damage our equipment.

How to solve this problem? To solve the problem we should provide a low resistance path to this current. When this current goes through a low resistance path, the voltage developed becomes negligible. Usually, in circuits high-speed diode is used for this purpose. See the figure below how current passes through the diode when the switch opens at t=0.



Monday, 6 January 2020

Two sensor code for line follower using feedback


This post is about two sensor version of line follower bot with feedback idea. Go below for main post link.
With two sensors you will find that bot has oscillating behaviour around the line. Which is not good as sometimes bot will never come back to the line. The idea is worth trying. Just modify your three sensor code and see the results.

//code is

int rightmotor1=12;
int leftmotor1=10;
int rightmotor2=13;
int leftmotor2=11;


int sensorleft=14;
int sensorright=16;


int l1=1;
int l2=1;

int f1=0;
int f2=0;

//////////////////////////////////////

void setup()
{
 pinMode(rightmotor1,OUTPUT);
 pinMode(leftmotor1,OUTPUT);
 pinMode(rightmotor2,OUTPUT);
 pinMode(leftmotor2,OUTPUT);

 pinMode(sensorleft,INPUT);
 pinMode(sensorright,INPUT);

}
///////////////////////////////////////////////////

void loop()
{
l1=digitalRead(sensorleft);
l2=digitalRead(sensorright);

/////////////////////////////////////////////////////////////////////
if(l1==1&&l2==1)   //feedback when comes all sensor on white
{
l1=f1;
l2=f2;
}

////////////////////////////////////////////////////////////////////////////
if(l1==0&&l2==1)   //left turn
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==0)  //right turn
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==0&&l2==0)  //go forward straight
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
///////////////////////////////////////////////////////////////////
f1=l1;
f2=l2;        //memory variables
////////////////////////////////////////////////////////////////////
}




Here f1 and f2 are our feedback variable or we can say memory variables.

This idea best works with three sensors. Checkout three sensor post here https://crackeconcept.blogspot.com/2014/03/3-sensor-line-follower-ardiuno.html

Hope this post will help you. Thank you.