Search This Blog

Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Monday, November 16, 2015

Fitting a curve to a function

ပေးထားတဲ့ curve တခုနဲ့ အကိုက်ညီဆံုး piecewise linear function တခုကို ရှာတဲ့ နမူနာ MatLab code တခုပါ။



%Fitting a curve to a piecewise linear function using least square method
%with the fixed start and end points

%clear command windows
clc;

%clear workspace
clear all;
%--------------------------------------------------------------------------
%Read curves
load Curve.dat;

%find lower left and upper right corners 
xD=Curve(1,:);
yD=Curve(2,:);
x0=min(xD); x1=max(xD); 
y0=min(yD);  y1=max(yD);
n=5;%order n i.e, the number of segments
FS=x1-x0;
global xs;
global yB;
global yE;
xs=(0:FS/n:FS);
yB=y0;
yE=y1;
%initialize coefficients (y values) to find 
%excluding the first and the last one
ys_initial=xs(2:n);
% options = optimset('MaxFunEvals',1000,'MaxIter',1000);
% LowerBoundC=y0;
% UpperBoundC=y1;
% ys =lsqcurvefit(@LineSeg,ys_initial,xD,yD,LowerBoundC,UpperBoundC,options);
ys =lsqcurvefit(@LineSeg,ys_initial,xD,yD);
%fixed start and end points
ys=[y0 ys y1];
%--------------------------------------------------------------------------
%Plot 
hFig1 = figure(1);
set(hFig1, 'Position', [600 100 500 300])
plot(xD,yD,':g','LineWidth',3,...
                'MarkerEdgeColor','b',...
                'MarkerFaceColor','b',...
                'MarkerSize',2)
hold on;             
plot(xs,ys,'-rs','LineWidth',1,...
     'MarkerEdgeColor','r',...
     'MarkerFaceColor','r',...
     'MarkerSize',4)
hold off;        
grid on;
%--------------------------------------------------------------------------


LineSeg.m
function [y] = LineSeg(ys,x)

global xs
global yB
global yE
L=length(x);
y=zeros(1,L);
ys=[yB ys yE];
LS=length(ys);
for i=1:L
    xt=x(i);
    %--------------------------
    %find segment
    for j=2:LS
        if(xs(j)>=xt) 
            break;
        end
    end    
    %interpolate
    x2=xs(j);
    y2=ys(j);
    y1=ys(j-1);
    x1=xs(j-1);
    y(i)=y1+(y2-y1)/(x2-x1)*(xt-x1);
    %--------------------------
end 

Saturday, September 19, 2015

Controlling Your Hardware from the Web Using Arduino

Arduino Ethernet shield 2 ကိုသုံးပြီး အင်တာနက်ကနေ သင့်ရဲ့ hardware တွေကို လှမ်းပြီး ထိန်းတဲ့အကြောင်း ဆွေးနွေးချင်ပါတယ်။ Arduino Ethernet 2 Library ကိုသုံးပြီး server အနေနဲ့ နမူနာတခု၊ client အနေနဲ့ တမျိုး စမ်းကြည့်ပါမယ်။ နောက်ဆုံး version Arduino IDE ကို သုံးဖို့လိုပါတယ်။ ဒီနမူနာမှာတော့ Arduino IDE 1.7.6 ကိုသုံးထားပါတယ်။ Arduino Uno ကို Ethernet shield 2 တပ်ထားတဲ့ပုံကို အောက်မှာ ပြထားပါတယ်။


Figure. Arduino Uno paired with Arduino Ethernet shield 2.


Thursday, September 10, 2015

Accelerometer LIS3DSH

LIS3DSH က STMicroelectronics ထုတ်တဲ့ 3-axis MEMS accelerometer တခု ဖြစ်ပါတယ်။ Full scale ကို ±2g ကနေ ±16g ထိ အမျိုးမျိုးရွေးလို့ ရပါတယ်။ အရွယ်အစားက လည်း 3mm x 3mm ပဲမို့ သေးသေးလေးပါ။ Digital output ကို SPI ဒါမှမဟုတ် I2C ကြိုက်တာသုံးလို့ရပါတယ်။ Supply voltage က 1.71 V ကနေ 3.6 V ထိပေးလို့ ရပါတယ်။ STEVAL-MKI134V1 adapter board လေး တခုဝယ်ပြီး စမ်းကြည့်ထားတဲ့ အတွေ့အကြုံ လေးအကြောင်း ပြောချင်ပါတယ်။


Figure. STEVAL-MKI134V1 - LIS3DSH adapter board for standard DIL24 socket.

Monday, March 16, 2009

String and ASCII Code Conversion in VB 2005

ကျွန်တော်တို့ အခါအားလျော်စွာ သုံးဖို့ လိုတဲ့ ASCII code နဲ့ string အပြန် အလှန် ပြောင်းလဲတဲ့ ဖန်ရှင်လေး တွေကို ရေးကြည့်ထား ပါတယ်။ အဲဒါ ကို A Visual Basic class to convert between hexadecimal, ascii, and text string on GitHub မှာ ယူနိုင်ပါတယ်။
'Author: Yan Naing Aye
'WebSite: http://cool-emerald.blogspot.sg/
'Updated: 2009 April 24
'-----------------------------------------------------------------------------
Public Class ClsMyStr
    Public Shared Function AsAsciiEncodedStr(ByVal CharString As String) As String
        Dim outS As String = ""
        Dim temp As String = ""
        Dim i As Integer = 0
        For i = 0 To CharString.Length - 1
            temp = "00" & Hex(Asc(CharString(i)))
            temp = Right(temp, 2)
            outS = outS & temp
        Next i
        Return outS
    End Function
    Public Shared Function AsSpacedAsciiEncodedStr(ByVal CharString As String) As String
        Dim outS As String = ""
        Dim temp As String = ""
        Dim i As Integer = 0
        For i = 0 To CharString.Length - 1
            temp = "00" & Hex(Asc(CharString(i)))
            temp = Right(temp, 2)
            outS = outS & temp & " "
        Next i
        Return outS
    End Function
    Public Shared Function AsAsciiDecodedStr(ByVal AsciiEncodedStr As String) As String
        Dim outS As String = ""
        Dim i As Integer = 0
        Dim l As Integer = AsciiEncodedStr.Length - 2

        If (AsciiEncodedStr.Length Mod 2) <> 0 Then
            l -= 1
        End If
        For i = 0 To l Step 2
            outS = outS & Chr(Val("&H" & AsciiEncodedStr.Substring(i, 2)))
        Next i
        Return outS
    End Function
    Public Shared Function GetAsciiEncodedStr(ByVal RawAsciiEncodedStr As String) As String
        Dim i As Integer = 0
        Dim c As String
        Dim cmd As String = ""

        For i = 0 To RawAsciiEncodedStr.Length - 1
            c = RawAsciiEncodedStr(i)
            If (Asc(c) >= &H30) AndAlso (Asc(c) <= &H39) Then
                cmd = cmd & c
            ElseIf (Asc(c) >= &H41) AndAlso (Asc(c) <= &H5A) Then
                cmd = cmd & c
            ElseIf (Asc(c) >= &H61) AndAlso (Asc(c) <= &H7A) Then
                cmd = cmd & Chr(Asc(c) - &H20) 'change to upper case
            Else
                'MessageBox.Show("Got invalid character.")
            End If
        Next i
        If cmd.Length < 2 Then
            cmd = "0" & cmd
        End If
        Return cmd
    End Function
    Public Shared Function DoubleQuote() As String
        Return ControlChars.Quote
    End Function
    Public Shared Function Byte2Text(ByVal byteArray() As Byte) As String
        Dim str As String = BitConverter.ToString(byteArray)
        Return str
    End Function
    Public Shared Function Byte2Str(ByVal byteArray() As Byte) As String
        'Dim str As String = System.Text.Encoding.ASCII.GetString(byteArray)
        Dim str As String = ""
        For i As Integer = 0 To UBound(byteArray)
            str &= Chr(byteArray(i))
        Next
        Return str
    End Function
    Public Shared Function Str2Byte(ByVal str As String) As Byte()
        'Dim ba() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
        Dim ba() As Byte
        Try
            ReDim ba(str.Length - 1)
            For i As Integer = 0 To UBound(ba)
                ba(i) = Asc(str(i))
            Next
        Catch ex As Exception
            ReDim ba(0)
            ba(0) = 0
        End Try        
        Return ba
    End Function
    Public Shared Function GetSignedDecimalText(ByVal RawStr As String) As String
        Dim i As Integer = 0
        Dim c As String
        Dim cmd As String = ""

        For i = 0 To RawStr.Length - 1
            c = RawStr(i)
            If (Asc(c) >= &H30) AndAlso (Asc(c) <= &H39) Then
                cmd = cmd & c
            ElseIf (Asc(c) = &H2D) Then
                If cmd.Length = 0 Then
                    cmd = cmd & c
                End If
            Else
                'MessageBox.Show("Got invalid character.")
            End If
        Next i        
        Return cmd
    End Function
End Class