Saturday, October 22, 2016

Linux command tips and tricks

For beginner, it is useful to know a few handy commands which can help to ease the linux learning process.

Linux is too big but there are a few commands which are commonly used.

From the website, vbird has a tutorial or these useful command but it is in Chinese, so i rewrito and simplified it in english.

Telnet
It is same as ssh and the method of using is "telnet xxx.xxx.xxx.xxx" (x is ip address)

Shutdown
"shutdown -h now" - shutdown now
"shutdown -r now" - reboot now
"shutdown -h 20.30" - shutdown at 20.30
"shutdown -h +10" - shutdown after 10 minutes

Reboot
Restart the computer. "reboot"

Cp
Copy file
"cp file1 file2" - copy file1 and rename it to file2

& and [Ctrl] + [z]
These two methods let the task running in background. Useful when you are processing exceptional long file.
Example of using: "make all &" or "make all + [Ctrl] + [z]" - it will run in background.

Fg
Show current running task. "fg"

Ps -aux
Show every task and pid. "ps -aux"

Kill
After we get a task's pid, use kill to terminate the task. "kill xxxx" (x is pid number)

Cd
Change the working directory.
"cd" - back to home
"cd ~" - back to home
"cd .." - up one level directory
"cd /usr/bin" - change the working directory to /usr/bin

Pwd
Show your current working directory. "pwd"

Ls
List out all the files
"ls -l" - list out all the details of files
"ls -a" - list out all files including hidden files.
"ls -al"  list out all files detail including hidden files.

Cat
Show the status of register or content of a file. "cat /sys/bus/usb/devices/1-2/power/control" or "cat abc.txt"

Mkdir
Create a folder. "mkdir abc"

Rm
Delete a file or directory
"rm abc.txt" - delete the abc.txt
"rm -rf abc" - delete the file abc and all contents inside abc

Mv
Move a file to different directory or change the name.
"mv abc.txt ~/" -  move the abc.txt to home
"mv abc.txt efg.txt" - change the name of abc.txt to efg.txt

Find
Useful to find a file. "find /path/name/ -name file_name"

Whereis
From the bash command history, search the particular file. "whereis file_name"

Chmod
Change the nature of a file. "r - 4, w - 2, x - 1". Needed when you want write scripts.
"chmod 755 scripts_name"

Df
List out all memory detail. "df"

Du
List out all file size. "du"

NOT YET FINISHED, WILL ADD IT WHEN I HAVE TIME. MAYBE WILL POLISH IT A BIT

Thursday, September 15, 2016

low cost isolated current sensing IC - ACS710

For low cost isolated current sensing, Allegro hall effect current sensing IC has come into picture. Allegro has produces a series of low cost current sensing IC.

ACS710 has been in market for quite some time and it is suitable for renewable energy application as in described in “AN296110-Current Sensing for Renewable Energy”.

ACS710 has 1 mΩ sensing resistance for low power loss, user selectable overcurrent fault level, provided in surface mount SOIC16 package, operating temperature is from -40ºC to 125ºC.

Best part is no isolated voltage supply is needed. (Save the cost)

Maybe the only drawback is the low voltage isolation.
The voltage isolation characteristics that had been conducted by Allegro.
Test Characteristic
Rating (VAC)
Dielectric Strength Test Voltage (60s)
3000
Working Voltage for Basic Isolation
277

Neverthelss, for most applications, the continuous working voltage of 277VAC is more than enough.

ACS713 is provided in several sensing range such as follow:
Sensing Range (Ampere)
Sensitivity at Vcc=5V (mV/A)
±6
151
±10
85
±12.5
56
±25
28

Supply voltage – 3V to 5.5V.
Signal bandwidth – 120kHz

From the above picture, you can see that for the full operation of 120kHz, filter capacitor of 0.1nF or less is needed. 

Of course, you can increase the filter capacitance, the higher the filter capacitance, the lower the bandwidth. Good point is the RMS noise also significant reduced.

ACS713 has the over-current detection mechanism and the settings/calculation as follow
For the setting of overcurrent detection,
Version 6BB and 10BB, the formula is as follow:
VOC = 1.17 × Sens × | IOC |

Example,
For ACS710KLATR-6BB-T, if required overcurrent fault switchpoint is 10 A, and VCC = 5 V, then the required VOC can be calculated as follows:
VOC = 1.17 × Sens × IOC = 1.17 × 151 × 10 = 1767 (mV)


Version 12CB and 25CB, the formula is as follow:
VOC = Sens × | IOC |

Example,
For ACS710KLATR-25CB-T, if required overcurrent fault switchpoint is 50 A, and VCC = 5 V, then the required VOC can be calculated as follows:
VOC = Sens × IOC = 28 × 50 = 1400 (mV)


One more thing to consider when using this IC is the temperate effect toward the sensing accuracy.
The accuracy of the ACS710 against the temperature variation is as follow:
Although at constant 25 Celsius, the output voltage also has some variation.
However, for such low price, this temperature variation performance is acceptable. 

The brother ACS712 had been very popular among hobbyists, 
this not so new newbies (ACS713) can be worth trying, I am designing a breakout module for it.
Not sure when going to finish it. If I did finish it, I will test it even further.

Tuesday, September 13, 2016

Blinking LED in pattern

Bob Blick arguably the first person who come out with the propeller clock. It is a 7 led that spin on a rotor and giving the illusion effect of numbers (clock time) in the air.
Image result for bob blick propeller clock
Original link: seem to be expired
Another link: Still active

It is around 14 years ago.
Now the internet is flooded with propeller led imaging projects. Even the market is selling the propeller led image device at relatively cheap price.

Well, cheap but it is not satisfying until you diy it yourself.

Basic ingredients:
leds - 8
mcu - use the development kit (pic18f4553)
power supply - source from pc usb port
jumper wire - 9
propeller - use your hand, moving up and down

Problems encountered:
I am using the ultra bright led, plus cant find suitable resistor. So no limiting current resistor is used and the ultra bright is really annoying. Pain in the eye when you have direct contact with it.
In the end, i use the phone to do video recording and monitor the output afterwards.

Hardware connection:

my development board has output pin for lcd 16x2, so direct connect from lcd 16x2 pins to breadboard is the simplest way.

Now, start to design my character:
Each character is at the 8x5 pattern configuration. Below is my designation 'A', 'B', 'C'.

Character ‘A’

0b00000,
0b01110,
0b10001,
0b10001,
0b10001,
0b11111,
0b10001,
0b10001
Character ‘B’

0b00000,
0b11110,
0b10001,
0b10001,
0b11110,
0b10001,
0b10001,
0b11110

Character ‘C’

0b00000,
0b01110,
0b10001,
0b10000,
0b10000,
0b10000,
0b10001,
0b01110

And then, convert it to hexadecimal (just for better view and shorter code)
A - 0x3E, 0x48, 0x48, 0x48, 0x3E,
B - 0x36, 0x49, 0x49, 0x49, 0x7F,
C - 0x22, 0x41, 0x41, 0x41, 0x3E,

For each character, I will add some blank spaces, just to distinguish each of the character,
And I will get something like this:
 rom char string[30] = {  
      0x3E, 0x48, 0x48, 0x48, 0x3E,   
      0x00, 0x00, 0x00, 0x00, 0x00,   
      0x36, 0x49, 0x49, 0x49, 0x7F,   
      0x00, 0x00, 0x00, 0x00, 0x00,   
      0x22, 0x41, 0x41, 0x41, 0x3E,  
      0x00, 0x00, 0x00, 0x00, 0x00,  
 }  

Next is the blinking pattern is single direction or bi direction,
single direction - blink according to the data in array from 0 to 30, then start from 0 again.
bi direction -  blink according to the data in array from 0 to 30, then count back, from 30 to 0.
 #ifdef UNIDIRECTION  
           i++;  
           if(i > 30) i = 0;  
 #endif  
 #ifdef BIDIRECTION  
           if(direction == 0){  
                i++;  
                if(i > 30) direction = 1;  
           }  
           else{  
                i--;  
                if(i < 0) direction = 0;  
           }  
 #endif  

I didnt know which solution is the best, so up to the user to decide.

3rd is the delay timing, using the try and error method, in the end i use 2.2 mili second.
Maybe not so accurate because I am using the delay loop instead of interrupt.

4th, shaking your hand "up and down" or "left and right", preferably is "left and right". However, moving your hand "up and down" can be more consistent than "left and right".

5th, face the mirror, move the hand at different timing until you can get the correct timing. This will be tiring.

Output:

Well, the message just came out wrong. so wrong....
but i dont have other pic. so.....dont misunderstood.

Conclusion:
It is harder than it look to get the correct moving timing using your hand.
And I actually took video, then browse the video in laptop, and pause every moment until i get the visible and clear character. Yeah, I cheat.
I will let it be like this for current stage. I will improve it at next blog (if i have time).

Project file and source code: LINK



Sunday, July 17, 2016

RTOS - FreeRTOS - first look - ARMFLY tutorial

Real time operating system has been a crucial task scheduler in large scale microcontroller project.  It allows the development of project to be divided into smaller part and each part is independently develop maintain.

Furthermore, the price of microcontroller is becoming cheaper and equipped with better processing power, RTOS is an additional good feature to be implemented in microcontroller. 

The low cost development kits have ease the learning process of embedded system. For example, the STMicroelectronic series development kits are cheapest in the market (currently), the microcontroller attached with development board is equipped with a lot of peripherals like ADC, DAC, PWM, timer, usb, can, serial communication, etc. It also come with on board debugger/programmer which is helpful in debug the program. Above all, it has large memory footprint (way more that enough).

Why let the RAM and flash go wasted, when you can waste it on RTOS. (Additional RAM is required to allocate stack size for task.)

There are a lot of free RTOS in the market. Previously I have tried OSA rtos for it's low memory footprint and suitability for PIC18f. In fact, I have written three OSA learning tutorials - 1, 2 and 3.

Freertos is an another alternative for microcontroller.

It is widely used in the commercial products, but not much company acknowledge it (freertos forum). One of the notable product that had been using it and publicly acknowledge the usage of freertos is pebble watch.

The vast user base has proven the stability of freertos, but freertos has it learning curve. The source code of freertos is readily available and well organised. Despite that, the source code has low level readability and the official document requires certain fee.

Simple starting guide is desirable to get started with freertos. The best tutorial is always the official website to learn about the function of freertos.

After some googling and baidu, I found one comprehensive tutorial written in Chinese by one China company "ARMFLY" 安富莱电子.

ARMFLY is making ARM development kit particularly on STM32. the good thing about their development kits is they provide tutorial and support in forum. It is also worth mentioning, ARMFLY is selling their boards at taobao.

Beside freertos, ARMFLY had provides tutorials in UCOS, embos, USB, Lwip, and RTX.

The ARMFLY freertos tutorial has 145 examples (project files is available for download) and 1200 pages documents. Link

ARMFLY has provides 145 examples for three of it's developments board which are
STM32F429,
STM32F407 and
STM32F103.

The good thing about this tutorial is step by step guidance provided with image to show the reader exact step instead of reading line by line. ARMFLY is using IAR and MDK compiler in their tutorial, so it is the best to get started with these two compilers.

The downside of this tutorial is written in Chinese. So it is an added advantageous to the reader who know Chinese.

It is worth mentioning that tutorial given is not tied to their development boards. You can apply the example given to any STM32 development boards as long as long the microcontroller core is supported by freertos. Else you have to do the porting yourself.

For the project source code given, maybe you need to change the IO port configuration to make the project workable.

Although there are some English tutorials on web, but there are basically not even one tutorial that provides spoon feed guidance. So this Chinese version is crucial for self learner and people who has no prior experience on RTOS.

Now, let have a look at the ARMFLY board, STM32-V5
The specification also quit amazing:
MCU:
STM32F407IGT6, 1M Flash, 192KRAM
Crystal:
25MHz for main clock, 32768 for RTC
Additional Memory:
2M SRAM, 16M Nor Flash, 128M NADN Flash, 16KEEPROM,8M Flash (No idea for what purpose)
Connectors:
1 Micro SD, 2 CAN2.0, 1 RS232, 1 RS485, 1 USB device, 1 USB host, 2 LAN, Audio, PS 2
ICs:
CODEC (WM8978), AM/FM (Si4730), accelerometer and MEMS gyro (MPU6050), Magnetometer (HMC5883L), Light sensor (BH1750FVI), Barometer (BMP085)
Additional:
Infrared transmitter, Infrared receiver, 1 CR1220 battery, 4.3 inches TFT display

It also reserved some pin for modules, guess you just have to buy it. Basically, the on board ICs and connectors have provided a good learning environment as you do not need to scout for additional components and do your own wiring. Best of all, after sales technical support is available at their forum.

Download link for tutorials:
STM32F429: tutorial, source code
password:bs5g

STM32F407: tutorial, source code
password:8fi7

STM32F103: tutorial, source code
password: sy7f

password is required for download the source code.

Friday, July 8, 2016

Linux learning - References and some notes

Recently I just started the learning journey of Linux. I am a window person, and have been pampered by the user friendliness of window 7, window xp. Getting started in Linux - Ubuntu has been quit a challenge for me. Although Ubuntu come with GUI, but the settings and software installation must be done in command window.

Knowing some useful commands is crucial in Linux survival kits.

As always I like to take note the essential and almost important functions.
1. cd - always take note of your working directory. ~ is home. / is top level directory. .. is back on top one level.
2. ls - list out all the files.
3. sudo - work as administrator. If you have a command, that you are pretty sure it is working, but you see error. Add sudo in front of command.
4. esc - press esc to go to command part in editor.
5. :q - quit the editor.
6. :q! - quit the editor without saving any changes.
7. :x, :wq - save and exit editor.
8. tab - press tab to auto complete your folder/file name.
9. shift + insert - paste your sentences in ssh.
10. highlight - copy, double click to highlight sentences in command window.
11. mouse right click - paste your sentences in ssh.
12. mv - move the folder or change name.
13. cp - copy


Useful software:
1. tmux - terminal multiplexer, useful when ssh is in used.
2. samba - ssh with gui.
3. putty - ssh client.
4. mtputty - simpler and more convenient package of putty.

tmux tips:
- to enter tmux: tmux
- to attach a tmux session: tmux a -t 0, 0 is the tmux session
- to see tmux session: enter tmux first, then only type "tmux ls"
- to detached a tmux session: ctrl + a + d
- to split screen horizontally: (1) tmux selectp -t 0, (2) tmux splitw -h -p 50
- to split screen vertically: (1) tmux selectp -t 0, (2) tmux splitw -v -p 50

Useful links:
1. Vim Commands Cheat Sheet - Easy to understand description.
2. Unix/Linux Command Reference - List of commands printable version.
3. Linux function description -  Good explanation.



Monday, May 23, 2016

osa rtos - 3rd switch debounce

Switch debounce is a common problem faced when playing with digital circuit. It is a condition where single pressed by user would appears multiple presses.

Usual software method used to avoid this is by taking multiple samples at certain time interval.

 if (time_inverval == 10ms)          // run this function every 10ms  
 {  
      if (pin1 == HIGH)              // check the pin status  
      {  
           pin1_counter++;  
      }  
      else  
      {  
           pin1_counter--;  
      }  
      if (pin1_counter>=10)          // confirm the samples for 10 times  
      {  
           pin1_counter = 0;          // clear the pin1_counter after 10 samples  
           pin1_flag = 1;  
      }  
 } 

Or you can use extra diode and capacitor to filter out the debounce glitches as shown at labbookpages. The labbookpages actually shows solutions from A Switch Debouncer Circuit, Software Debouncing, to Digital Switch Debouncing, to Switch Debouncing ICs. Nevertheless, extra circuitry mean extra cost, it is better to settle everything in software. Only a few lines are needed.

This blog (or my osa rtos learning diary) is to show how to implement switch debounce in rtos. The solution is shown at osa rtos at "button" section but I never really understand it. So I decide to try it out.

From osa rtos website:

        //------------------------------------------------------------------------------
        //  Wait for key press (with debounce)
        //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(!pin_BUTTON);
            OS_Delay(40 ms);
        } while (pin_BUTTON);

The function use the OS_Cond_Wait to wait for button is pressed then delay it for 40ms to skip the debounce signal.

After that, wait for the key to be released.

        //------------------------------------------------------------------------------
        //  Wait for key release
        //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(pin_BUTTON);
            OS_Delay(40 ms);
        } while (!pin_BUTTON);
 
 
    }

The implementation is likely the same, so user source code should be placed in between "wait for key press" and "wait for key release" (I assume).

So I add one flag for button pressed, so my overall code for taskButton is as follow:


#ifndef TASK_BUTTON_H
#define TASK_BUTTON_H

#include <osa.h>
#include <p18cxxx.h>

#define mInitSW1()     {TRISBbits.TRISB0=1;}
#define mInitSW2()     {TRISBbits.TRISB1=1;}

#define mSW_1    PORTBbits.RB0
#define mSW_2    PORTBbits.RB1

extern unsigned char flag_button1;
extern unsigned char flag_button2;

void taskButton1(void);
void taskButton2(void);

#endif /* TASK_BUTTON_H */

The taskButton function is:


void taskButton1(void)
{
 mInitSW1();
 flag_button1 = 0;
 
    for (;;) {
 //------------------------------------------------------------------------------
 //  Wait for key press (with debounce)
 //------------------------------------------------------------------------------
        do
        {
            OS_Cond_Wait(!mSW_1);
            OS_Delay(2);   // delay 40 ms
        } while (mSW_1); 

 //------------------------------------------------------------------------------
 //  Raise a flag
 //------------------------------------------------------------------------------
  flag_button1 = 1;
 
 //------------------------------------------------------------------------------
 //  Wait for key release
 //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(mSW_1);
            OS_Delay(2);
        } while (!mSW_1);    
    }
}

I put a video to show how the whole program is working. The led blinking, 16x2 display and button.



Project file is downloadable at here.

Sunday, May 8, 2016

My Tools



For the past one year, I have been buying components, tools. It started by buying some development kits and modules, then some IC components and breadboard, when Taobao is promoting the 2rmb delivery fee to Malaysia, I bought a soldering station and multimeter. Along that, I am purchasing electronic components from Element14 and RS electronic for my design.

My room is very messy. Seeing the components boxes laying on the floor, the development kits are stacking in the drawer, and my tools are scattering here and there.

I decide to tidy up the room and just for fun I am showing current tools that I own.


TOOLS
Buy From
1.
31 in 1 electronic tool set
Taobao
2.
Test pen
Cytron
3.
Legendary PICKIT 2
Element14
4.
Tweezers
Taobao
5.
Rolson wire cutter
Ace Hardware
6.
Victor multimeter VC890C
Taobao
7.
Victor multimeter VC830L
Cytron
8.
Solder sucker
Taobao
9.
Kasadl soldering station KSD 936
Taobao

I just ordered some component boxes from Taobao, I will show my components arrangement in next post.