雄介 太田 / Mbed 2 deprecated YOZAKURA_ARM

Dependencies:   Dynamixel EthernetInterface MEMS_Thermal_Sensor mbed-rtos SerialHalfDuplex mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*YOZAKURAのアームコード
00002   UDP通信で送るデータ S_Data={linearD[4],pitchD[4],yawD[4],ThermoD1[16],ThermoD2[16],CO2[4]} 計48bytes
00003   UDP通信で受け取るデータ R_Data={mode[2bit],linear_ref[2bit],pitch_ref[2bit],yaw_ref[2bit]} 計1byte
00004 */
00005 
00006 #include "mbed.h"
00007 #include "rtos.h"
00008 
00009 DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4);
00010 Mutex stdio_mutex;
00011 
00012 /*--Ethernet:begin------------------------------------------------------------------------------------------*/
00013 #include "mbed.h"
00014 #include "EthernetInterface.h" 
00015 
00016 const char* ECHO_SERVER_ADDRESS = "192.168.1.90"; 
00017 const int ECHO_SERVER_PORT = 60000;
00018 
00019 const char* CLIENT_ADDRESS = "192.168.1.100"; 
00020 const int CLIENT_PORT = 70000;
00021 
00022 EthernetInterface eth; 
00023 UDPSocket sock; 
00024 Endpoint echo_server; 
00025 
00026 void Ethernet_init() {
00027     eth.init("192.168.1.100", "255.255.255.0", ""); //  Use hard IP
00028     eth.connect(); 
00029 
00030     sock.bind(CLIENT_PORT);
00031     sock.set_blocking(false);
00032     
00033     echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00034 }
00035 /*--Ethernet:end--------------------------------------------------------------------------------------------*/
00036 
00037 
00038 /*--Dynamixel:begin-----------------------------------------------------------------------------------------*/
00039 #include "AX12.h"
00040 #include "MX28.h"
00041 
00042 DigitalOut low(p19); DigitalOut RelaySwitch(p17);
00043 
00044 AX12 linear (p13, p14, 0);      //直動Dynamixel
00045 MX28 pitch (p13, p14, 1);       //ピッチDynamixel
00046 MX28 yaw (p13, p14, 2);         //ヨーDynamixel
00047 
00048 int linear_goal, pitch_goal, yaw_goal;
00049 
00050 //最小値,最大値,角速度,初期値を指定[unit:degree]
00051 //MX:MultiTurnモードでは-2520°~2520°(SetGoalの書き換え不要)
00052 int linear_min = 100; int linear_MAX = 720; float linear_Speed=0.1; int linear_Init = linear_MAX;
00053 int pitch_min = 172; int pitch_MAX = 334; float pitch_Speed=0.2; int pitch_Init = pitch_MAX;
00054 int yaw_min = 360; int yaw_MAX = 360; float yaw_Speed=0.2; int yaw_Init = 0; //MultiTurnモード
00055 
00056 void Dyna_init() {
00057     low = 0; RelaySwitch = 1;
00058     linear.SetCWLimit(linear_min); linear.SetCCWLimit(linear_MAX); 
00059     linear.SetCRSpeed(linear_Speed); linear_goal=linear_Init; linear.SetGoal(linear_Init);
00060     pitch.SetCWLimit(linear_min); pitch.SetCCWLimit(linear_MAX); 
00061     pitch.SetCRSpeed(pitch_Speed); pitch_goal=linear_Init; pitch.SetGoal(pitch_Init);
00062     yaw.SetCWLimit(linear_min); yaw.SetCCWLimit(linear_MAX); 
00063     yaw.SetCRSpeed(yaw_Speed); yaw_goal=yaw_Init; yaw.SetGoal(yaw_Init);
00064     wait(1);
00065 }
00066 
00067 void GetDyna(char* data) {
00068     float lP, lV, pP, pC, yP, yC;
00069     
00070     lP = linear.GetPosition(); lV = linear.GetVolts();
00071     pP = pitch.GetPosition(); pC = pitch.GetCurrent();
00072     yP = yaw.GetPosition(); yC = yaw.GetCurrent();
00073     
00074     linear_goal=lP; pitch_goal=pP; yaw_goal=yP; //現在角度を目標値に設定
00075     
00076     sprintf(data,"%f %f %f %f %f %f ",lP,lV,pP,pC,yP,yC);  
00077 }
00078 
00079 void SetGoalDyna(int l_mode, int p_mode, int y_mode) {
00080     switch(l_mode){
00081         case 0: break;
00082         case 1: linear.SetTorqueLimit(1); linear_goal++; break;
00083         case 2: linear.SetTorqueLimit(1); linear_goal--; break;
00084     }    
00085     linear.SetGoal(linear_goal);
00086     
00087     switch(p_mode){
00088         case 0: break;
00089         case 1: pitch_goal++; break;
00090         case 2: pitch_goal--; break;
00091     }    
00092     pitch.SetGoal(pitch_goal);
00093     
00094     switch(y_mode){
00095         case 0: break;
00096         case 1: yaw_goal++; break;
00097         case 2: yaw_goal--; break;
00098     }
00099     yaw.SetGoal(yaw_goal);
00100 }
00101 
00102 void Dyna_home_position() {
00103     float lp;
00104     linear.SetGoal(linear_Init);
00105     lp=linear.GetPosition();
00106     if(lp > linear_MAX - 30) {  //ある程度縮んだら
00107         pitch.SetGoal(pitch_Init);
00108         yaw.SetGoal(yaw_Init);
00109     }
00110 }
00111 
00112 void Dyna_reset() {
00113     RelaySwitch = 0;
00114     wait_ms(10);
00115     RelaySwitch = 1;
00116 }
00117 
00118 void Dyna_end() {
00119     RelaySwitch = 0;
00120 }
00121 /*--Dynamixel:end-----------------------------------------------------------------------------------------*/
00122 
00123 
00124 /*--Thermal_Sensor:begin----------------------------------------------------------------------------------*/
00125 /*MEMS非接触温度センサ:形D6T-44L-06 4×4素子タイプ*/
00126 /*データシート:http://www.omron.co.jp/ecb/products/sensor/special/mems/pdf/AN-D6T-01JP_r2.pdf*/
00127 #include "MEMS.h"
00128 
00129 MEMS MEMS1(p9, p10); // sda, scl
00130 MEMS MEMS2(p28, p27); // sda, scl
00131 
00132 void GetThermo(char* data) {
00133     float ThD1[16], ThD2[16];
00134     char con[10];
00135     
00136     MEMS1.temp(ThD1);
00137     MEMS2.temp(ThD2);
00138     
00139     for(int i=0;i<16;i++){
00140         sprintf(con,"%3.1f ",ThD1[i]);
00141         strcat(data,con);
00142     }
00143     for(int i=0;i<16;i++){
00144         sprintf(con,"%3.1f ",ThD2[i]);
00145         strcat(data,con);
00146     }
00147 }
00148 /*--Thermal_Sensor:end------------------------------------------------------------------------------------*/
00149 
00150 
00151 /*--CO2_Sensor:begin--------------------------------------------------------------------------------------*/
00152 /*CO2センサモジュール:A051020-AQ6B-01*/
00153 /*データシート:http://www.fisinc.co.jp/common/pdf/A051020-AQ6.pdf*/
00154 /*参考 外気:396.0[ppm](2013年) 呼気:13,200[ppm] ※このセンサで測れるのは3000[ppm]まで*/
00155 #include "mbed.h"
00156 
00157 AnalogIn ain(p20);
00158 
00159 float GetCO2() {
00160     float v; //生データ:電圧
00161     float sensor_v,CO2;
00162     
00163     v = ain.read()*3.3;
00164     sensor_v = v * 5.0/3.3; //電圧レベルを合わせる
00165     CO2 = sensor_v * 1000 + 400;  //データシートより
00166 
00167     return(CO2);
00168 }
00169 /*--CO2_Sensor:end----------------------------------------------------------------------------------------*/
00170 
00171 
00172 /*---------------
00173     MAIN ROOP
00174 ----------------*/
00175 int main() {
00176     char Dyna_data[100];
00177     char Thermo_data[100];
00178     float CO2_data;
00179     
00180     Ethernet_init();    
00181     stdio_mutex.lock(); 
00182     Dyna_init();
00183     stdio_mutex.unlock();
00184     
00185     char S_data[200], R_data[2];
00186     int Joy_mode, linear_mode, pitch_mode, yaw_mode;
00187     
00188     while(1) {
00189         
00190         sock.receiveFrom(echo_server, R_data, sizeof(R_data)); //オペステからデータを取得
00191         wait_ms(1);
00192         int J = atoi(R_data);
00193         Joy_mode = J & 0x03; //ジョイスティックのモード判定
00194         
00195         switch(Joy_mode){
00196             case 0: //通常モード
00197                 stdio_mutex.lock();
00198             /*--Dynamixel:begin---------------------------------------------------------------*/       
00199                 //Dynamixelへの命令を判定
00200                 linear_mode = R_data[0] & 0x0c; linear_mode = linear_mode >> 2;
00201                 pitch_mode = R_data[0] & 0x30; pitch_mode = linear_mode >> 4;
00202                 yaw_mode = R_data[0] & 0xc0; yaw_mode = linear_mode >> 6;                
00203 
00204                 //現在の角度・電圧・電流を取得
00205                 myled1=1;
00206                 GetDyna(Dyna_data);
00207                 myled1=0;
00208                 
00209                 //目標角度を変更
00210                 myled2=1;
00211                 SetGoalDyna(linear_mode, pitch_mode, yaw_mode);
00212                 myled2=0;
00213             /*--Dynamixel:end------------------------------------------------------------------*/       
00214         
00215             /*--Thermal_Sensor:begin-----------------------------------------------------------*/
00216                 //値を取得
00217                 GetThermo(Thermo_data);
00218             /*--Thermal_Sensor:end-------------------------------------------------------------*/
00219         
00220             /*--CO2_Sensor:begin---------------------------------------------------------------*/
00221                 //値を取得
00222                 CO2_data=GetCO2();
00223             /*--CO2_Sensor:end-----------------------------------------------------------------*/
00224             
00225             //値を送信
00226                 myled3=1;
00227                 sprintf(S_data, "%s%s%f",Dyna_data,Thermo_data,CO2_data);
00228                 stdio_mutex.unlock();
00229                 sock.sendTo(echo_server, S_data, sizeof(S_data));
00230                 myled3=0;           
00231                 break;
00232             
00233             case 1: //ホームポジション
00234                 stdio_mutex.lock(); Dyna_home_position(); stdio_mutex.unlock(); break;
00235                 
00236             case 2: //リセット
00237                 stdio_mutex.lock(); Dyna_reset(); stdio_mutex.unlock(); break;
00238             
00239             case 3: //終了
00240                 stdio_mutex.lock(); myled4=1; Dyna_end(); stdio_mutex.unlock(); sock.close(true); eth.disconnect(); return 0;
00241         }
00242     }
00243 }