Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Dynamixel SerialHalfDuplex mbed
main.cpp
00001 /*YOZAKURAのアームコード 00002 シリアル通信で送るデータ S_Data={linearD[4],pitchD[4],yawD[4],ThermoD1[16],ThermoD2[16],CO2[4]} 計48bytes 00003 シリアル通信で受け取るデータ R_Data={mode[2bit],linear_ref[2bit],pitch_ref[2bit],yaw_ref[2bit]} 計1byte 00004 */ 00005 00006 #include "mbed.h" 00007 00008 Serial pc(USBTX, USBRX); // tx, rx 00009 DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4); 00010 00011 /*--Dynamixel:begin-----------------------------------------------------------------------------------------*/ 00012 #include "AX12.h" 00013 #include "MX28.h" 00014 00015 DigitalOut low(p16); DigitalOut RelaySwitch(p18); 00016 00017 AX12 linear (p13, p14, 0, 1000000); //直動Dynamixel 00018 MX28 pitch (p13, p14, 1, 1000000); //ピッチDynamixel 00019 MX28 yaw (p13, p14, 2, 1000000); //ヨーDynamixel 00020 00021 int linear_goal, pitch_goal, yaw_goal; 00022 00023 //最小値,最大値,角速度,初期値を指定[unit:degree] 00024 //MX:MultiTurnモードでは-2520°~2520°(SetGoalの書き換え不要) 00025 int linear_min = 100; int linear_MAX = 300; float linear_Speed=0.1; int linear_Init = linear_MAX; 00026 int pitch_min = 172; int pitch_MAX = 334; float pitch_Speed=0.2; int pitch_Init = pitch_MAX; 00027 int yaw_min = 360; int yaw_MAX = 360; float yaw_Speed=0.2; int yaw_Init = 0; //MultiTurnモード 00028 00029 void Dyna_init() { 00030 low = 0; RelaySwitch = 1; 00031 printf("linear_Init\n"); 00032 linear.SetCWLimit(linear_min); linear.SetCCWLimit(linear_MAX); 00033 linear.SetCRSpeed(linear_Speed); linear_goal=linear_Init; linear.SetGoal(linear_Init); 00034 printf("pitch_Init\n"); 00035 pitch.SetCWLimit(pitch_min); pitch.SetCCWLimit(pitch_MAX); 00036 pitch.SetCRSpeed(pitch_Speed); pitch_goal=pitch_Init; pitch.SetGoal(pitch_Init); 00037 printf("yaw_Init\n"); 00038 yaw.SetCWLimit(yaw_min); yaw.SetCCWLimit(yaw_MAX); 00039 yaw.SetCRSpeed(yaw_Speed); yaw_goal=yaw_Init; yaw.SetGoal(yaw_Init); 00040 wait(1); 00041 } 00042 00043 void Dyna_GetData(char* data) { 00044 float lP, lV, pP, pC, yP, yC; 00045 00046 lP = linear.GetPosition(); lV = linear.GetVolts(); 00047 pP = pitch.GetPosition(); pC = pitch.GetCurrent(); 00048 yP = yaw.GetPosition(); yC = yaw.GetCurrent(); 00049 00050 linear_goal=lP; pitch_goal=pP; yaw_goal=yP; //現在角度を目標値に設定 00051 printf("\nlinearPosition : %4.1f[degree]\n",lP); printf("Voltage : %4.1f[V]\n",lV); 00052 printf("pitchPosition : %4.1f[degree]\n",pP); printf("pitchCurrent : %4.1f[mA]\n",pC); 00053 printf("yawPosition : %4.1f[degree]\n",yP); printf("yawCurrent : %4.1f[mA]\n",yC); 00054 sprintf(data,"%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f ",lP,lV,pP,pC,yP,yC); 00055 } 00056 00057 void Dyna_SetGoal(int linear_mode, int pitch_mode, int yaw_mode) { 00058 switch(linear_mode){ 00059 case 29: break; 00060 case 30: linear.SetTorqueLimit(1); linear_goal-=2; break; 00061 case 31: linear.SetTorqueLimit(1); linear_goal+=2; break; 00062 } 00063 if(linear_goal>linear_MAX) linear_goal=linear_MAX; 00064 if(linear_goal<linear_min) linear_goal=linear_min; 00065 linear.SetGoal(linear_goal); 00066 00067 switch(pitch_mode){ 00068 case 29: break; 00069 case 30: pitch_goal-=3; 00070 if(pitch_goal>pitch_MAX) pitch_goal=pitch_MAX; 00071 break; 00072 case 31: pitch_goal+=3; 00073 if(pitch_goal<pitch_min) pitch_goal=pitch_min; 00074 break; 00075 } 00076 if(pitch_goal>pitch_MAX) pitch_goal=pitch_MAX; 00077 if(pitch_goal<pitch_min) pitch_goal=pitch_min; 00078 pitch.SetGoal(pitch_goal); 00079 00080 switch(yaw_mode){ 00081 case 29: break; 00082 case 30: yaw_goal-=3; break; 00083 case 31: yaw_goal+=3; break; 00084 } 00085 if(pitch_goal>2520) pitch_goal=2520; 00086 if(pitch_goal<-2520) pitch_goal=-2520; 00087 yaw.SetGoal(yaw_goal); 00088 } 00089 /*--Dynamixel:end-----------------------------------------------------------------------------------------*/ 00090 00091 00092 /*--Thermal_Sensor:begin----------------------------------------------------------------------------------*/ 00093 /*MEMS非接触温度センサ:形D6T-44L-06 4×4素子タイプ*/ 00094 /*データシート:http://www.omron.co.jp/ecb/products/sensor/special/mems/pdf/AN-D6T-01JP_r2.pdf*/ 00095 00096 I2C MEMS1(p9, p10); // sda, scl 00097 I2C MEMS2(p28, p27); // sda, scl 00098 00099 #define D6T_addr 0x14 00100 #define D6T_cmd 0x4c 00101 00102 #define THERMO_DEBUG 1 00103 00104 char I2C_rd1[64]; // 生データ 00105 short datr1[16]; // 16点 温度データ(10倍整数) 00106 short PTAT1; // センサ内部PTAT温度データ(10倍整数) 00107 double dt1[16]; // 16点 温度データ 00108 double dt1_temp[16]; 00109 short d_PTAT1; // センサ内部PTAT温度データ 00110 00111 char I2C_rd2[64]; // 生データ 00112 short datr2[16]; // 16点 温度データ(10倍整数) 00113 short PTAT2; // センサ内部PTAT温度データ(10倍整数) 00114 double dt2[16]; // 16点 温度データ 00115 double dt2_temp[16]; 00116 short d_PTAT2; // センサ内部PTAT温度データ 00117 00118 void GetThermo(char* data) { 00119 char con[6]; 00120 sprintf(data,""); 00121 00122 /*MEMS1*/ 00123 int i,j; 00124 int itemp; 00125 00126 //// measure 00127 MEMS1.start(); 00128 MEMS1.write(D6T_addr); 00129 MEMS1.write(D6T_cmd); 00130 // Repeated Start condition 00131 MEMS1.read(D6T_addr,I2C_rd1,35); 00132 // if(check_PEC(I2C_rd) == -1) continue; // error 00133 for(i=0,j=0;i<17;i++){ 00134 itemp = (I2C_rd1[j++] & 0xff); 00135 itemp += I2C_rd1[j++] * 256; 00136 if(i == 0) PTAT1 = itemp; 00137 else datr1[i-1] = itemp; 00138 } 00139 for(i=0;i<16;i++){ 00140 dt1[i] = 0.1 * datr1[i]; 00141 } 00142 for(int i=0;i<16;i++){ 00143 if(dt1[i]<50 && dt1[i]>5) { 00144 sprintf(con,"%3.1f ",dt1[i]); 00145 dt1_temp[i]=dt1[i]; 00146 } 00147 else sprintf(con,"%3.1f ",dt1_temp[i]); 00148 strcat(data,con); 00149 } 00150 if(THERMO_DEBUG){ 00151 printf("\nThermal_Sensor 1"); 00152 for(i=0;i<16;i++){ 00153 if(i%4==0) printf("\n"); 00154 printf("%3.1f ",dt1[i]); 00155 } printf("\n"); 00156 } 00157 d_PTAT1 = 0.1 * PTAT1; 00158 wait(0.1); 00159 /*MEMS1*/ 00160 00161 /*MEMS2*/ 00162 //// measure 00163 MEMS2.start(); 00164 MEMS2.write(D6T_addr); 00165 MEMS2.write(D6T_cmd); 00166 // Repeated Start condition 00167 MEMS2.read(D6T_addr,I2C_rd2,35); 00168 // if(check_PEC(I2C_rd) == -1) continue; // error 00169 for(i=0,j=0;i<17;i++){ 00170 itemp = (I2C_rd2[j++] & 0xff); 00171 itemp += I2C_rd2[j++] * 256; 00172 if(i == 0) PTAT2 = itemp; 00173 else datr2[i-1] = itemp; 00174 } 00175 for(i=0;i<16;i++){ 00176 dt2[i] = 0.1 * datr2[i]; 00177 } 00178 for(int i=0;i<16;i++){ 00179 if(dt1[i]<50 && dt1[i]>5) { 00180 sprintf(con,"%3.1f ",dt2[i]); 00181 dt2_temp[i]=dt2[i]; 00182 } 00183 else sprintf(con,"%3.1f ",dt2_temp[i]); 00184 strcat(data,con); 00185 } 00186 if(THERMO_DEBUG){ 00187 printf("\nThermal_Sensor 2"); 00188 for(i=0;i<16;i++){ 00189 if(i%4==0) printf("\n"); 00190 printf("%3.1f ",dt2[i]); 00191 } printf("\n"); 00192 } 00193 d_PTAT2 = 0.1 * PTAT2; 00194 wait(0.1); 00195 /*MEMS2*/ 00196 } 00197 /*--Thermal_Sensor:end------------------------------------------------------------------------------------*/ 00198 00199 00200 /*--CO2_Sensor:begin--------------------------------------------------------------------------------------*/ 00201 /*CO2センサモジュール:A051020-AQ6B-01*/ 00202 /*データシート:http://www.fisinc.co.jp/common/pdf/A051020-AQ6.pdf*/ 00203 /*参考 外気:396.0[ppm](2013年) 呼気:13,200[ppm] ※このセンサで測れるのは3000[ppm]まで*/ 00204 #include "mbed.h" 00205 00206 AnalogIn ain(p20); 00207 00208 #define CO2_DEBUG 1 00209 00210 float GetCO2() { 00211 float v; //生データ:電圧 00212 float sensor_v,CO2; 00213 00214 v = ain.read()*3.3; 00215 sensor_v = v * 5.0/3.3; //電圧レベルを合わせる 00216 CO2 = sensor_v * 1000 + 400; //データシートより 00217 00218 if(CO2_DEBUG) printf("\nCO2:%4.1f\n",CO2); 00219 00220 return(CO2); 00221 } 00222 /*--CO2_Sensor:end----------------------------------------------------------------------------------------*/ 00223 00224 00225 /*--------------- 00226 MAIN ROOP 00227 ----------------*/ 00228 int main() { 00229 char Dyna_data[50]; 00230 char Thermo_data[200]; 00231 float CO2_data; 00232 00233 Dyna_init(); 00234 00235 char Send_data[250]; 00236 int Read_data; 00237 int Joy_mode, linear_mode, pitch_mode, yaw_mode; 00238 00239 while(1) { 00240 00241 /*--Dynamixel:begin---------------------------------------------------------------*/ 00242 // //Dynamixelへの命令を判定 00243 // linear_mode = R_data & 0x0c; linear_mode = linear_mode >> 2; 00244 // pitch_mode = R_data & 0x30; pitch_mode = pitch_mode >> 4; 00245 // yaw_mode = R_data & 0xc0; yaw_mode = yaw_mode >> 6; 00246 00247 //現在の角度・電圧・電流を取得 00248 myled1=1; 00249 Dyna_GetData(Dyna_data); 00250 myled1=0; 00251 00252 printf("linear_mode : "); 00253 linear_mode=pc.getc(); 00254 printf("%d\n",linear_mode); 00255 printf("pitch_mode : "); 00256 pitch_mode=pc.getc(); 00257 printf("%d\n",pitch_mode); 00258 printf("yaw_mode : "); 00259 yaw_mode=pc.getc(); 00260 printf("%d\n",yaw_mode); 00261 00262 //目標角度を変更 00263 myled2=1; 00264 Dyna_SetGoal(linear_mode, pitch_mode, yaw_mode); 00265 myled2=0; 00266 /*--Dynamixel:end------------------------------------------------------------------*/ 00267 00268 /*--Thermal_Sensor:begin-----------------------------------------------------------*/ 00269 //値を取得 00270 myled3=1; 00271 GetThermo(Thermo_data); 00272 /*--Thermal_Sensor:end-------------------------------------------------------------*/ 00273 00274 /*--CO2_Sensor:begin---------------------------------------------------------------*/ 00275 //値=”” 00276 CO2_data=GetCO2(); 00277 myled3=0; 00278 /*--CO2_Sensor:end-----------------------------------------------------------------*/ 00279 00280 //値を送信 00281 myled4=1; 00282 printf("\nDyna_data : %s\n",Dyna_data); printf("Thermo_data : %s\n",Thermo_data); 00283 sprintf(Send_data,"%s%s%4.1f",Dyna_data,Thermo_data,CO2_data); 00284 printf("Send_data : %s\n",Send_data); 00285 myled4=0; 00286 00287 } 00288 }
Generated on Fri Jul 22 2022 03:56:09 by
1.7.2