==========================================================================================
AVR
==========================================================================================
<AVR1280>
=모터제어
-AX-12+
http://itsmart333.tistory.com/attachment/cfile8.uf@2470124A559C9BF4096E79.pdf
http://support.robotis.com/ko/techsupport_kor.htm#product/dynamixel/dxl_mx_main.htm
http://support.robotis.com/ko/
-AX-12+ => 스텝핑모터(자석에 끌려서 동작) - (자동차 계기판 RPM...)
(232 - TTL)
UART TXD

=
<STEPMOTOR.H>
#ifndef __STEPMOTER_H__ #define __STEPMOTER_H__ #include <avr/io.h> #include <util/delay.h> #include "usart.h" //--- Control Table Address --- //EEPROM AREA #define P_MODEL_NUMBER_L 0 #define P_MODOEL_NUMBER_H 1 #define P_VERSION 2 #define P_ID 3 #define P_BAUD_RATE 4 #define P_RETURN_DELAY_TIME 5 #define P_CW_ANGLE_LIMIT_L 6 #define P_CW_ANGLE_LIMIT_H 7 #define P_CCW_ANGLE_LIMIT_L 8 #define P_CCW_ANGLE_LIMIT_H 9 #define P_SYSTEM_DATA2 10 #define P_LIMIT_TEMPERATURE 11 #define P_DOWN_LIMIT_VOLTAGE 12 #define P_UP_LIMIT_VOLTAGE 13 #define P_MAX_TORQUE_L 14 #define P_MAX_TORQUE_H 15 #define P_RETURN_LEVEL 16 #define P_ALARM_LED 17 #define P_ALARM_SHUTDOWN 18 #define P_OPERATING_MODE 19 #define P_DOWN_CALIBRATION_L 20 #define P_DOWN_CALIBRATION_H 21 #define P_UP_CALIBRATION_L 22 #define P_UP_CALIBRATION_H 23 #define P_TORQUE_ENABLE (24) #define P_LED (25) #define P_CW_COMPLIANCE_MARGIN (26) #define P_CCW_COMPLIANCE_MARGIN (27) #define P_CW_COMPLIANCE_SLOPE (28) #define P_CCW_COMPLIANCE_SLOPE (29) #define P_GOAL_POSITION_L (30) #define P_GOAL_POSITION_H (31) #define P_GOAL_SPEED_L (32) #define P_GOAL_SPEED_H (33) #define P_TORQUE_LIMIT_L (34) #define P_TORQUE_LIMIT_H (35) #define P_PRESENT_POSITION_L (36) #define P_PRESENT_POSITION_H (37) #define P_PRESENT_SPEED_L (38) #define P_PRESENT_SPEED_H (39) #define P_PRESENT_LOAD_L (40) #define P_PRESENT_LOAD_H (41) #define P_PRESENT_VOLTAGE (42) #define P_PRESENT_TEMPERATURE (43) #define P_REGISTERED_INSTRUCTION (44) #define P_PAUSE_TIME (45) #define P_MOVING (46) #define P_LOCK (47) #define P_PUNCH_L (48) #define P_PUNCH_H (49)
//--- Instruction --- #define INST_PING 0x01 #define INST_READ 0x02 #define INST_WRITE 0x03 #define INST_REG_WRITE 0x04 #define INST_ACTION 0x05 #define INST_RESET 0x06 #define INST_DIGITAL_RESET 0x07 #define INST_SYSTEM_READ 0x0C #define INST_SYSTEM_WRITE 0x0D #define INST_SYNC_WRITE 0x83 #define INST_SYNC_REG_WRITE 0x84
#define CHECK_TXD0_FINISH bit_is_set(UCSR0A,6)
void StepMotor_Init(); void Ctrl_motor(); #endif //__STEPMOTER_H__
|
<STEPMOTOR.C>
#include "stepmotor.h"
int Calc_Ang(int val) // 각도 계산 { unsigned long temp = val*2.8444;
char c[4] ={0}; c[0] = ((int)temp/100)+'0'; c[1] = (((int)temp%100)/10)+'0'; c[2] = (((int)temp%100%10))+'0'; USART_Print(c,4);
return (int)temp; }
char para[128]; // PACKET pata void Make_Packet(char Id, char Inst, char Paralength) // 패킷 만들어 보냄 { char gbpTxBuffer[128]; char bCount,bCheckSum,bPacketLength;
gbpTxBuffer[0] = 0xff; gbpTxBuffer[1] = 0xff; gbpTxBuffer[2] = Id; gbpTxBuffer[3] = Paralength+2;
//Length(Paramter,Instruction,Checksum)
gbpTxBuffer[4] = Inst; for(bCount = 0; bCount < Paralength; bCount++) { gbpTxBuffer[bCount+5] = para[bCount]; }
bCheckSum = 0; bPacketLength = Paralength+4+2;
for(bCount = 2; bCount < bPacketLength-1; bCount++) //except 0xff,checksum { bCheckSum += gbpTxBuffer[bCount]; } gbpTxBuffer[bCount] = ~bCheckSum; //Writing Checksum with Bit Inversion RS485_TXD; /* for(bCount = 0; bCount < bPacketLength; bCount++) { sbi(UCSR0A,6);//SET_TXD0_FINISH; TxD80(gbpTxBuffer[bCount]); } while(!CHECK_TXD0_FINISH); //Wait until TXD Shift register empty RS485_RXD; */ USART_Print(gbpTxBuffer,bCount+1); return;//(bPacketLength); }
void Ctrl_motor() // 모터 따로 동작 { int ang = 0;
_delay_ms(20); memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(180); para[1] = ang; para[2] = ang>>8; Make_Packet(0x0B, INST_WRITE, 3); _delay_ms(1000);
memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(180); para[1] = ang; para[2] = ang>>8; Make_Packet(0x07, INST_WRITE, 3); _delay_ms(1000);
memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(90); para[1] = ang; para[2] = ang>>8; Make_Packet(0x0B, INST_WRITE, 3); _delay_ms(1000);
memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(90); para[1] = ang; para[2] = ang>>8; Make_Packet(0x07, INST_WRITE, 3); _delay_ms(1000); }
void Ctrl_motor2() // 모터 동시 동작 { int ang = 0;
// INST_REG_WRITE _delay_ms(20); memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(10); para[1] = ang; para[2] = ang>>8; Make_Packet(0x07, INST_REG_WRITE, 3); _delay_ms(1000);
// INST_REG_WRITE memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS ang = Calc_Ang(45); para[1] = ang; para[2] = ang>>8; Make_Packet(0x0B, INST_REG_WRITE, 3); _delay_ms(20);
// ACTION _delay_ms(20); memset(para,0,128); Make_Packet(0xFE, INST_ACTION, 0); _delay_ms(1000);
}
void StepMotor_Init() { //RESET ///* _delay_ms(20); memset(para,0,128); Make_Packet(0xFE, INST_RESET, 0); UBRR0_BaudChg(0); //*/
//TEST _delay_ms(100); memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS para[1] = Calc_Ang(180); para[2] = (Calc_Ang(180))>>8; Make_Packet(0x01, INST_WRITE, 3); _delay_ms(1000);
memset(para,0,128); para[0] = P_GOAL_POSITION_L; // ADDRESS para[1] = Calc_Ang(90); para[2] = (Calc_Ang(90))>>8; Make_Packet(0x01, INST_WRITE, 3); _delay_ms(20); //TEST
///* //BAUDRATE _delay_ms(20); memset(para,0,128); para[0] = P_BAUD_RATE; // ADDRESS para[1] = 207;//(2000000-9600)/9600;//207; Make_Packet(0x01, INST_WRITE, 2); _delay_ms(1000); UBRR0_BaudChg(1); //*/
//ID _delay_ms(20); memset(para,0,128); para[0] = P_ID; para[1] = 0x0B; // ID Make_Packet(0x01, INST_WRITE, 2); _delay_ms(20);
}
|
<USART.C>
#include "usart.h"
//속도/UART 셋팅 void USART0_Init(void) { //UBRR0H = UBRR_H; //UBRR0L = UBRR_L; //UBRR0 = 0; // 1000000 UBRR0 = ((unsigned long)((F_OSC/(16.0*BAUD)) - 0.5)); //
UCSR0A = 0b00000000;//(0<<U2X0)|(0<<MPCM0); UCSR0B = 0b00001000;//(0<<RXEN0)|(1<<TXEN0)|(0<<UCSZ02); // 0100 UCSR0C = 0b00000110;//(0<<UMSEL01)|(0<<UMSEL00)|(0<<UPM01)|(0<<UPM00)|(0<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00); //100
/* UCSR0A = (0<<U2X)|(0<<MPCM); UCSR0B = (1<<RXEN)|(1<<TXEN)|(0<<UCSZ2); UCSR0C = (0<<UMSEL)|(1<<UPM1)|(0<<UPM0)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0); */ return; }
void UBRR0_BaudChg(int i) { if(0 == i) UBRR0 = 0; else UBRR0 = ((unsigned long)((F_OSC/(16.0*BAUD)) - 0.5)); }
void USART1_Init(void) { //UBRR1H = UBRR_H; //UBRR1L = UBRR_L; //UBRR1 = 0; UBRR1 = ((unsigned long)((F_OSC/(16.0*BAUD)) - 0.5)); //
UCSR1A = 0b00000000;//(0<<U2X1)|(0<<MPCM1); UCSR1B = 0b00001000;//(0<<RXEN1)|(1<<TXEN1)|(0<<UCSZ12); UCSR1C = 0b00000110;//(0<<UMSEL11)|(0<<UMSEL10)|(0<<UPM11)|(0<<UPM10)|(0<<USBS1)|(1<<UCSZ11)|(1<<UCSZ10);
return; }
void USART0_Tx( unsigned char ucData ) { //Wait for empty transmit buffer - polling 방식 while ( 0 == ( UCSR0A & (1<<UDRE0)) ); // write ok신호 까지 정지 : (1<<UDRE) : MASK - 특정 비트 추출 //Put data into buffer, sends the data UDR0 = ucData;
return; }
void USART1_Tx( unsigned char ucData ) { //Wait for empty transmit buffer while ( 0 == ( UCSR1A & (1<<UDRE1)) ); //Put data into buffer, sends the data UDR1 = ucData;
return; }
void USART_Print(const unsigned char * ucString, int cnt) { const unsigned char * temp = ucString; int i=0;
for(i = 0; i < cnt ; ++i) { USART0_Tx(*temp); //USART1_Tx(*temp); ++temp; }
return; }
|
<MAIN.C>
#include <avr/io.h> #include <util/delay.h> #include "usart.h" #include "stepmotor.h"
void Init(void) { USART0_Init(); USART1_Init(); //StepMotor_Init(); return; }
int main(void) { Init(); Ctrl_motor(); Ctrl_motor2();
while(1) {
}
return 0; }
|
==========================================================================================
NETWORK
==========================================================================================
<NETWORK>
http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site
• 리눅스 네트워크 프로그래밍
• 네트워크 프로그래밍 기초 문서들
• 예제로 알아보는 소켓 프로그래밍
=소켓프로그래밍
-SERVER
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <stdlib.h>
int main(int argc, char ** argv) { int server_sockfd; int client_sockfd; int state, client_len; int pid;
FILE *fp; struct sockaddr_in clientaddr; struct sockaddr_in serveraddr;
char buf[255]; char line[255];
if(argc !=2) { printf("Usage : ./zipcode [port]\n"); printf("예 : ./zipcode 444\n"); exit(0); }
memset(line, '0',255); state = 0;
//주소파일을 읽어들인다 client_len = sizeof(clientaddr); if((fp = fopen("zipcode.txt","r")) == NULL) { perror("file open error : "); exit(0); }
// internet 기반의 소켓 생성(INET) if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket error : "); exit(0); } bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons(atoi(argv[1])); state = bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); if(state == -1) { perror("bind error : "); exit(0); }
state = listen(server_sockfd, 5); if(state == -1) { perror("listen error : "); exit(0); }
while(1) { client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len); if(client_sockfd ==-1) { perror("Accept error : "); exit(0); } while(1) { rewind(fp); memset(buf, '0', 255);
if(read(client_sockfd, buf, 255) <=0) { close(client_sockfd); break; } if(strncmp(buf, "quit", 4) ==0) { write(client_sockfd, "bye bye", 8); close(client_sockfd); break; }
while(fgets(line, 255, fp) != NULL) { if(strstr(line, buf) != NULL) { write(client_sockfd, line, 255); } memset(line, '0', 255); } write(client_sockfd, "end", 255); printf("send end\n"); } }
close(client_sockfd); return 0; }
|
-CLIENT
#include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
int main(int argc, char ** argv) { int client_len; int client_sockfd;
FILE *fp_in; char buf_in[255]; char buf_get[255];
struct sockaddr_in clientaddr;
if(argc !=2) { printf("Usage : ./zipcode_cl [port]\n"); printf("예 : ./zipcode_cl 4444\n"); exit(0); }
client_sockfd = socket(AF_INET, SOCK_STREAM, 0); clientaddr.sin_family = AF_INET; clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); clientaddr.sin_port = htons(atoi(argv[1]));
client_len = sizeof(clientaddr);
if(connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0) { perror("Connect error : "); exit(0); }
while(1) { printf("지역이름 입력 : "); fgets(buf_in, 255, stdin);
buf_in[strlen(buf_in)-1] = 0; write(client_sockfd, buf_in, 255); if(strncmp(buf_in, "quit", 4) ==0) { close(client_sockfd); exit(0); }
while(1) { read(client_sockfd, buf_get, 255); if(strncmp(buf_get, "end", 3) ==0) { break; }
printf("%s", buf_get);
} } close(client_sockfd); return 0; }
|


=
외부에서 프로세스에 접근 시 PORT번호 사용
=에러처리
EXIT =>대신 return 번호; // 에러별로 번호 각각
=port가 닫혀있음 => listen => 영업시작(대기상대)
=NONBLOCKING 함수 => 바로 리턴
backlog : 동시 접속자 수 (동시에 뽑을 수 있는 번호표 기계수) => 메모리 등 비용 발생 => 5 권장


=accept : accept 해주지 않으면 접속 불가(청원경찰) - 소켓번호, 상대방 정보저장 할 메모리 주소 ,
:socket 번호 새로 생성 반환
:BLOCKING 함수 (SCANF...)
-어디서 왔는지 체크(SERVER는 PORT 두 개 필요 - SERVER(대기용) / CLIENT(통신용))
=해킹
B-PC A-PC
C-PC ( DDOS로 A-PC포트 막고) : A로 위장 B로 접속
=서버 socket - 1. 대기 socket
2. 통신 accept
=DOS(DENY OF SERVICE)공격 (5개 동시접속 가능 포트에 1000개 동시 접속시 마비)
=DDOS (DISTRIBUTIED DENY OF SERVICE) - (나경원 박원순 선거 오전 마비)
= SERVER
1.소켓 생성
2.IP/PORT 설정
3.바인드
4.리슨
5.어셉트
6.READ/WRITE - 통신
7.CLOSE
=CLIENT
1.소켓 생성
2.IP/PORT 설정
3.CONNECT (5. SEVER:ACCEPT) => 3 HANDSHAKE (EX : 네이버 접속:첫페이지 받고 접속끊김(세션기술로 재접속))
4.WRITE/READ - 통신
5.CLOSE
==서버(멀티 방식)
=1. 멀티프로세스(Fork) 방식(각 클라이언트에대응 할 서버프로그램 추가로 복사해서 늘려 대응) => 사용 안하는 메모리영역 발생 (좋지않음)
=2. 프로세스 자체에서 함수부분을 뜯어내 프로세스처럼 동작 => 멀티스레드(pthread)
==READ/WRITE
=3. 멀티플렉스( SELECT )
-서버 히스테리시스 테스트
-
-3 HANDSHAKE : TCP 절차 프로토콜(이것의 약점 공격 : DDOS)
=
#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
int main() { struct in_addr addr;
addr.s_addr = inet_addr("192.168.0.10"); printf("%s\n",inet_ntoa(addr)); return 0; } |


=
#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
int main() { struct in_addr addr; unsigned char * ucp = (unsigned char *)&addr;
addr.s_addr = inet_addr("192.168.0.10"); printf("%s\n",inet_ntoa(addr)); printf("%02x\n",*(ucp+0)); printf("%02x\n",*(ucp+1)); printf("%02x\n",*(ucp+2)); printf("%02x\n",*(ucp+3)); return 0; } |


=DC모터는 HOLDING 개념 없음 (기어로 잡고 있음) - 칩제어 (시리얼 프로토콜에 맞춰 동작)
: 스테핑모터 자석 촘촘 -> 정밀 각도 제어 - 펄스보내 제어(오리지널 제어)
-큰 로봇에는 센서 있으나 작은로봇은 센서 없음(보행용센서)
-TIMER COUNTER
-PWM : OVERFLOW(COUNTING 속도 : 분주비로 조정 + count 범위 조정 => 듀티비 조정
-ABS(브레이크 상태에서 핸들 조정 가능)