[ 本帖最后由 keelai8 于 2011-7-12 23:54 编辑 ]
作者: keelai8 时间: 2011-7-12 11:58 标题: 其实就是一步步把主程序的流程和硬件对应调好 int main(void)
{
int cs;
// vars used for detection of incremental motion
jerk = 0.0;
setup_io(); // make all i/o pins go the right dir
STATUS_LED = 0; // led on
setup_uart(); // setup the serial interface to the PC
setup_TMR1(); // set up 1ms timer
IEC0bits.T1IE = 1; // Enable interrupts for timer 1
// needed for delays in following routines
// 1/2 seconds startup delay
timer_test = 5000;
while ( timer_test );
printf("\r\nPowerup..i/o...uart...timer...");
init_pid();
pid.enable = 0; // turn servo loop off for a while
printf("pid...");
setup_pwm(); // start analog output
set_pwm(0.0);
printf("pwm...");
setup_encoder(); // 16 bit quadrature encoder module setup
printf("encoder...");
setup_capture(); // 2 pins with quadrature cmd from PC
printf("capture...");
printf("done\r\n");
// some junk for the serial channel
printf("%s%s\n\r",CPWRT,VERSION);
STATUS_LED = 1; // led off when init finished
// restore config from eeprom
// Read array named "setupEE" from DataEEPROM and place
// the result into array in RAM named, "setup"
restore_setup();
cs = -calc_cksum(((long int)&pid.cksum - (long int)&pid)/sizeof(int),(int*)&pid);
if ( cs != pid.cksum )
{
// opps, no valid setup detected
// assume we are starting from a new box
printf("No valid setup found in EEPROM 0x%04X\r\n",pid.cksum);
init_pid();
while (1 )
{
// a very fast flash to indicate no config... serial activity
// (hopefully the user setting params gets us out of the loop)
STATUS_LED = 0; timer_test = 500; while ( timer_test );
STATUS_LED = 1; timer_test = 500; while ( timer_test );
if ( rxrdy ) break;
}
}
else
{
printf("Using setup from eeprom.. ? for help\r\n");
print_tuning();
}
printf("using %fms servo loop interval\r\n",pid.ticksperservo * 0.250);
// BLOCK OF TEST ROUTINES FOR HARDWARE DEBUGGING
// test_pwm_interface(); // play with opa549 hardware
// test_pc_interface(); // echo cmded posn to serial port
while (1)
{
// check for serial cmds
if ( rxrdy )
process_serial_buffer();
if ( jerk > 0.0 )
{
while ( 1 )
{
// loop forever until serial active or servo gets disabled
if ( rxrdy || !SVO_ENABLE )
{
jerk = 0.0;
break;
}
pid.command += jerk;
timer_test = 5000; while ( timer_test );
pid.command -= jerk;
timer_test = 5000; while ( timer_test );
}
}
// show we are at drive limit(error)
if (pid.limit_state)
STATUS_LED = 0;
else
STATUS_LED = 1;
// check for a drive fault ( posn error > allowed )
if (( pid.maxerror > 0.0 ) &&
( fabs(pid.error) > pid.maxerror ) &&
SVO_ENABLE )
{
pid.enable = 0; // shut down servo updates
while (1) // trap here until svo disabled or pwr cycle
{
set_pwm( 0.0 );
printf("drive fault... maxerror exceeded %f\r\n",(double)pid.error);
STATUS_LED = 0; timer_test = 2500; while ( timer_test );
STATUS_LED = 1; timer_test = 2500; while ( timer_test );
STATUS_LED = 0; timer_test = 2500; while ( timer_test );
STATUS_LED = 1; timer_test = 2500; while ( timer_test );
if (!SVO_ENABLE)
break;
}
while ( !SVO_ENABLE ); // wait for us to be enabled
}
// check to see if external forces are causing use to change servo status
if (SVO_ENABLE)
{
if ( pid.enable == 0 ) // last loop, servo was off
{
pid.enable = 1;
printf("servo-enabled\r\n>");
// give the servo loop some time to get established
timer_test = 2500; while ( timer_test );
}
}
else
{
if ( pid.enable == 1 ) // last loop servo was active
{
pid.enable = 0;
printf("servo-disabled\r\n>");
}
}
}
// to keep compiler happy....
return 0;
}作者: keelai8 时间: 2011-7-12 12:02 标题: 这个程序也很适合入门深入式开发. 这是最先的几行,有一个亮指示灯的动作,所以第一步就先焊好硬件,调试以下入门级的程序先.
int main(void)
{
int cs;
// vars used for detection of incremental motion
jerk = 0.0;
setup_io(); // make all i/o pins go the right dir
STATUS_LED = 0; // led on
}
后面的几节就是关于指这个硬件的了.作者: 穷人穷玩法 时间: 2011-7-12 12:18
发这个大部分人看不懂也没兴趣,要引人入胜,就得先发大家都感兴趣的内容。作者: qwbhbwz 时间: 2011-7-12 12:34
不错的记录,跟车学习,顶一下:em15:作者: lysgyx 时间: 2011-7-12 12:44
看到一大堆代码,现在就怕看这个,我:em15: 闪作者: tomhare 时间: 2011-7-12 15:39
厉害的楼主,伺服贵就贵在软件上啊,硬件都公式化了作者: 半桶水 时间: 2011-7-12 19:54
看不懂,等成品作者: jun139993 时间: 2011-7-12 20:15
捡到一本武林秘籍,无奈看不懂 , 哎:em25:作者: XJKSZC 时间: 2011-7-12 22:17
软硬兼施才是高手 ,学习了作者: 书生一个 时间: 2011-7-12 23:06
看天书作者: keelai8 时间: 2011-7-12 23:51
哈哈,其实早就有把程序组合成图形的软件了,一拉就生成代码,不用看的.
伺服方案现在也不贵,很多人都能买到成套生产方案带样机.关键是成本控制问题,另外也要有一定看代码调试能力,
对于写代码我的年龄已经太老,只能玩这些简单的东西了.作者: l10658f 时间: 2011-7-13 00:54
囫囵吞枣总比不看强,支持下。作者: johnny_cl 时间: 2011-7-13 08:33
看不懂!代码看着头晕!作者: lizhijun 时间: 2011-7-13 09:18
强力支持