Linux altında C kullanarak Step Motor Süren Program

C kullanark seri porta ulaşmayı sağlayan özgün kodu bu linkten de edinebilirsiniz. Aşağıdaki kod bunun biraz değiştirilerek step motor sürebilen hale getirilmiş versiyonudur.


//seri porttan step motor sürülmesi
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "adrport.h"

int main(int argc, char *argv[])
{
char LUT[16][4]= { "0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"};
int ff[]={8,4,2,1};
int bf[]={1,2,4,8};
int fh[]={8,12,4,6,2,3,1,9};
int bh[]={9,1,3,2,6,4,12,8};
char emir[]="0000";

int delay,step,i,j,k,MODE;

if (argc < 6 || argc > 6)
{
printf("'StepMotor needs 4 parameters for the serial port\n");
printf(" ie.Use 'StepMotor 0 forward full 50 10' to connect to /dev/ttyS0 and\n");
printf("forward 50 full steps with 10 ms delay\n");
return 0;
}

if (strcmp(argv[2],"forward")==0)
{
if (strcmp(argv[3],"full")==0) MODE=1;
else if (strcmp(argv[3],"half")==0) MODE=2;
}
else if (strcmp(argv[2],"backward")==0)
{
if (strcmp(argv[3],"full")==0) MODE=3;
else if (strcmp(argv[3],"half")==0) MODE=4;
}

if (OpenAdrPort(argv[1]) < 0) return 0;
step=atoi(argv[4]);
delay=1000*atoi(argv[5]);

for (i=0;i<step;i++)

{

switch(MODE)
{
case 1:
for (j=0;j<4;j++) emir[j]=LUT[ff[i%4]][j];
break;

case 2:
for (j=0;j<4;j++) emir[j]=LUT[fh[i%8]][j];
break;

case 3:
for (j=0;j<4;j++) emir[j]=LUT[bf[i%4]][j];
break;

case 4:
for (j=0;j<4;j++) emir[j]=LUT[bh[i%8]][j];
break;
}


k=WriteAdrPort(emir);
usleep(delay);
}


CloseAdrPort();

} // end main


Bu kodun doğru biçimde çalışabilmesi için ayrıca adrport.c ve adrport.h dosyalarına da ihtiyacınız olacak, onlar da sırasıyla da şu şekilde;

adrport.c




// adrport.c - Serial Port Handler
// Copyright MMI, MMII by Sisusypro Incorporated

// Permission is hereby granted to freely copy,
// modify, utilize and distribute this example in
// whatever manner you desire without restriction.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
#include "adrport.h"
static int fd = 0;

// opens the serial port
// return code:
// > 0 = fd for the port
// -1 = open failed
int OpenAdrPort(char* sPortNumber)
{
char sPortName[64];
printf("in OpenAdrPort port#=%s\n", sPortNumber);
sprintf(sPortName, "/dev/ttyS%s", sPortNumber);
printf("sPortName=%s\n", sPortName);

// make sure port is closed
CloseAdrPort(fd);

fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("open error %d %s\n", errno, strerror(errno));
}
else
{
struct termios my_termios;
printf("fd is %d\n", fd);
tcgetattr(fd, &my_termios);
// NOTE: you may want to save the port attributes
// here so that you can restore them later
// printf("old cflag=%08x\n", my_termios.c_cflag);
// printf("old oflag=%08x\n", my_termios.c_oflag);
// printf("old iflag=%08x\n", my_termios.c_iflag);
// printf("old lflag=%08x\n", my_termios.c_lflag);
// printf("old line=%02x\n", my_termios.c_line);

tcflush(fd, TCIFLUSH);

my_termios.c_cflag = B2400 | CS8 |CREAD | CLOCAL | HUPCL;

cfsetospeed(&my_termios, B2400);
tcsetattr(fd, TCSANOW, &my_termios);
/*
printf("new cflag=%08x\n", my_termios.c_cflag);
printf("new oflag=%08x\n", my_termios.c_oflag);
printf("new iflag=%08x\n", my_termios.c_iflag);
printf("new lflag=%08x\n", my_termios.c_lflag);
printf("new line=%02x\n", my_termios.c_line);*/
} // end if
return fd;
} // end OpenAdrPort

// writes zero terminated string to the serial port
// return code:
// >= 0 = number of characters written
// -1 = write failed
int WriteAdrPort(char* psOutput)
{
int iOut;
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
iOut = write(fd, psOutput, strlen(psOutput));
if (iOut < 0)
{
printf("write error %d %s\n", errno, strerror(errno));
}
else
{
printf("wrote %d chars: %s\n", iOut, psOutput);
} // end if
return iOut;
} // end WriteAdrPort

// read string from the serial port
// return code:
// >= 0 = number of characters read
// -1 = read failed
int ReadAdrPort(char* psResponse, int iMax)
{
int iIn;
printf("in ReadAdrPort iMax=%d\n", iMax);
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
strncpy (psResponse, "N/A", iMax<4?iMax:4);
iIn = read(fd, psResponse, iMax-1);
if (iIn < 0)
{
if (errno == EAGAIN)
{
return 0; // assume that command generated no response
}
else
{
printf("read error %d %s\n", errno, strerror(errno));
} // end if
}
else
{
psResponse[iIn<iMax?iIn:iMax] = '\0';
printf("read %d chars: %s\n", iIn, psResponse);
} // end if

return iIn;
} // end ReadAdrPort

// closes the serial port
void CloseAdrPort()
{
// you may want to restore the saved port attributes
if (fd > 0)
{
close(fd);
} // end if
} // end CloseAdrPort



adrport.h



// adrport.h
// Copyright MMI, MMII by Sisusypro Incorporated

int OpenAdrPort (char* sPortNumber);
int WriteAdrPort(char* psOutput);
int ReadAdrPort(char* psResponse, int iMax);
void CloseAdrPort();



Bu StepMotor.c kodumuzun derlenmesi de şu şekilde olmaktadır;

gcc -Wall -o StepMotor StepMotor.c adrport.c

StepMotor.c dosyasının içeriğinden de anlaşılabileceği gibi, kodu derlediğimizde oluşan StepMotor çalıştırılabilir dosyası komut satırından kullanılmakta ve bazı parametreler kullanmaktadır. Örnek bir çalışma şu şekilde olabilir;

StepMotor 0 forward full 50 10

Burada programın adından sonra gelen 0 (sıfır) port numaramız; bunlar linuxta /dev/tty0 /dev/tty1 gibi isimler alırlar. İkincisi motorun dönüş yönü, forward (ileri) ya da backward (geri) olabilir. Üçüncü opsiyon yarım adım (half) ya da tam adım (full) olabilir. 50 adım sayımız, 10 ise iki adım arasındaki milisaniye cinsinden gecikme süresidir.

Comments

Popular posts from this blog

Latex'te Denklem İçerisine Ufak Boşluklar Koymak

LaTeX'te Sunum Hazırlamak

Octave'da Grafik Çizdirme