Sunday, June 25, 2017

Julia - Language - Writing and Reading files

Julia has an open function using which you can write to file and read from file. Here, we will first create a file named "myfile.txt" and then read from it. 

Step 1: Writing to a file:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> fopen = open("myfile.txt","w")
IOStream(<file write>)

julia> write(fopen,"Hello World! Welcome to Julia"*"\n")
30

julia> close(fopen)


Step 2: Reading from files:

julia> ### reading file now

julia> fopen = open("myfile.txt","r")
IOStream(<file write>)

julia> fread = readlines(fopen)
1-element Array{String,1}:
 "Hello World! Welcome to Julia"

julia> close(fopen)

julia> fopen = open("myfile.txt","r")
IOStream(<file write>)

julia> fread = readstring(fopen)
"Hello World! Welcome to Julia\n"

julia> close(fopen)

Step 3: Let us now check for the presence of the file. We execute OS commands from within Julia:

shell> ls -l
total 8
-rw-r--r-- 1 ubuntu ubuntu   30 Jun 25 20:30 myfile.txt

julia> 

We have seen now that, writing of file has been successful. Also, we are able to read from it. 

The following are the read and write modes available in Julia Language:


Mode Description
r read
r+ read, write
w write, create, truncate
w+ read, write, create, truncate
a write, create, append
a+ read, write, create, append



Saturday, June 24, 2017

Ubuntu - Install Application - Beyond Compare



Beyond Compare is a file comparing Utility. To Install it in Linux Mint / Ubuntu:

Step 1: Download Beyond Compare from website:

$ wget http://www.scootersoftware.com/bcompare-3.3.13.18981_i386.deb
--2017-06-24 06:59:47--  http://www.scootersoftware.com/bcompare-3.3.13.18981_i386.deb
Resolving www.scootersoftware.com (www.scootersoftware.com)... 72.32.90.251
Connecting to www.scootersoftware.com (www.scootersoftware.com)|72.32.90.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10447378 (10.0M) [application/octet-stream]
Saving to: ‘bcompare-3.3.13.18981_i386.deb’

100%[==============================================================================================================================>] 1,04,47,378 1.96MB/s   in 13s    

2017-06-24 07:00:04 (766 KB/s) - ‘bcompare-3.3.13.18981_i386.deb’ saved [10447378/10447378]

/opt/softwares $ 


Step 2: Install it using dpkg:

$ sudo dpkg -i bcompare-3.3.13.18981_i386.deb 
Selecting previously unselected package bcompare.
(Reading database ... 166740 files and directories currently installed.)
Preparing to unpack bcompare-3.3.13.18981_i386.deb ...
Unpacking bcompare (3.3.13-18981) ...
Setting up bcompare (3.3.13-18981) ...
OK
Processing triggers for shared-mime-info (1.2-0ubuntu3) ...
Processing triggers for gnome-menus (3.10.1-0ubuntu2) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu1) ...
Processing triggers for mime-support (3.54ubuntu1.1) ...
 /opt/softwares $ 

Step 3: Check for installation:

/opt/softwares $ which bcompare
/usr/bin/bcompare
 /opt/softwares $ 

Step 4: You can now launch it, either by searching it in the menu. Or, you can launch it from terminal by typing - bcompare at the prompt:

/opt/softwares $ bcompare
/opt/softwares $ 

Beyond Compare is now installed in Linux Mint / Ubuntu.

Friday, June 23, 2017

Julia - Command Line - Executing Shell commands at the prompt

In Julia Language, keying in semi-colon (;) at the prompt (julia>) will change the prompt to (shell>) in which we can execute shell commands.


$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> 

shell> 

julia>


To return to the julia prompt (julia>) just key in backspace. Of course, pressing the return key will take you the prompt in the next line.

Julia - Language - Installation in Ubuntu

Visit JuliaLang website and download the current stable release: https://julialang.org/downloads/


Step 1: Open Terminal and download Julia:

$ wget https://julialang-s3.julialang.org/bin/linux/x64/0.6/julia-0.6.0-linux-x86_64.tar.gz

Step 2: Uncompress the Archive:

$ tar -zxvf julia-0.6.0-linux-x86_64.tar.gz

Step 3: After uncompressing the archive - the following folder was created:

$ ls -l
total 1686492
-rw-rw-r-- 1 ubuntu ubuntu   66543012 Jun 20 00:25 julia-0.6.0-linux-x86_64.tar.gz
drwxr-xr-x 7 ubuntu ubuntu       4096 Jun 19 19:28 julia-903644385b
$

Step 4: Move this folder to a convinient location:

$ cd /opt
/opt$

/opt$ mv /home/mhc/Downloads/julia-903644385b .
/opt$

Step 5: Create a softlink for Julia in /usr/bin - this is so that we can run it from any location:

$ cd /usr/bin
/usr/bin$ sudo ln -s /opt/julia-903644385b/bin/julia julia
/usr/bin$

Step 6: Run julia:

$ cd ~
~$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> 


Monday, June 19, 2017

Julia - Programming Language - ans variable

In an interactive session of Julia programming language. When you type an expression at the prompt, say, 1 + 5 - the output is displayed at the prompt.

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.11 (2015-07-27 06:18 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  x86_64-linux-gnu

julia> 1+5

6

We can access this output using the variable "ans". This is because the result gets stored in the "ans" variable. 

julia> ans
6

Now, consider the below expression, in which we are explicitly storing the result in a variable, "var":

julia> var = 2 + 3
5

julia> var
5

julia> ans
5

julia>

In such a case, the answer of the expression is stored in both "var" and the "ans" variables. 

Caveat:

The binding of the result to the "ans" variable happens only during interactive sessions. It does not happen in other ways. 



Sunday, March 12, 2017

Arduino - Interface with HC-SR04 Ultrasonic Sensor

Ultrasonic sensors are used to measure distances from obstacles. We can say that they are like mini-radars. We are using the sensor: HC-SR04

We are building the circuits based on schematics in the following blog:
http://arduinobasics.blogspot.in/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html




Step 1: Connect the Ultrasonic Sensor to the Jumper Cable (Ground + VCC)




Step 2: Connect the corresponding Jumper Cable ends to Arduino UNO R3 board at pins referring to 5V and GND.




Step 3: Connect Pins Tr1g and Echo on the Ultrasonic sensor with Jumper Cables




Step 4: The other end of the jumper cables connect to Pin 7 and Pin 8 Respectively on the Arduino.





Step 5: Ultrasonic measures the nearby distance:





Step 6: Measurement displayed on Graph in cm:




Step 7: Ultrasonic measuring farther object:





Step 8:
Measurement displayed on Graph in cm. We can observe the shift in readings:




Code for running the Ultrasonic sensor:

/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8

 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
 on 10 Nov 2012.
 */


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 }

 //Delay 50ms before next reading.
 delay(500);
} 


Ardiuno - Run Program to operate Proximity Sensor

Step 1: Connect the IR Proximity Sensor to the jumper cables:
Ground should connect with Ground on the Arduino Board
5V should connect with 5V on Arduino Board.




Step 2: We can observe in the pic below,  our Arduino UNO R3 is having all female pins:




Step 3: Hence , we need to use berg sticks to convert it to male pin. This is because, we use the jumper cable to connect to other IC's and Jumper cable are having female pins.




Step 4:  Insert the Berg Sticks into Female Pins of Arduino:






Step 5: Connect the jumper cables such that the 5V cable from the IR proximity sensor connects to the 5V on the Arduino UNO R3 board and GND from the Sensor connects to the GND on the board:




Step 6: Connect Arduino Uno R3 board to the computer using cable.





Step 7: The Red LED on the proximity sensor glows when the finger obstructs the path of the sensor.




Step 8: The Red LED stops glowing on the finger being removed from the path of the IR proximity sensor.




Reference: http://makezilla.com/2016/05/11/obstacle-avoider-robot-using-arduino-uno-and-ir-proximity-sensor/