Arduino 控制左右舵机与位置编程

需积分: 50 110 下载量 119 浏览量 更新于2024-09-09 收藏 8KB TXT 举报
本文档主要介绍了如何编程控制Arduino Uno或类似平台上的舵机(Servo Motors)进行精确定位,特别是涉及到左右悬臂舵机的90度位置设置。在程序代码中,作者使用了Servo库来实现舵机的控制,并定义了一系列与舵机相关的常量。 1. **舵机角度定义**: - `#define SERVOFAKTORLEFT 550` 和 `#define SERVOFAKTORRIGHT 630` 定义了左右两个悬臂舵机的90度位置。理论上,650是中间位置,但在实际应用中可能需要根据舵机的实际角度范围进行微调。这些值代表了在Servo类中设置舵机目标角度的数值,180度对应全开,0度对应完全关闭。 2. **伺服舵机引脚分配**: - `#define SERVOPINLIFT 2`, `#define SERVOPINLEFT 3`, 和 `#define SERVOPINRIGHT 4` 分别定义了三个伺服电机的连接引脚。LIFT伺服用于升降功能,LEFT和RIGHT分别用于左右方向的舵机。 3. **舵机运动范围**: - `#define SERVOLEFTNULL 1750` 和 `#define SERVORIGHTNULL 880` 定义了左右舵机的极限位置,分别表示非工作区域,比如当不需要移动时的默认位置。 - `#define LIFT01080` 和 `#define LIFT1925` 分别定义了升降舵机的起始和目标位置,用于控制升降机构的行程。 4. **速度控制**: - `#define LIFTSPEED 1500` 表示升降舵机的速度,即每秒转动的角度数,这有助于平滑和快速地调整高度。 5. **舵机运动路径**: - 通过变量`L135`, `L257.2`, 和 `L314.2` 可能定义了某种路径或序列,但具体含义没有直接给出,可能是舵机的移动距离或目标角度序列。 6. **定位坐标**: - `#define O1X 23`, `#define O1Y -25`, `#define O2X 49`, 和 `#define O2Y -25` 可能是指两个舵机在空间中的初始位置坐标,用于指定它们对准的目标点。 7. **时间管理**: - `#include <Time.h>` 引入了时间库,可能用于定时任务或者与时间相关的舵机操作。 - `setTime(19, 38, 0, 0, 0, 0)` 设置了设备的时间,`drawTo(38, 24)` 可能是绘制一个与时间相关的位置或触发某个动作的时间点。 8. **初始化函数**: - 在`void setup()` 函数中,初始化了时间、舵机引脚以及延迟1000毫秒确保硬件准备好。 9. **主循环函数**: - `void loop()` 是程序的主执行部分,这里包含了控制舵机运动的逻辑,包括根据`SERVOFAKTORLEFT` 和 `SERVOFAKTORRIGHT` 值调整左右舵机的角度,可能是为了模拟某种机械结构的动作。 通过这个程序,你可以控制三个舵机进行精确的定位和路径规划,适用于自动化机械臂、无人机或其他需要精确角度控制的项目。注意实际使用时,可能需要根据硬件的具体情况进行参数调整和优化。
2018-06-12 上传
#include<REG52.h> #define uchar unsigned char #define uint unsigned int #define N 1 //¿è²¿¶æ»ú sbit PWM_OUT00=P0^0; sbit PWM_OUT01=P0^1; sbit PWM_OUT02=P0^2; sbit PWM_OUT03=P0^3; sbit PWM_OUT04=P0^4; sbit PWM_OUT05=P0^5; //СÍȶæ»ú sbit PWM_OUT10=P1^0; sbit PWM_OUT11=P1^1; sbit PWM_OUT12=P1^2; sbit PWM_OUT13=P1^3; sbit PWM_OUT14=P1^4; sbit PWM_OUT15=P1^5; //´óÍȶæ»ú sbit PWM_OUT20=P2^0; sbit PWM_OUT21=P2^1; sbit PWM_OUT22=P2^2; sbit PWM_OUT23=P2^3; sbit PWM_OUT24=P2^4; sbit PWM_OUT25=P2^5; //PWMµÄÊý¾ÝÖµ uint code PWM_Value[N][18]={1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510}; //¶¨ÒåpwmÐźÅÖеĸߵçƽʱ¼ä ·¶Î§ 535< pwm_h <2430 uint PWM_now[18] ; //µ±Ç°pwmµÄÊýÖµ uint *pwm_now_ad[18]; //µ±Ç°pwmµÄµØÖ· uchar dongzuo_pd=0; //¶¯×÷Ƭ¶ÎµÄ˳Ðò´úºÅ uchar order1; //¶¨Ê±Æ÷ɨÃèÐòÁÐ uchar dj_num; bit fa_time=0; bit fa_ck=0; struct Uartframe { unsigned char frameh; unsigned char id; //idΪ0x30ʱΪȫ·¿ØÖÆÖ¸Áî unsigned char cmd; unsigned char udata; unsigned char framee; }; struct Uartframe sendframe={'#',0xff,0xff,0xff,'$'}, recframe={'#',0xff,0xff,0xff,'$'}; /*------------------------------------------------ ´®¿Ú³õʼ»¯ ------------------------------------------------*/ void InitUART (void) { PS=1; SCON |= 0x50; // SCON: ģʽ 1, 8-bit UART, ʹÄܽÓÊÕ TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit ÖØ×° TH1 = 0xFD; // TH1: ÖØ×°Öµ 9600 ²¨ÌØÂÊ ¾§Õñ 11.0592MHz TR1 = 1; // TR1: timer 1 ´ò¿ª EA = 0; //¹Ø±Õ×ÜÖÐ¶Ï ES = 1; //´ò¿ª´®¿ÚÖÐ¶Ï } /* void send() { unsigned char i,*uartp = &sendframe;.frameh; for(i=0;i<sizeof(sendframe);i++) { ES = 0; TI = 0; SBUF = *uartp; while(!TI); TI = 0; ES = 1; uartp++; } } */ void gengxinshuju() { if(recframe.frameh=='#' & recframe.framee=='$') { switch(recframe.id) { case 0x04: PWM_now[recframe.cmd-1] = recframe.udata*7+550; P2=0x80; //ÓÃÁ÷Ë®µÆÑéÖ¤ break; case 0x01: P2=0x10; break; case 0x02: P2=0x20; break; case 0x03: P2=0x40; break; default: P2=0x0f; } } /* SBUF=recframe.frameh; SBUF=recframe.id; SBUF=recframe.cmd; SBUF=recframe.udata; SBUF=recframe.framee; */ } void ad() { uchar i; for(i=0;i<18;i++) { pwm_now_ad[i]=&PWM;_now[i]; } } void pwm_timer(); /*=================================================================================== ³õʼ»¯ÖÐ¶Ï =====================================================================================*/ void InitPWM(void) { order1=0; TMOD |=0x11; TH0=-1500/256; TL0=-1500%6; EA=0; EX0=0; ET0=1; TR0=1;PT0=1;PX0=0; } void delayMS(uint x) //ºÁÃ뼶ÑÓʱ Îó²î 0us { uchar a,b,c; while(x--) { for(c=1;c>0;c--) for(b=142;b>0;b--) for(a=2;a>0;a--); } } void pwm_nowfz() { uchar i,j,k,zu; uint *tempp; for(i=0;i<18;i++) { PWM_now[i]=PWM_Value[dongzuo_pd][i]; } for(zu=0;zu<18;zu=zu+3) { for(j=zu;j<zu+2;j++) //Èý¸ö¶æ»úÅÅÐò { for(k=j+1;k<zu+3;k++) { if(*pwm_now_ad[j]>*pwm_now_ad[k]) { tempp=pwm_now_ad[j]; pwm_now_ad[j]=pwm_now_ad[k]; pwm_now_ad[k]=tempp; } } } } } void dongzuo1() { dongzuo_pd=0;pwm_nowfz();EA=1; delayMS(2); /* delayMS(5000); dongzuo_pd=1;pwm_nowfz(); delayMS(5000); dongzuo_pd=2;pwm_nowfz(); delayMS(5000); dongzuo_pd=3;pwm_nowfz(); delayMS(5000); */ } void io_dp() //io¿ÚµçƽÉú³É { dj_num=pwm_now_ad[order1-1]-&PWM;_now[0]; switch(dj_num) { case 0 : PWM_OUT00=0;break; case 1 : PWM_OUT01=0;break; case 2 : PWM_OUT02=0;break; case 3 : PWM_OUT03=0;break; case 4 : PWM_OUT04=0;break; case 5 : PWM_OUT05=0;break; case 6 : PWM_OUT10=0;break; case 7 : PWM_OUT11=0;break; case 8 : PWM_OUT12=0;break; case 9 : PWM_OUT13=0;break; case 10 : PWM_OUT14=0;break; case 11: PWM_OUT15=0;break; case 12: PWM_OUT20=0;break; case 13: PWM_OUT21=0;break; case 14 : PWM_OUT22=0;break; case 15 : PWM_OUT23=0;break; case 16 : PWM_OUT24=0;break; case 17 : PWM_OUT25=0;break; } } void main(void) { InitPWM(); InitUART(); ad(); while(1) { dongzuo1(); } } unsigned char *uartp = &recframe;.frameh; void UARTInterrupt(void) interrupt 4 { uchar temp; if(RI) { RI = 0; temp=SBUF; SBUF=temp; if('#' == SBUF | (uartp-&recframe;.frameh)>sizeof(recframe)) uartp = &recframe;.frameh; if('$' == SBUF) gengxinshuju(); *uartp = SBUF; uartp++; } else { TI=0; } } /*=================================================================================== ¶¨Ê±Æ÷T0µÄÖжϷþÎñ³ÌÐò Ò»¸öÑ­»·20MS = 8*2.5ms =====================================================================================*/ void timer0(void) interrupt 1 using 1 { //pwm_timer(); switch(order1) { case 0: //io_dp(); TH0 =(-*pwm_now_ad[0])/256; TL0 =(-*pwm_now_ad[0])%6; PWM_OUT00=1; PWM_OUT01=1; PWM_OUT02=1; order1++; break; case 1: TH0 =-(*pwm_now_ad[1]-*pwm_now_ad[0])/256; TL0 =-(*pwm_now_ad[1]-*pwm_now_ad[0])%6; io_dp(); order1++; break; case 2: TH0 =-(*pwm_now_ad[2]-*pwm_now_ad[1])/256; TL0 =-(*pwm_now_ad[2]-*pwm_now_ad[1])%6; io_dp(); order1++; break; case 3: TH0 =-(2303-*pwm_now_ad[2])/256; TL0 =-(2303-*pwm_now_ad[2])%6; io_dp(); order1=19; break; case 19: //io_dp(); TH0 =(-*pwm_now_ad[3])/256; TL0 =(-*pwm_now_ad[3])%6; PWM_OUT03=1; PWM_OUT04=1; PWM_OUT05=1; //order1++; order1=4; break; case 4: TH0 =-(*pwm_now_ad[4]-*pwm_now_ad[3])/256; TL0 =-(*pwm_now_ad[4]-*pwm_now_ad[3])%6; io_dp(); order1++; break; case 5: TH0 =-(*pwm_now_ad[5]-*pwm_now_ad[4])/256; TL0 =-(*pwm_now_ad[5]-*pwm_now_ad[4])%6; io_dp(); order1++; if(order1>18) order1=0; break; case 6: TH0 =-(2303-*pwm_now_ad[5])/256; TL0 =-(2303-*pwm_now_ad[5])%6; io_dp(); order1=20; break; case 20: TH0 =(-*pwm_now_ad[6])/256; TL0 =(-*pwm_now_ad[6])%6; PWM_OUT10=1; PWM_OUT11=1; PWM_OUT12=1; //order1++; order1=7; break; case 7: TH0 =-(*pwm_now_ad[7]-*pwm_now_ad[6])/256; TL0 =-(*pwm_now_ad[7]-*pwm_now_ad[6])%6; io_dp(); order1++; break; case 8: TH0 =-(*pwm_now_ad[8]-*pwm_now_ad[7])/256; TL0 =-(*pwm_now_ad[8]-*pwm_now_ad[7])%6; io_dp(); order1++; if(order1>18) order1=0; break; case 9: TH0 =-(2303-*pwm_now_ad[8])/256; TL0 =-(2303-*pwm_now_ad[8])%6; io_dp(); order1=21; break; case 21: TH0 =(-*pwm_now_ad[9])/256; TL0 =(-*pwm_now_ad[9])%6; PWM_OUT13=1; PWM_OUT14=1; PWM_OUT15=1; //order1++; order1=10; break; case 10: TH0 =-(*pwm_now_ad[10]-*pwm_now_ad[9])/256; TL0 =-(*pwm_now_ad[10]-*pwm_now_ad[9])%6; io_dp(); order1++; break; case 11: TH0 =-(*pwm_now_ad[11]-*pwm_now_ad[10])/256; TL0 =-(*pwm_now_ad[11]-*pwm_now_ad[10])%6; io_dp(); order1++; break; case 12: TH0 =-(2303-*pwm_now_ad[11])/256; TL0 =-(2303-*pwm_now_ad[11])%6; io_dp(); order1=22; break; case 22: TH0 =(-*pwm_now_ad[12])/256; TL0 =(-*pwm_now_ad[12])%6; PWM_OUT20=1; PWM_OUT21=1; PWM_OUT22=1; //order1++; order1=13; break; case 13: TH0 =-(*pwm_now_ad[13]-*pwm_now_ad[12])/256; TL0 =-(*pwm_now_ad[13]-*pwm_now_ad[12])%6; io_dp(); order1++; break; case 14: TH0 =-(*pwm_now_ad[14]-*pwm_now_ad[13])/256; TL0 =-(*pwm_now_ad[14]-*pwm_now_ad[13])%6; io_dp(); order1++; break; case 15: TH0 =-(2303-*pwm_now_ad[14])/256; TL0 =-(2303-*pwm_now_ad[14])%6; io_dp(); order1=23; break; case 23: TH0 =(-*pwm_now_ad[15])/256; TL0 =(-*pwm_now_ad[15])%6; PWM_OUT23=1; PWM_OUT24=1; PWM_OUT25=1; //order1++; order1=16; break; case 16: TH0 =-(*pwm_now_ad[16]-*pwm_now_ad[15])/256; TL0 =-(*pwm_now_ad[16]-*pwm_now_ad[15])%6; io_dp(); order1++; break; case 17: TH0 =-(*pwm_now_ad[17]-*pwm_now_ad[16])/256; TL0 =-(*pwm_now_ad[17]-*pwm_now_ad[16])%6; io_dp(); order1++; break; case 18: TH0 =-(5000-*pwm_now_ad[17])/256; TL0 =-(5000-*pwm_now_ad[17])%6; io_dp(); //order1=19; order1++; if(order1>18) order1=0; break; default : order1=0;break; } }
2014-08-18 上传