//定时计数器1中断处理函数 void timer1_interrupt_handler(void) { uchar msg_force_array[]="zz21w"; force_ulong=ReadCount();//读取数据放入全局变量 ulong_to_uchar_array(force_ulong);//数据类型转换 msg_force_array[4]=force_high8; uart0_send_string(msg_force_array);//向上位机发送夹紧力实时数值(仅有效值高八位) TCNT1=0xC2F6;//需要重新设定周期500ms }
/* READ a CStringArray object from input file 'fid'. This function read a CStringArray object from input file 'fid'. 'fid' was returned by 'fopen' */ mxArray * ReadCStringArray( FILE * fp ) { unsigned int dCount = ReadCount( fp ); int i; mxArray * aString; aString = mxCreateCellMatrix( dCount, 1 ); for( i = 0; i < (int)dCount; i ++ ) mxSetCell( aString, i, ReadCString( fp ) ); return aString; }
/* READ a CArray object from input file "fp". */ mxArray * ReadCArray( FILE * fp, int type ) { int dims[2]; unsigned long dCount = ReadCount( fp ); mxArray * array; int unit_size; dims[0] = dCount; dims[1] = 1; switch( type ) { case mxCHAR_CLASS: case mxINT8_CLASS: case mxUINT8_CLASS: unit_size = 1; break; case mxINT16_CLASS: case mxUINT16_CLASS: unit_size = 2; break; case mxUINT32_CLASS: case mxINT32_CLASS: case mxSINGLE_CLASS: unit_size = 4; break; case mxDOUBLE_CLASS: case mxINT64_CLASS: case mxUINT64_CLASS: unit_size = 8; break; } array = mxCreateNumericArray( 2, dims, type, mxREAL ); if (dCount > 0) fread( mxGetData(array), unit_size, dCount, fp ); return array; }
if (str == NULL) { WriteCount (0); } else { DWORD size = strlen (str) + 1; WriteCount (size); Write (str, size - 1); } return *this; } FArchive &FArchive::operator>> (std::string &s) { DWORD size = ReadCount (); if (size == 0) s = ""; else { char *cstr = new char[size]; size--; Read (cstr, size); cstr[size] = 0; s = cstr; delete[] cstr; } return *this; } FArchive &FArchive::operator<< (BYTE c)
//---------------------------【主函数】↓----------------------------------------------------------- void main(void) { //.....................[函数体内变量声明]............................... uchar i;//清空指令存储变量时所用循环的计数变量 //向舵机发送移动命令时所用的变量(下面2个)↓ uchar motor_command[9]={0xff,0xff,0x00,0x05,0x03,0x20,0x00,0x00,0x00};// uchar CHECK;//舵机指令最后一位校验码 //上电手指复位阶段用到的变量(下面2个)↓ //保证舵机停止命令只进行一次 uchar cage_0=1; uchar cage_1=1; //正常工作模式(模式一)手指松开阶段用到的变量(下面2个)↓ //保证每次夹紧操作时舵机停止命令只进行一次 uchar approach_0; uchar approach_1; //夹紧第二阶段的while循环跳出指示 uchar hold_stage_2_continue=1; //大循环中查询两端限位和空夹所用到的变量(下面7个)↓ //while大循环从两端限位后开始,因此不允许再次发送舵机停止命令,而是等待限位结束 uchar stop_allow_cage_0=0; uchar stop_allow_cage_1=0; //不允许手指向端部移动,允许手指向中间移动,设置四个变量是因为考虑到两端限位跟空夹 uchar release_allow_motor_0=0; uchar release_allow_motor_1=0; uchar hold_allow_motor_0=1; uchar hold_allow_motor_1=1; //允许上电复位后就开始检测是否空夹 uchar stop_allow_empty=1; //调试模式手指移动速度,初始值为通过串口设定前的默认值 uchar PARA2=0x10,PARA3=0x01;//保证低速,仅允许通过串口命令更改其值! //一般模式手指移动速度,仅允从EEPROM中获取数值 uchar com_finger0_ratio_1_PARA2,com_finger0_ratio_1_PARA3;//手指0,Ratio 1 uchar com_finger0_ratio_2_PARA2,com_finger0_ratio_2_PARA3;//手指0,Ratio 2 uchar com_finger0_ratio_3_PARA2,com_finger0_ratio_3_PARA3;//手指0,Ratio 3 uchar com_finger1_ratio_1_PARA2,com_finger1_ratio_1_PARA3;//手指1,Ratio 1 uchar com_finger1_ratio_2_PARA2,com_finger1_ratio_2_PARA3;//手指1,Ratio 2 uchar com_finger1_ratio_3_PARA2,com_finger1_ratio_3_PARA3;//手指1,Ratio 3 uchar msg_eeprom_array[17];//向上位机返回EEPROM中数值 uchar msg_interrupt_array[7]={'z','z','4','4','0','0','0'};//向上位机返回夹持器与外部碰撞报警(中断)允许变量的值 //.......................[初始化配置]......................... uart0_init(19200);//串口0(与上位机通信)初始化,波特率均为19200 uart1_init(19200);//串口1(与舵机通信)初始化,波特率均为19200 timer1_init();//定时计数器1初始化 force_data_init();//应变片读取初始化 //限位的配置 //PE2=接近开关0,高阻态输入 DDRE&=(~BIT(2));//DDRE2=0 PORTE&=(~BIT(2));//PORTE2=0 //PE3=接近开关1,高阻态输入 DDRE&=(~BIT(3));//DDRE2=0 PORTE&=(~BIT(3));//PORTE2=0 //PE5=INT5=限位0,带上拉电阻输入 DDRE&=(~BIT(5));//意思是DDRE5=0,其余位不变。但注意不可按注释的方式写! PORTE|=BIT(5);//意思是PORTE5=1,其余位不变。但注意不可按注释的方式写! //PE6=INT6=限位1,带上拉电阻输入 DDRE&=(~BIT(6)); PORTE|=BIT(6); //PE7=空夹,带上拉电阻输入 DDRE&=(~BIT(7)); PORTE|=BIT(7); //................[功能:上电后手指复位].................................... SREG |= 0X80;//打开全局中断 //相关变量初始化 cage0_state=0; cage1_state=0; //命令舵机停止转动 uart1_send_string((uchar*)no0stop,9); delay(50); uart1_send_string((uchar*)no1stop,9); delay(50); //使手爪松开 uart1_send_string((uchar*)no0release,9); delay(50); uart1_send_string((uchar*)no1release,9); //等待两端限位触发 while(cage_0|cage_1) { if(cage_0) { if(!(PINE & BIT(5)))//PE5=0进入 { delay(50); if(!(PINE & BIT(5))) { uart1_send_string((uchar*)no0stop,9); //uart0_send_string("zz30"); cage_0=0; } } } if(cage_1) { if(!(PINE & BIT(6)))//PE6=0进入 { delay(50); if(!(PINE & BIT(6))) { uart1_send_string((uchar*)no1stop,9); //uart0_send_string("zz31"); cage_1=0; } } } } uart0_send_string("zz00");//向上位机报告准备就绪 UCSR0B|=(1<<RXEN0)|(1<<RXCIE0); //UART0接收使能,接收中断使能 //........................[while(1)大循环]............................................ while(1) { if(uart0_instr_flag==1) { switch(gripper_mood) { case 0: { if(array_cmp(uart0_instr,"0100")==0) { //uart0_send_string(" mood 0: enter 1-regular working mood! "); gripper_mood=1; ext_interrupt_init();//外部中断(夹持器与外部环境碰撞)初始化 //初始化后会立即出发一次INT0和INT1,所以报警允许变量需要先置0再置1 //夹持器与外部碰撞报警(中断)允许变量 ext_collision_alert_allow_int0=1;//上侧 ext_collision_alert_allow_int1=1;//下侧 ext_collision_alert_allow_int4=1;//指尖 //获取EEPROM中存储的RATIO值 command_data_read_finger_0_ratio_1(&com_finger0_ratio_1_PARA2,&com_finger0_ratio_1_PARA3); command_data_read_finger_0_ratio_2(&com_finger0_ratio_2_PARA2,&com_finger0_ratio_2_PARA3); command_data_read_finger_0_ratio_3(&com_finger0_ratio_3_PARA2,&com_finger0_ratio_3_PARA3); command_data_read_finger_1_ratio_1(&com_finger1_ratio_1_PARA2,&com_finger1_ratio_1_PARA3); command_data_read_finger_1_ratio_2(&com_finger1_ratio_2_PARA2,&com_finger1_ratio_2_PARA3); command_data_read_finger_1_ratio_3(&com_finger1_ratio_3_PARA2,&com_finger1_ratio_3_PARA3); //获取EEPROM中存储的夹紧力阈值有效值高八位 command_data_read_force_high8(&force_judge); } if(array_cmp(uart0_instr,"0200")==0) { //uart0_send_string(" mood 0: enter 2-configuration mood! "); gripper_mood=2; ext_interrupt_init();//外部中断(夹持器与外部环境碰撞)初始化 //初始化后会立即出发一次INT0和INT1,所以报警允许变量需要先置0再置1 //夹持器与外部碰撞报警(中断)允许变量 ext_collision_alert_allow_int0=1;//上侧 ext_collision_alert_allow_int1=1;//下侧 ext_collision_alert_allow_int4=1;//指尖 } break; } case 1: { if(array_cmp(uart0_instr,"1000")==0)//调试模式·读取EEPROM中存储的RATIO和力阈值 { //更新ratio变量值、力阈值和消息数组的值 //获取EEPROM中存储的RATIO值 command_data_read_finger_0_ratio_1(&com_finger0_ratio_1_PARA2,&com_finger0_ratio_1_PARA3); command_data_read_finger_0_ratio_2(&com_finger0_ratio_2_PARA2,&com_finger0_ratio_2_PARA3); command_data_read_finger_0_ratio_3(&com_finger0_ratio_3_PARA2,&com_finger0_ratio_3_PARA3); command_data_read_finger_1_ratio_1(&com_finger1_ratio_1_PARA2,&com_finger1_ratio_1_PARA3); command_data_read_finger_1_ratio_2(&com_finger1_ratio_2_PARA2,&com_finger1_ratio_2_PARA3); command_data_read_finger_1_ratio_3(&com_finger1_ratio_3_PARA2,&com_finger1_ratio_3_PARA3); msg_eeprom_array[0]='z'; msg_eeprom_array[1]='z'; msg_eeprom_array[2]='3'; msg_eeprom_array[3]='3'; msg_eeprom_array[4]=com_finger0_ratio_1_PARA2; msg_eeprom_array[5]=com_finger0_ratio_1_PARA3; msg_eeprom_array[6]=com_finger0_ratio_2_PARA2; msg_eeprom_array[7]=com_finger0_ratio_2_PARA3; msg_eeprom_array[8]=com_finger0_ratio_3_PARA2; msg_eeprom_array[9]=com_finger0_ratio_3_PARA3; msg_eeprom_array[10]=com_finger1_ratio_1_PARA2; msg_eeprom_array[11]=com_finger1_ratio_1_PARA3; msg_eeprom_array[12]=com_finger1_ratio_2_PARA2; msg_eeprom_array[13]=com_finger1_ratio_2_PARA3; msg_eeprom_array[14]=com_finger1_ratio_3_PARA2; msg_eeprom_array[15]=com_finger1_ratio_3_PARA3; //获取EEPROM中存储的夹紧力阈值有效值高八位 command_data_read_force_high8(&force_judge); msg_eeprom_array[16]=force_judge; delay(50); uart0_send_string_with_num(msg_eeprom_array,17);//上传EEPROM中存储的数值 } if(array_cmp(uart0_instr,"1100")==0)//松开 { if(release_allow_motor_0) { //构造舵机指令 motor_command[2]=0x00;//ID=0 motor_command[6]=com_finger0_ratio_3_PARA2; motor_command[7]=com_finger0_ratio_3_PARA3+0x04; CHECK=ratio_command_check(0,com_finger0_ratio_3_PARA2,com_finger0_ratio_3_PARA3+0x04); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 } if(release_allow_motor_1) { //构造舵机指令 motor_command[2]=0x01;//ID=1 motor_command[6]=com_finger1_ratio_3_PARA2; motor_command[7]=com_finger1_ratio_3_PARA3+0x04; CHECK=ratio_command_check(1,com_finger1_ratio_3_PARA2,com_finger1_ratio_3_PARA3+0x04); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 } } if(array_cmp(uart0_instr,"1200")==0)//夹紧 { if(hold_allow_motor_0 & hold_allow_motor_1) { //第一阶段 //构造舵机指令 motor_command[2]=0x00;//ID=0 motor_command[6]=com_finger0_ratio_1_PARA2; motor_command[7]=com_finger0_ratio_1_PARA3; CHECK=ratio_command_check(0,com_finger0_ratio_1_PARA2,com_finger0_ratio_1_PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 //构造舵机指令 motor_command[2]=0x01;//ID=1 motor_command[6]=com_finger1_ratio_1_PARA2; motor_command[7]=com_finger1_ratio_1_PARA3; CHECK=ratio_command_check(1,com_finger1_ratio_1_PARA2,com_finger1_ratio_1_PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 approach_0=1; approach_1=1; //等接近开关触发,同时查询检测是否空夹 while((approach_0|approach_1)&&(PINE & BIT(7))) { if(approach_0) { if(!(PINE & BIT(2)))//PE2=0进入 { delay(50); if(!(PINE & BIT(2))) { uart1_send_string((uchar*)no0stop,9); //uart0_send_string(" interrupt 5 "); approach_0=0; } } } if(approach_1) { if(!(PINE & BIT(3)))//PE3=0进入 { delay(50); if(!(PINE & BIT(3))) { uart1_send_string((uchar*)no1stop,9); //uart0_send_string(" interrupt 6 "); approach_1=0; } } } } if(PINE & BIT(7))//不空夹则进行第二阶段 { //第二阶段 //构造舵机指令 motor_command[2]=0x00;//ID=0 motor_command[6]=com_finger0_ratio_2_PARA2; motor_command[7]=com_finger0_ratio_2_PARA3; CHECK=ratio_command_check(0,com_finger0_ratio_2_PARA2,com_finger0_ratio_2_PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 //构造舵机指令 motor_command[2]=0x01;//ID=1 motor_command[6]=com_finger1_ratio_2_PARA2; motor_command[7]=com_finger1_ratio_2_PARA3; CHECK=ratio_command_check(1,com_finger1_ratio_2_PARA2,com_finger1_ratio_2_PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9);//下发指令 //等待夹紧力达到阈值并继续判断是否空夹 force_high8=0; hold_stage_2_continue=1; while(hold_stage_2_continue && (PINE & BIT(7))) { force_ulong=ReadCount(); ulong_to_uchar_array(force_ulong);//目的是获取force_high8 if(force_high8>=force_judge) { hold_stage_2_continue=0;//下次循环跳出 //停止舵机运行 delay(50); uart1_send_string((uchar*)no0stop,9); delay(50); uart1_send_string((uchar*)no1stop,9); hold_allow_motor_0=0;//禁止0号手指向端部靠近 hold_allow_motor_1=0;//禁止1号手指向端部靠近 //报告上位机已经夹紧 delay(50); uart0_send_string("zz10"); delay(50); uart0_send_string("zz10"); } } } } } if(array_cmp(Type(uart0_instr),"13")==0)//设定手指0,Ratio 1 { com_finger0_ratio_1_PARA2=uart0_instr[2]; com_finger0_ratio_1_PARA3=uart0_instr[3]; command_data_save_finger_0_ratio_1(com_finger0_ratio_1_PARA2,com_finger0_ratio_1_PARA3); } if(array_cmp(Type(uart0_instr),"14")==0)//设定手指0,Ratio 2 { com_finger0_ratio_2_PARA2=uart0_instr[2]; com_finger0_ratio_2_PARA3=uart0_instr[3]; command_data_save_finger_0_ratio_2(com_finger0_ratio_2_PARA2,com_finger0_ratio_2_PARA3); } if(array_cmp(Type(uart0_instr),"15")==0)//设定手指0,Ratio 3 { com_finger0_ratio_3_PARA2=uart0_instr[2]; com_finger0_ratio_3_PARA3=uart0_instr[3]; command_data_save_finger_0_ratio_3(com_finger0_ratio_3_PARA2,com_finger0_ratio_3_PARA3); } if(array_cmp(Type(uart0_instr),"16")==0)//设定手指1,Ratio 1 { com_finger1_ratio_1_PARA2=uart0_instr[2]; com_finger1_ratio_1_PARA3=uart0_instr[3]; command_data_save_finger_1_ratio_1(com_finger1_ratio_1_PARA2,com_finger1_ratio_1_PARA3); } if(array_cmp(Type(uart0_instr),"17")==0)//设定手指1,Ratio 2 { com_finger1_ratio_2_PARA2=uart0_instr[2]; com_finger1_ratio_2_PARA3=uart0_instr[3]; command_data_save_finger_1_ratio_2(com_finger1_ratio_2_PARA2,com_finger1_ratio_2_PARA3); } if(array_cmp(Type(uart0_instr),"18")==0)//设定手指1,Ratio 3 { com_finger1_ratio_3_PARA2=uart0_instr[2]; com_finger1_ratio_3_PARA3=uart0_instr[3]; command_data_save_finger_1_ratio_3(com_finger1_ratio_3_PARA2,com_finger1_ratio_3_PARA3); } if(array_cmp(Type(uart0_instr),"19")==0)//设定force_judge { force_judge=uart0_instr[2]; command_data_save_force_high8(force_judge); } break; } case 2: { if(array_cmp(uart0_instr,"2100")==0)//调试模式·手指0停止 { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 uart1_send_string((uchar*)no0stop,9); } if(array_cmp(uart0_instr,"2101")==0)//调试模式·手指0松开方向移动 { if(release_allow_motor_0) { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 motor_command[2]=0x00;//ID=0 motor_command[6]=PARA2; motor_command[7]=PARA3+0x04;//顺时针,绝对不可在此更改PARA3的值! CHECK=ratio_command_check(0,PARA2,PARA3+0x04); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9); //delay(50); //uart0_send_string_with_num(motor_command,9); TIMSK|=BIT(2);//打开定时计数1中断,向上返回夹持力值 } } if(array_cmp(uart0_instr,"2102")==0)//调试模式·手指0夹紧方向移动 { if(hold_allow_motor_0) { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 motor_command[2]=0x00;//ID=0 motor_command[6]=PARA2; motor_command[7]=PARA3; CHECK=ratio_command_check(0,PARA2,PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9); //delay(50); //uart0_send_string_with_num(motor_command,9); TIMSK|=BIT(2);//打开定时计数1中断,向上返回夹持力值 } } if(array_cmp(uart0_instr,"2110")==0)//调试模式·手指1停止 { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 uart1_send_string((uchar*)no1stop,9); } if(array_cmp(uart0_instr,"2111")==0)//调试模式·手指1松开方向移动 { if(release_allow_motor_1) { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 motor_command[2]=0x01;//ID=1 motor_command[6]=PARA2; motor_command[7]=PARA3+0x04;//顺时针,绝对不可在此更改PARA3的值! CHECK=ratio_command_check(1,PARA2,PARA3+0x04); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9); //delay(50); //uart0_send_string_with_num(motor_command,9); TIMSK|=BIT(2);//打开定时计数1中断,向上返回夹持力值 } } if(array_cmp(uart0_instr,"2112")==0)//调试模式·手指1夹紧方向移动 { if(hold_allow_motor_1) { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 motor_command[2]=0x01;//ID=1 motor_command[6]=PARA2; motor_command[7]=PARA3; CHECK=ratio_command_check(1,PARA2,PARA3); motor_command[8]=CHECK; delay(50); uart1_send_string(motor_command,9); //delay(50); //uart0_send_string_with_num(motor_command,9); TIMSK|=BIT(2);//打开定时计数1中断,向上返回夹持力值 } } if(array_cmp(Type(uart0_instr),"22")==0) { TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 delay(50); uart1_send_string((uchar*)no0stop,9); delay(50); uart1_send_string((uchar*)no1stop,9); PARA2=uart0_instr[2]; PARA3=uart0_instr[3]; //delay(50); //uart0_send_string("ratio changed"); } if(array_cmp(uart0_instr,"2301")==0)//调试模式·设置手指0速度1(两指相同)夹紧第一阶段 { command_data_save_finger_0_ratio_1(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-0 Ratio-1 Set Successfully! "); } if(array_cmp(uart0_instr,"2302")==0)//调试模式·设置手指0速度2(两指相同)夹紧第二阶段 { command_data_save_finger_0_ratio_2(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-0 Ratio-2 Set Successfully! "); } if(array_cmp(uart0_instr,"2303")==0)//调试模式·设置手指0速度3(两指相同)松开阶段 { command_data_save_finger_0_ratio_3(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-0 Ratio-3 Set Successfully! "); } if(array_cmp(uart0_instr,"2311")==0)//调试模式·设置手指1速度1(两指相同)夹紧第一阶段 { command_data_save_finger_1_ratio_1(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-1 Ratio-1 Set Successfully! "); } if(array_cmp(uart0_instr,"2312")==0)//调试模式·设置手指1速度2(两指相同)夹紧第二阶段 { command_data_save_finger_1_ratio_2(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-1 Ratio-2 Set Successfully! "); } if(array_cmp(uart0_instr,"2313")==0)//调试模式·设置手指1速度3(两指相同)松开阶段 { command_data_save_finger_1_ratio_3(PARA2,PARA3); //delay(50); //uart0_send_string(" Finger-1 Ratio-3 Set Successfully! "); } if(array_cmp(Type(uart0_instr),"24")==0)//调试模式·设置夹紧力阈值 { force_judge=uart0_instr[2]; command_data_save_force_high8(force_judge); //delay(50); //uart0_send_string(" Force Set Successfully! "); } if(array_cmp(uart0_instr,"2500")==0)//调试模式·读取EEPROM中存储的RATIO和力阈值 { //更新ratio变量值、力阈值和消息数组的值 //获取EEPROM中存储的RATIO值 command_data_read_finger_0_ratio_1(&com_finger0_ratio_1_PARA2,&com_finger0_ratio_1_PARA3); command_data_read_finger_0_ratio_2(&com_finger0_ratio_2_PARA2,&com_finger0_ratio_2_PARA3); command_data_read_finger_0_ratio_3(&com_finger0_ratio_3_PARA2,&com_finger0_ratio_3_PARA3); command_data_read_finger_1_ratio_1(&com_finger1_ratio_1_PARA2,&com_finger1_ratio_1_PARA3); command_data_read_finger_1_ratio_2(&com_finger1_ratio_2_PARA2,&com_finger1_ratio_2_PARA3); command_data_read_finger_1_ratio_3(&com_finger1_ratio_3_PARA2,&com_finger1_ratio_3_PARA3); msg_eeprom_array[0]='z'; msg_eeprom_array[1]='z'; msg_eeprom_array[2]='3'; msg_eeprom_array[3]='3'; msg_eeprom_array[4]=com_finger0_ratio_1_PARA2; msg_eeprom_array[5]=com_finger0_ratio_1_PARA3; msg_eeprom_array[6]=com_finger0_ratio_2_PARA2; msg_eeprom_array[7]=com_finger0_ratio_2_PARA3; msg_eeprom_array[8]=com_finger0_ratio_3_PARA2; msg_eeprom_array[9]=com_finger0_ratio_3_PARA3; msg_eeprom_array[10]=com_finger1_ratio_1_PARA2; msg_eeprom_array[11]=com_finger1_ratio_1_PARA3; msg_eeprom_array[12]=com_finger1_ratio_2_PARA2; msg_eeprom_array[13]=com_finger1_ratio_2_PARA3; msg_eeprom_array[14]=com_finger1_ratio_3_PARA2; msg_eeprom_array[15]=com_finger1_ratio_3_PARA3; //获取EEPROM中存储的夹紧力阈值有效值高八位 command_data_read_force_high8(&force_judge); msg_eeprom_array[16]=force_judge; delay(50); uart0_send_string_with_num(msg_eeprom_array,17);//上传EEPROM中存储的数值 } break; } default:break; } if(array_cmp(uart0_instr,"3100")==0)//恢复触碰报警 ext interrupt 0 { ext_collision_alert_allow_int0=1;//上侧 } if(array_cmp(uart0_instr,"3200")==0)//恢复触碰报警 ext interrupt 1 { ext_collision_alert_allow_int1=1;//下侧 } if(array_cmp(uart0_instr,"3300")==0)//恢复触碰报警 ext interrupt 4 { ext_collision_alert_allow_int4=1;//指尖 } if(array_cmp(uart0_instr,"3400")==0)//读取报警允许变量的状态 { msg_interrupt_array[4]=ext_collision_alert_allow_int0; msg_interrupt_array[5]=ext_collision_alert_allow_int1; msg_interrupt_array[6]=ext_collision_alert_allow_int4; uart0_send_string_with_num(msg_interrupt_array,7); } /*末尾应完成命令执行后的还原工作A-D*/ uart0_instr_flag=0; //A.命令接收标志位置0 uart0_r_instr_chk=0;//B.命令构造字符数计数置0 for(i=0;i<12;i++)//C.命令清除 { uart0_instr[i]=0; } UCSR0B|=BIT(RXCIE0);//D.恢复UART0的接收中断 } /* 【偏向撞击保护的编程】 (1)一旦手指碰撞到限位开关产生低电平,哪怕是抖动、不稳定的低电平, 也要禁止手指继续向碰撞位置移动,此时不需要延迟防抖的处理; (2)只有当手指真正完全地离开了碰撞地点,限位开关IO成为稳定的高电平, 才允许手指再次向碰撞的方向移动。 */ if(!(PINE & BIT(5)))//检测手指0是否复位 { if(stop_allow_cage_0) { release_allow_motor_0=0;//禁止1号手指向端部靠近 hold_allow_motor_0=1;//允许1号手指向中间靠近 TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 delay(50); uart1_send_string((uchar*)no0stop,9); delay(50); uart0_send_string("zz30"); delay(50); uart0_send_string("zz30"); stop_allow_cage_0=0; } } else { if(!stop_allow_cage_0) { delay(500); if(PINE & BIT(5)) { release_allow_motor_0=1;//允许1号手指向端部靠近 stop_allow_cage_0=1; } } } if(!(PINE & BIT(6)))//检测手指1是否复位 { if(stop_allow_cage_1) { release_allow_motor_1=0;//禁止1号手指向端部靠近 hold_allow_motor_1=1;//允许1号手指向中间靠近 TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 delay(50); uart1_send_string((uchar*)no1stop,9); delay(50); uart0_send_string("zz31"); delay(50); uart0_send_string("zz31"); stop_allow_cage_1=0; } } else { if(!stop_allow_cage_1) { delay(500); if(PINE & BIT(6)) { release_allow_motor_1=1;//允许1号手指向端部靠近 stop_allow_cage_1=1; } } } if(!(PINE & BIT(7)))//检测是否空夹 { if(stop_allow_empty) { hold_allow_motor_0=0;//禁止0号手指向端部靠近 hold_allow_motor_1=0;//禁止1号手指向端部靠近 TIMSK&=(~BIT(2));//屏蔽定时计数1中断,停止向上返回夹持力值 delay(50); uart1_send_string((uchar*)no1stop,9); delay(50); uart1_send_string((uchar*)no0stop,9); delay(50); uart0_send_string("zz32"); delay(50); uart0_send_string("zz32"); stop_allow_empty=0; } } else { if(!stop_allow_empty) { delay(500); if(PINE & BIT(7)) { hold_allow_motor_0=1;//允许0号手指向中间靠近 hold_allow_motor_1=1;//允许1号手指向中间靠近 stop_allow_empty=1; } } } } }
/* READ a CTypedPtrArray object from input file 'fp'. */ mxArray * ReadCTypedPtrArray( FILE * fp ) { unsigned short wBigObjectTag = 0x7fff; unsigned short wClassTag = 0x8000; unsigned long dwBigClassTag = 0x80000000; unsigned short wNewClassTag = 0xffff; unsigned long dCount = ReadCount( fp ); int i, n; unsigned short wTag, wSchema, wLen, wClassIndex; unsigned long obTag; char szName[80]; mxArray * array; char ** className; char * paramFields[] = { "m_szName", "m_szData" }; char * trialFields[] = { "m_dTimeStamp", "m_wFigDelay", "m_dTime", "m_fA_X", "m_fB_X", "m_fR_X", "m_fA_Y", "m_fB_Y", "m_fR_Y", "m_nItem", "m_nSet", "m_nStatus", "m_nResponseTime", "m_nV1", "m_nV2", "m_nV3", "m_ucActiveChannel", "m_szComment", "m_wSpikeEvent", "m_dSpikeTime", "m_wEyePointX", "m_wEyePointY", "m_wEyePointTime", "m_dKeyTime", "m_nKeyAction", "m_dFPTime", "m_bFPAction", "m_nLFPSampleRate", "m_nLFPChannels", "m_nLFPGain", "m_nLFPSample" }; className = (char**)mxCalloc( sizeClassName + dCount + 1, sizeof(char*) ); memcpy( className, ClassName, sizeClassName * sizeof(char*) ); mxFree( ClassName ); ClassName = className; for( i = 0; i < (int)dCount; i ++ ) { fread( &wTag, 2, 1, fp ); if( wTag == wBigObjectTag ) fread( &obTag, 4, 1, fp ); else obTag = ((wTag & wClassTag) << 16) | (wTag & ~wClassTag); if( wTag == wNewClassTag ) { fread( &wSchema, 2, 1, fp ); fread( &wLen, 2, 1, fp ); fread( szName, wLen, 1, fp ); szName[wLen] = '\0'; sizeClassName ++; ClassName[sizeClassName] = mxCalloc( strlen( szName ) + 1, sizeof(char) ); strcpy( ClassName[sizeClassName], szName ); sizeClassName ++; } else { wClassIndex = (unsigned short)(obTag & ~dwBigClassTag); strcpy( szName, ClassName[wClassIndex] ); sizeClassName ++; } if( strcmp( szName, "CParam" ) == 0 ) { if( i == 0 ) { n = sizeof(paramFields)/sizeof(char*); array = mxCreateStructMatrix( dCount, 1, n, paramFields ); } ReadCParam( array, i, fp ); } else if( strcmp( szName, "CTrial" ) == 0 ) { if( i == 0 ) { n = sizeof(trialFields)/sizeof(char*); array = mxCreateStructMatrix( dCount, 1, n, trialFields ); } if(!ReadCTrial( array, i, fp )) break; } else printf( "Unknown data format.\n" ); } return array; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { unsigned short wDataFormat; mxArray ** nTestType = &plhs[0]; mxArray ** szComment = &plhs[1]; mxArray ** nSpikeChannel = &plhs[2]; mxArray ** aTestType = &plhs[3]; mxArray ** aExpParam = &plhs[4]; mxArray ** aBehavParam = &plhs[5]; mxArray ** aGraphParam = &plhs[6]; mxArray ** aTestSeries = &plhs[7]; mxArray ** aTrial = &plhs[8]; mxArray ** aFOREP = &plhs[9]; mxArray ** aResponseTime = &plhs[10]; mxArray ** aTrialStatus = &plhs[11]; mxArray ** aDepth2Match = &plhs[12]; mxArray ** aCursorDepth = &plhs[13]; mxArray ** ucMajorChannel= &plhs[14]; mxArray ** nCellNum = &plhs[15]; mxArray ** ucElectrodeNum= &plhs[16]; mxArray ** nElectrodeDepth=&plhs[17]; mxArray ** fSpikeShape = &plhs[18]; mxArray ** nSpikeShapeTimes = &plhs[19]; mxArray ** dSpikeShapeTimestamp = &plhs[20]; mxArray ** nSpikeShapeSampleRate = &plhs[21]; int dims[2]; FILE * fp; char * szFileName; size_t buflen = mxGetM(prhs[0]) * mxGetN(prhs[0]) + 1; unsigned long dwCount; int i, j; mxArray * array; szFileName = mxCalloc( buflen, sizeof(char) ); mxGetString(prhs[0], szFileName, buflen); if( !(fp = fopen( szFileName, "rb" )) ) { printf( "Can't open file!\n" ); return; } setvbuf( fp, NULL, _IOFBF, 32000 ); dims[0] = 1; dims[1] = 1; *nTestType = mxCreateNumericArray( 2, dims, mxINT32_CLASS, mxREAL ); *nSpikeChannel = mxCreateNumericArray( 2, dims, mxINT32_CLASS, mxREAL ); *ucMajorChannel = mxCreateNumericArray( 2, dims, mxUINT8_CLASS, mxREAL ); *nSpikeShapeSampleRate = mxCreateNumericArray( 2, dims, mxINT32_CLASS, mxREAL ); dims[1] = 8; *nCellNum = mxCreateNumericArray( 2, dims, mxINT16_CLASS, mxREAL ); *ucElectrodeNum = mxCreateNumericArray( 2, dims, mxUINT8_CLASS, mxREAL ); *nElectrodeDepth = mxCreateNumericArray( 2, dims, mxINT16_CLASS, mxREAL ); fread( &wDataFormat, 2, 1, fp ); if(wDataFormat > 10) { printf("The format of underlining VLab file is newer than expected! !\n"); /*this piece of code actually reads data up to wDataFormat = 2*/ /*return;*/ } fread( mxGetData(*nTestType), 4, 1, fp ); if( wDataFormat == 0 || wDataFormat >= 2 ) *szComment = ReadCString( fp ); else *szComment = mxCreateString( mxCalloc( 1, sizeof(char) ) ); fread( mxGetData(*nSpikeChannel), 4, 1, fp ); if( wDataFormat > 0 ) { fread( mxGetData(*ucMajorChannel), 1, 1, fp ); fread( mxGetData(*nCellNum), 2, 8, fp ); fread( mxGetData(*ucElectrodeNum), 1, 8, fp ); fread( mxGetData(*nElectrodeDepth), 2, 8, fp ); } *aTestType = ReadCStringArray( fp ); ClassName = (char**)mxCalloc( 1, sizeof(char*) ); sizeClassName = 0; *aExpParam = ReadCTypedPtrArray( fp ); *aBehavParam = ReadCTypedPtrArray( fp ); *aGraphParam = ReadCTypedPtrArray( fp ); *aTestSeries = ReadCStringArray( fp ); *aTrial = ReadCTypedPtrArray( fp ); *aFOREP = ReadCArray( fp, mxINT32_CLASS ); *aResponseTime = ReadCArray( fp, mxINT32_CLASS ); *aTrialStatus = ReadCArray( fp, mxINT32_CLASS ); *aDepth2Match = ReadCArray( fp, mxINT32_CLASS ); *aCursorDepth = ReadCArray( fp, mxINT32_CLASS ); /* spike shape */ if (wDataFormat >= 10) { *fSpikeShape = mxCreateCellMatrix(8, 1); *nSpikeShapeTimes = mxCreateCellMatrix(8, 1); for (i = 0; i < 8; i ++) { dwCount = ReadCount( fp ); array = mxCreateCellMatrix(dwCount, 1); for (j = 0; j < (int)dwCount; j ++) mxSetCell( array, j, ReadCArray(fp, mxSINGLE_CLASS)); mxSetCell(*fSpikeShape, i, array); mxSetCell(*nSpikeShapeTimes, i, ReadCArray(fp, mxINT32_CLASS)); } *dSpikeShapeTimestamp = ReadCArray(fp, mxUINT32_CLASS); dims[1] = 1; *nSpikeShapeSampleRate = mxCreateNumericArray( 2, dims, mxINT32_CLASS, mxREAL ); fread(mxGetData(*nSpikeShapeSampleRate), 4, 1, fp ); } else { *fSpikeShape = mxCreateCellMatrix(8, 1); *nSpikeShapeTimes = mxCreateCellMatrix(8, 1); dims[1] = 1; *dSpikeShapeTimestamp = mxCreateNumericArray(2, dims, mxUINT32_CLASS, mxREAL); *nSpikeShapeSampleRate = mxCreateNumericArray(2, dims, mxINT32_CLASS, mxREAL); } fclose( fp ); mxFree( ClassName ); ClassName = NULL; }