Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
<mega128.h>
<math.h>
<stdio.h>
<delay.h>
#endif
#define
#define
#define
#define
#define
FRAMING_ERROR (1<<FE)
PARITY_ERROR (1<<UPE)
DATA_OVERRUN (1<<DOR)
DATA_REGISTER_EMPTY (1<<UDRE)
RX_COMPLETE (1<<RXC)
= 1;
= 2;
= 3; d = 1;
= 1;
= 2;
= 3;
= 4; d = 2;
= 1;
= 2;
= 3;
= 4;
= 5; d = 3;
= 1;
= 2;
L1 95
L2 93
L3 115
Rad 57.3248407643
ResServo 3.413
float femur_ang,tibia_ang,se,a1,a2,a3;
char buffer[16];
int a,flag,flag2=0;
void leg_kinematic(float x,float y)
{
se = sqrt(((x-coxa_l)*(x-coxa_l)) + (y*y));
a1 = atan2((x-coxa_l),y);
a2 = acos((se*se + femur_l*femur_l - tibia_l*tibia_l ) / (2 * femur_l *
se));
femur_ang = 90-(a1 + a2)*180/3.14;
tibia_ang = 180-(acos(((femur_l*femur_l)+(tibia_l*tibia_l)-(se*se))/(2*f
emur_l*tibia_l))*180/3.14);
}
// Declare your global variables here
void SetPosition(unsigned char ID,unsigned int GoalPosition)
{
unsigned int x,y;
putchar(0xFF); //header 1
putchar(0xFF); //header 2
y = ID; x =y;
putchar(y); //ID
y = 5; x +=y;
putchar(y); //Panjang Paket (length)
y = 0x03; x +=y;
putchar(y); //INS Write
y = 0x1e; x +=y;
putchar(y); //Address
y = GoalPosition & 255; x +=y;
putchar(y); //Data Low
y = GoalPosition >> 8; x +=y;
putchar(y); //Data high
putchar(~x);
}
float xp, yp;
float Res, sudut1, sudut2, sudut3;
void Convert2Angle(){
int data1, data2, data3;
data1 = 512 + (int)(sudut1 * ResServo);
data2 = 512 + (int)(sudut2 * ResServo);
data3 = 512 + (int)(sudut3 * ResServo);
SetPosition(1, data1);
SetPosition(2, data2);
SetPosition(3, data3);
SetPosition(4, 530);
}
void InverseKinematic(int x, int y, float angle){
xp = (float)x + L3 * sin(angle/Rad);
yp = (float)y - L3 * cos(angle/Rad);
Res = sqrt(xp * xp + yp * yp);
a1 = atan2(xp, yp)*Rad;
a2 = acos((Res*Res - L2 * L2 - L1 * L1)/(2*L1*L2))*Rad;
a3 = asin((L2/Res)*sin(a2/Rad))*Rad;
sudut1 = (a1 + a3);
sudut2 = -a2;
sudut3 = angle + (sudut2 + sudut1);
Convert2Angle();
}
int t,x0=-50,y0=240,x1=100,y1=180,last_y=0,last_x=0, itr=100;
float angle;
void Trajectory(){
if(t > itr){
x0 = last_x;
y0 = last_y;
x1 = X[titik];
y1 = Y[titik];
titik ++;
if(titik > jmlTitik)titik = 1;
t = 0;
}else{
last_x = x0 + ((x1 - x0)*t)/itr;
last_y = y0 + ((y1 - y0)*t)/itr;
t++;
}
angle = atan2((float)(last_x-80),(float)(last_y-100))*Rad;
InverseKinematic(last_x, last_y, -angle);
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;
// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer3 Stopped
// Mode: Normal top=FFFFh
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 1000000
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x00;
// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x98;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x67;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
x0 = 0;
y0 = 200;
x1 = 0;
y1 = 200;
X[0]=0;
Y[0]=200;
titik = 0;
jmlTitik=1;
while (1)
{
// Place your code here
/* if(flag2==0){
if(a>=100){
SetPosition(4,310);
delay_ms(1000);
flag2=1;
a=-100;
}
if(a<=-100) flag=0;
if(flag==0) {
a++;
SetPosition(4,520);
}
leg_kinematic(210,a);
}
if(flag2==1){
if(a>=100) {
SetPosition(4,310);
delay_ms(1000);
flag2=3;
a=-100;
}
if(a<=-100) flag=0;
if(flag==0) {
a++;
SetPosition(4,C);
}
leg_kinematic(260,a);
}
delay_ms(10);
SetPosition(1,512-300+(300/90*femur_ang));
SetPosition(2,512-(300/90*tibia_ang));
SetPosition(3,512);
*/
//InverseKinematic(80,200,0);
if(gerak == 1){
Trajectory();
}
lcd_gotoxy(0,0);
sprintf(buffer,"%d %d %d %d %d %d %d",X[1],X[2],X[3],X[4],X[5],X[6],X[7]
);
//sprintf(buffer,"%d %d %d ",DataX[0],DataX[1],DataX[2]);
lcd_puts(buffer);
lcd_gotoxy(0,1);
sprintf(buffer,"%d %d %d %d %d %d %d",Y[1],Y[2],Y[3],Y[4],Y[5],Y[6],Y[7]
);
lcd_puts(buffer);
delay_ms(1);
};
}