//WiFiClient 非同期 #include <ESP8266WiFi.h> const char* ssid = "SSID";//無線LANのSSIDを設定します。 const char* password = "password";//無線LANのpasswordを設定します。 const char* host = "192.168.11.2";//パソコンのIPアドレスを設定します。 const int httpPort = 13000;//TCPサーバのポート static String gSendText=""; void setup() { Serial.begin(115200);//シリアルポートを115200bpsで開始 delay(10); // We start by connecting to a WiFi network Serial.println(); WiFi.begin(ssid, password);//無線LANに接続要求 while (WiFi.status() != WL_CONNECTED) {//接続完了まで待ちます。 delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void loop() { delay(500); // Use WiFiClient class to create TCP connections WiFiClient client; if (!client.connect(host, httpPort)) {//TCPサーバへの接続要求 //Serial.print("x"); } else { if(gSendText.length() > 1) { client.print(gSendText);//データを送信 gSendText=""; } else{client.print("a");}//"a"を送信 delay(10); // Read all the lines of the reply from server and print them to Serial while(client.available()) { String line = client.readStringUntil('\n');//受信します。 Serial.print(line+"\r\n"); gSendText=line + "=OK\r\n";//送信データのセット } } }
//WIFI動くおもちゃソースプログラム #include <xc.h> #pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board) #pragma config CPUDIV = OSC1_PLL2 #pragma config USBDIV = 2 // Clock source from 96MHz PLL/2 #pragma config FOSC = HSPLL_HS #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config PWRT = OFF #pragma config BOR = ON #pragma config BORV = 3 #pragma config VREGEN = ON //USB Voltage Regulator #pragma config WDT = OFF #pragma config WDTPS = 32768 #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config PBADEN = OFF //#pragma config CCP2MX = ON #pragma config STVREN = ON #pragma config LVP = OFF //#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming #pragma config XINST = OFF // Extended Instruction Set #pragma config CP0 = OFF #pragma config CP1 = OFF //#pragma config CP2 = OFF //#pragma config CP3 = OFF #pragma config CPB = OFF //#pragma config CPD = OFF #pragma config WRT0 = OFF #pragma config WRT1 = OFF //#pragma config WRT2 = OFF //#pragma config WRT3 = OFF #pragma config WRTB = OFF // Boot Block Write Protection #pragma config WRTC = OFF //#pragma config WRTD = OFF #pragma config EBTR0 = OFF #pragma config EBTR1 = OFF //#pragma config EBTR2 = OFF //#pragma config EBTR3 = OFF #pragma config EBTRB = OFF #define _XTAL_FREQ 48000000 //__delay_ms #define SW1 PORTEbits.RE0 #define SW2 PORTEbits.RE1 #define LED PORTEbits.RE2 void init(void); char UART_Init(const long int baudrate); void UART_Write(char data); char UART_TX_Empty(); void UART_Write_Text(char *text); char UART_Data_Ready(); char UART_Read(); void UART_Read_Text(char *Output, unsigned int length); void UARTreadTEXT_1(); void UARTreadTEXT_2(); void MyWait(int iT); void MyWait_2(int iT); void RUN1(void); void RUN2(void); void RUN3(void); void RUN4(void); //int n; char UARTreadBuffer[256];//バッファ char gN=0;//受信文字数 char gChar;//コマンド void init(void) { ADCON1 = 0b00001111; TRISA = 0b00000000; TRISB = 0b00000000; TRISC = 0b00110000; //D-,D+ TRISD = 0b00000000; TRISE = 0b00000011; //SW1,2=INPUT LED=OUTPUT LATA = 0b00000000; LATB = 0b00000000; LATC = 0b00000000; LATD = 0b00000000; LATE = 0b00000000; } void main(void) { init();//初期設定 UART_Init(115200);//UART初期設定、ボーレイト設定 //割り込み使用時は以下を有効にします。 RCIF = 0; //reset RX pin flag RCIP = 1; //high priority RCIE = 1; //Enable RX interrupt PEIE = 1; //Enable pheripheral interrupt (serial port is a pheripheral) INTCONbits.GIE = 1;//Global Interrupt Enable bit int i; LED=0; int i,j; PORTBbits.RB1=1; for ( i = 0; i < 10; i++ ) { PORTDbits.RD0=1; PORTDbits.RD1=1; PORTDbits.RD2=1; PORTDbits.RD3=1; PORTDbits.RD4=1; PORTDbits.RD5=1; PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_us( 500 ); PORTDbits.RD0=0; PORTDbits.RD1=0; PORTDbits.RD2=0; PORTDbits.RD3=0; PORTDbits.RD4=0; PORTDbits.RD5=0; PORTDbits.RD6=0; PORTDbits.RD7=0; for ( j = 0; j < 2; j++ )__delay_ms( 10 ); } while(1) { LED=1; MyWait_2(200); UARTreadTEXT_2(); switch (gChar) { case '1': UART_Write_Text("BT1=RUN\r\n"); RUN1(); break; case '2': UART_Write_Text("BT2=RUN\r\n"); RUN2(); break; case '3': UART_Write_Text("BT3=RUN\r\n"); RUN3(); break; case '4': UART_Write_Text("BT4=RUN\r\n"); RUN4(); break; default: break; } }//end while } void interrupt SYS_InterruptHigh(void) { if(PIR1bits.RCIF == 1) { UARTreadBuffer[gN] = UART_Read(); gN++; //PIR1bits.RCIF=0; //LED=1; } } char UART_Init(const long int baudrate) { unsigned int x; BRGH = 1; //Setting High Baud Rate x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate if(x>255) //If High Baud Rage Required { BRGH = 0; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate } if(x<256) { SPBRG = x; //Writing SPBRG Register SYNC = 0; //Setting Asynchronous Mode, ie UART SPEN = 1; //Enables Serial Port TRISC7 = 1; //As Prescribed in Datasheet TRISC6 = 1; //As Prescribed in Datasheet CREN = 1; //Enables Continuous Reception TXEN = 1; //Enables Transmission return 1; //Returns 1 to indicate Successful Completion } return 0; //Returns 0 to indicate UART initialization failed } void UART_Write(char data) { while(!TRMT); TXREG = data; } char UART_TX_Empty() { return TRMT; } void UART_Write_Text(char *text) { int i; for(i=0;text[i]!='\0';i++) UART_Write(text[i]); } char UART_Data_Ready() { return RCIF; } char UART_Read() { while(!RCIF); return RCREG; } void UART_Read_Text(char *Output, unsigned int length) { unsigned int i; for(int i=0;i<length;i++) Output[i] = UART_Read(); } void MyWait(int iT) { int i,ii; for(ii=1;ii<iT;ii++) { __delay_ms(10); if(gN>=1) { for(i=1;i<gN;i++) { if(UARTreadBuffer[i-1]=='O' && UARTreadBuffer[i]=='K') { __delay_ms(5); return; } } } } } void MyWait_2(int iT) { int i,ii; for(ii=1;ii<iT;ii++) { __delay_ms(10); if(gN>=1) { for(i=1;i<gN;i++) { if(UARTreadBuffer[i-1]=='B' && UARTreadBuffer[i]=='T') { gChar=UARTreadBuffer[i+1]; __delay_ms(5); return; } } } } } void UARTreadTEXT_1() { int i; char Buffer[256]; if(gN>=1) { char iN=gN; gN=0; for(i=0;i<iN;i++) { Buffer[i]=UARTreadBuffer[i]; } } int test=0; } void UARTreadTEXT_2() { int i; char Buffer[256]; if(gN>=1) { char iN=gN; gN=0; for(i=0;i<iN;i++) { Buffer[i]=UARTreadBuffer[i]; } Buffer[iN]=0x00; UART_Write_Text(Buffer); } } void RUN1(void) { int i; for ( i = 0; i < 20; i++ ) { PORTDbits.RD3=1; PORTDbits.RD4=1; PORTDbits.RD5=1; __delay_ms( 1 ); PORTDbits.RD3=0; __delay_ms( 1 ); PORTDbits.RD4=0; PORTDbits.RD5=0; __delay_ms( 10 ); } for ( i = 0; i < 30; i++ ){__delay_ms( 10 );} for ( i = 0; i < 20; i++ ) { PORTDbits.RD0=1; PORTDbits.RD1=1; PORTDbits.RD2=1; __delay_ms( 1 ); PORTDbits.RD0=0; PORTDbits.RD2=0; __delay_ms( 1 ); PORTDbits.RD1=0; __delay_ms( 10 ); } for ( i = 0; i < 20; i++ ) { PORTDbits.RD3=1; PORTDbits.RD4=1; PORTDbits.RD5=1; __delay_ms( 1 ); PORTDbits.RD4=0; PORTDbits.RD5=0; __delay_ms( 1 ); PORTDbits.RD3=0; __delay_ms( 10 ); } for ( i = 0; i < 30; i++ ){__delay_ms( 10 );} for ( i = 0; i < 20; i++ ) { PORTDbits.RD0=1; PORTDbits.RD1=1; PORTDbits.RD2=1; __delay_ms( 1 ); PORTDbits.RD1=0; __delay_ms( 1 ); PORTDbits.RD0=0; PORTDbits.RD2=0; __delay_ms( 10 ); } } void RUN2(void) { int i; for ( i = 0; i < 30; i++ ) { PORTDbits.RD3=1; PORTDbits.RD4=1; PORTDbits.RD5=1; __delay_ms( 1 ); PORTDbits.RD3=0; __delay_ms( 1 ); PORTDbits.RD4=0; PORTDbits.RD5=0; __delay_ms( 10 ); } for ( i = 0; i < 30; i++ ){__delay_ms( 10 );} for ( i = 0; i < 30; i++ ) { PORTDbits.RD0=1; PORTDbits.RD1=1; PORTDbits.RD2=1; __delay_ms( 1 ); PORTDbits.RD1=0; __delay_ms( 1 ); PORTDbits.RD0=0; PORTDbits.RD2=0; __delay_ms( 10 ); } for ( i = 0; i < 30; i++ ) { PORTDbits.RD3=1; PORTDbits.RD4=1; PORTDbits.RD5=1; __delay_ms( 1 ); PORTDbits.RD4=0; PORTDbits.RD5=0; __delay_ms( 1 ); PORTDbits.RD3=0; __delay_ms( 10 ); } for ( i = 0; i < 30; i++ ){__delay_ms( 10 );} for ( i = 0; i < 30; i++ ) { PORTDbits.RD0=1; PORTDbits.RD1=1; PORTDbits.RD2=1; __delay_ms( 1 ); PORTDbits.RD0=0; PORTDbits.RD2=0; __delay_ms( 1 ); PORTDbits.RD1=0; __delay_ms( 10 ); } } void RUN3(void) { int i,j; for ( i = 0; i < 3; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); PORTDbits.RD6=0; __delay_ms( 1 ); PORTDbits.RD7=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 5; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_us( 500 ); PORTDbits.RD6=0; PORTDbits.RD7=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 3; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); PORTDbits.RD7=0; __delay_ms( 1 ); PORTDbits.RD6=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 5; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_us( 500 ); PORTDbits.RD6=0; PORTDbits.RD7=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } } void RUN4(void) { int i,j; for ( i = 0; i < 3; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_ms( 1 ); PORTDbits.RD7=0; PORTDbits.RD6=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 5; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_us( 500 ); PORTDbits.RD6=0; PORTDbits.RD7=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 3; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); PORTDbits.RD6=0; PORTDbits.RD7=0; __delay_ms( 1 ); for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } for ( i = 0; i < 5; i++ ) { PORTDbits.RD6=1; PORTDbits.RD7=1; __delay_ms( 1 ); __delay_us( 500 ); PORTDbits.RD6=0; PORTDbits.RD7=0; for ( j = 0; j < 5; j++ )__delay_ms( 10 ); } }
//ADDXL345_HMC5883 27章WiFi無線でprocessingの3Dサンプルを動かす #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_ADXL345_U.h> #include <stdio.h> //WiFiClient 非同期 #include <ESP8266WiFi.h> const char* ssid = "SSID";//無線LANのSSIDを設定します。 const char* password = "password";//無線LANのpasswordを設定します。 const char* host = "192.168.11.2";//パソコンのIPアドレスを設定します。 const int httpPort = 13000;//TCPサーバのポート static String gSendText=""; /* Assign a unique ID to this sensor at the same time */ Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); #include <Adafruit_HMC5883_U.h> Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345); void displaySensorDetails(void) { sensor_t sensor; accel.getSensor(&sensor); Serial.println("------------------------------------"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2"); Serial.println("------------------------------------"); Serial.println(""); delay(500); } void displayDataRate(void) { Serial.print ("Data Rate: "); switch(accel.getDataRate()) { case ADXL345_DATARATE_3200_HZ: Serial.print ("3200 "); break; case ADXL345_DATARATE_1600_HZ: Serial.print ("1600 "); break; case ADXL345_DATARATE_800_HZ: Serial.print ("800 "); break; case ADXL345_DATARATE_400_HZ: Serial.print ("400 "); break; case ADXL345_DATARATE_200_HZ: Serial.print ("200 "); break; case ADXL345_DATARATE_100_HZ: Serial.print ("100 "); break; case ADXL345_DATARATE_50_HZ: Serial.print ("50 "); break; case ADXL345_DATARATE_25_HZ: Serial.print ("25 "); break; case ADXL345_DATARATE_12_5_HZ: Serial.print ("12.5 "); break; case ADXL345_DATARATE_6_25HZ: Serial.print ("6.25 "); break; case ADXL345_DATARATE_3_13_HZ: Serial.print ("3.13 "); break; case ADXL345_DATARATE_1_56_HZ: Serial.print ("1.56 "); break; case ADXL345_DATARATE_0_78_HZ: Serial.print ("0.78 "); break; case ADXL345_DATARATE_0_39_HZ: Serial.print ("0.39 "); break; case ADXL345_DATARATE_0_20_HZ: Serial.print ("0.20 "); break; case ADXL345_DATARATE_0_10_HZ: Serial.print ("0.10 "); break; default: Serial.print ("???? "); break; } Serial.println(" Hz"); } void displayRange(void) { Serial.print ("Range: +/- "); switch(accel.getRange()) { case ADXL345_RANGE_16_G: Serial.print ("16 "); break; case ADXL345_RANGE_8_G: Serial.print ("8 "); break; case ADXL345_RANGE_4_G: Serial.print ("4 "); break; case ADXL345_RANGE_2_G: Serial.print ("2 "); break; default: Serial.print ("?? "); break; } Serial.println(" g"); } void setup(void) { Serial.begin(115200); Serial.println("Accelerometer Test"); Serial.println(""); /* Initialise the sensor */ if(!accel.begin()) { /* There was a problem detecting the ADXL345 ... check your connections */ Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); while(1); } /* Set the range to whatever is appropriate for your project */ accel.setRange(ADXL345_RANGE_16_G); //displaySetRange(ADXL345_RANGE_8_G); //displaySetRange(ADXL345_RANGE_4_G); //displaySetRange(ADXL345_RANGE_2_G); /* Display some basic information on this sensor */ displaySensorDetails(); /* Display additional settings (outside the scope of sensor_t) */ displayDataRate(); displayRange(); Serial.println(""); if(!mag.begin()) { //Serial.println("Ooops, no HMC5883 detected ... Check your wiring!"); while(1); } WiFi.begin(ssid, password);//無線LANに接続要求 while (WiFi.status() != WL_CONNECTED) {//接続完了まで待ちます。 delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void loop(void) { /* Get a new sensor event */ sensors_event_t event; accel.getEvent(&event); sensors_event_t event2; mag.getEvent(&event2); int Xd=(int)(1000*event.acceleration.x); int Yd=(int)(1000*event.acceleration.y); int Zd=(int)(1000*event.acceleration.z); int Mx=(int)(100*event2.magnetic.x); int My=(int)(100*event2.magnetic.y); int Mz=(int)(100*event2.magnetic.z); char str[256]; sprintf(str,"%d,%d,%d,%d,%d,%d\r\n",Xd,Yd,Zd,Mx,My,Mz); //Serial.print(str); /* Display the results (acceleration is measured in m/s^2) */ //Serial.print("X: "); Serial.print(Xd); Serial.print(","); //Serial.print("Y: "); Serial.print(Yd); Serial.print(","); //Serial.print("Z: "); Serial.print(Zd); Serial.print(","); //Serial.print("Mx: "); Serial.print(Mx); Serial.print(","); //Serial.print("My: "); Serial.print(My); Serial.print(","); //Serial.print("Mz: "); Serial.print(Mz); Serial.print(","); delay(200); // Use WiFiClient class to create TCP connections WiFiClient client; if (client.connect(host, httpPort)) { client.print(str); } }
//TCP_Server_ADXL345_HMC5883_2 27章WiFi無線でprocessingの3Dサンプルを動かす import processing.net.*; int port = 13000; boolean myServerRunning = true; Server myServer; import processing.serial.*; Serial myPort; // The serial port float Mx=0; float My=0; float Mz=0; int gC=0;int gF=0;int gN=20; int[] Ax=new int[gN+1]; int[] Ay=new int[gN+1]; int[] Az=new int[gN+1]; float Dx=0,Dy=0,Dz=0; int gC0=0,gCmax=4; float Dxnew=0,Dynew=0,Dznew=0,Dxold,Dyold,Dzold; float Mxnew=0,Mynew=0,Mznew=0,Mxold,Myold,Mzold; int gM=0; void setup () { size(740, 660, P3D); myServer = new Server(this, port); // Starts a myServer on port 13000 } void draw () { int i; int Sx=0,Sy=0,Sz=0; background(0, 256, 256); Client thisClient = myServer.available(); if (thisClient != null) { if (thisClient.available() > 0) { String myString =thisClient.readString(); myString = trim(myString); int sensors[] = int(split(myString, ',')); int sensorNum; for (sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print(sensors[sensorNum] + "\t"); } println(); int f; if(sensorNum==6){f=1;} else { f=0; thisClient.write("BT"+gM+"\r\n"); } if(f==1) { Mxold=Mxnew;Mxnew=atan2(sensors[0],sensors[2]); Myold=Mynew;Mynew=atan2(sensors[1],sensors[2]); Mzold=Mznew;Mznew=atan2(sensors[4],sensors[3]); if(abs(Mxnew)>abs(Mynew)) { if(abs(Mxnew)<0.2) {gM=0;} else { if(abs(Mxnew)<0.4) {gM=3;} else{gM=1;} } } else { if(abs(Mynew)<0.2) {gM=0;} else { if(abs(Mynew)<0.4) {gM=4;} else{gM=2;} } } Ax[gC]=sensors[0]; Ay[gC]=sensors[1]; Az[gC]=sensors[2]; if(gF==1) { for(i=0;i<gN;i++) { Sx=Sx+Ax[i]; Sy=Sy+Ay[i]; Sz=Sz+Az[i]; } Dxold=Dxnew;Dxnew=0.1*(Ax[gC]-Sx/gN); Dyold=Dynew;Dynew=0.1*(Ay[gC]-Sy/gN); Dzold=Dznew;Dznew=0.1*(Az[gC]-Sz/gN); } gC++; if(gC > gN) {gF=1;gC=0;} gCmax=gC0; gC0=0; } } } if(gF==1) { Mx=Mxold+(Mxnew-Mxold)*gC0/gCmax; My=Myold+(Mynew-Myold)*gC0/gCmax; Mz=Mzold+(Mznew-Mzold)*gC0/gCmax; Dx=Dxold+(Dxnew-Dxold)*gC0/gCmax; Dy=Dyold+(Dynew-Dyold)*gC0/gCmax; Dz=Dzold+(Dznew-Dzold)*gC0/gCmax; println(Mx+" " +My +" " +Mz+" "+Dx+" " +Dy +" " +Dz); } gC0++; lights(); translate(width / 2, height / 2,-200); rotateX(Mx); rotateY(Mz); rotateZ(My); noStroke(); fill(240, 240, 230); translate(0,0,100); translate(Dx,Dz,Dy); OB_A2();//Z軸回転体(胴体) OB_A3();//Z軸回転体(胴体先端) OB2();//主翼 OB3();//水平尾翼 OB4();//垂直尾翼 OB_A4();//Z軸回転体(エンジン) } void OB_A4()//Z軸回転体(エンジン) { int sides=16;//分割数 int Sn=6;//面数 float Pz[]={0,-1,-50,-60,-70,-70};//z座標 float R[]={15,20,20,15,15,1};//回転物半径 int i,j; float angleIncrement = TWO_PI/sides; pushMatrix(); translate(60, 36.3,-145); for(j=0;j<Sn-1;j++) { float angle = 0; beginShape(QUAD_STRIP); for (i = 0; i < sides + 1; ++i) { vertex(R[j]*cos(angle), R[j]*sin(angle), Pz[j]); vertex(R[j+1]*cos(angle), R[j+1]*sin(angle), Pz[j+1]); angle += angleIncrement; } endShape(CLOSE); } popMatrix(); pushMatrix(); translate(-60, 36.3,-145); for(j=0;j<Sn-1;j++) { float angle = 0; beginShape(QUAD_STRIP); for (i = 0; i < sides + 1; ++i) { vertex(R[j]*cos(angle), R[j]*sin(angle), Pz[j]); vertex(R[j+1]*cos(angle), R[j+1]*sin(angle), Pz[j+1]); angle += angleIncrement; } endShape(CLOSE); } popMatrix(); } void OB4()//垂直尾翼 { float Px[]={0,0,3,-3,0,0,2,-2};//ポイントx float Py[]={0,0,0,0,-80,-80,-80,-80};//ポイントy float Pz[]={25,-25,0,0,0,-25,-15,-15};//ポイントz int Sn=10;//面数 int S1[]={0,0,1,1,0,0,1,1,4,5};//面ポイント1 int S2[]={2,4,2,5,3,4,3,5,6,6};//面ポイント2 int S3[]={6,6,5,6,7,7,7,7,7,7};//面ポイント3 int i; pushMatrix(); translate(0, 0,-395); for(i=0;i<Sn;i++) { beginShape(); vertex(Px[S1[i]], Py[S1[i]],Pz[S1[i]]); vertex(Px[S2[i]], Py[S2[i]],Pz[S2[i]]); vertex(Px[S3[i]], Py[S3[i]],Pz[S3[i]]); endShape(CLOSE); } popMatrix(); } void OB3()//水平尾翼 { float Px[]={0,0,0,100,100,100};//ポイントx float Py[]={3,3,-3,2,2,-2};//ポイントy float Pz[]={15,-15,0,-5,-15,-10};//ポイントz int Sn=7;//面数 int S1[]={0,0,0,0,1,1,3};//面ポイント1 int S2[]={1,3,2,3,2,4,4};//面ポイント2 int S3[]={4,4,5,5,5,5,5};//面ポイント3 int i; pushMatrix(); translate(0, 0,-415); for(i=0;i<Sn;i++) { beginShape(); vertex(Px[S1[i]], Py[S1[i]],Pz[S1[i]]); vertex(Px[S2[i]], Py[S2[i]],Pz[S2[i]]); vertex(Px[S3[i]], Py[S3[i]],Pz[S3[i]]); endShape(CLOSE); } for(i=0;i<Sn;i++) { beginShape(); vertex(-Px[S1[i]], Py[S1[i]],Pz[S1[i]]); vertex(-Px[S2[i]], Py[S2[i]],Pz[S2[i]]); vertex(-Px[S3[i]], Py[S3[i]],Pz[S3[i]]); endShape(CLOSE); } popMatrix(); } void OB2()//主翼 { float Px[]={10,10,10,220,220,220};//ポイントx float Py[]={20,20,10,15,15,10};//ポイントy float Pz[]={40,-40,0,10,-10,0};//ポイントz int Sn=7;//面数 int S1[]={0,0,0,0,1,1,3};//面ポイント1 int S2[]={1,3,2,3,2,4,4};//面ポイント2 int S3[]={4,4,5,5,5,5,5};//面ポイント3 int i; pushMatrix(); translate(0, 0,-200); for(i=0;i<Sn;i++) { beginShape(); vertex(Px[S1[i]], Py[S1[i]],Pz[S1[i]]); vertex(Px[S2[i]], Py[S2[i]],Pz[S2[i]]); vertex(Px[S3[i]], Py[S3[i]],Pz[S3[i]]); endShape(CLOSE); } for(i=0;i<Sn;i++) { beginShape(); vertex(-Px[S1[i]], Py[S1[i]],Pz[S1[i]]); vertex(-Px[S2[i]], Py[S2[i]],Pz[S2[i]]); vertex(-Px[S3[i]], Py[S3[i]],Pz[S3[i]]); endShape(CLOSE); } popMatrix(); } void OB_A3()//Z軸回転体(胴体先端) { int sides=16;//分割数 int Sn=8;//面数 float Pz[]={0,-3.2,-11.7,-21.8,-31.9,-45.3,-70.2,-129.4};//z座標 float R[]={0.1,3.7,7.4,9.6,11.8,13.8,17.4,17.7};//回転物半径 int i,j; float angleIncrement = TWO_PI/sides; pushMatrix(); translate(0, 8.5); for(j=0;j<Sn-1;j++) { float angle = 0; beginShape(QUAD_STRIP); for (i = 0; i < sides + 1; ++i) { vertex(R[j]*cos(angle), R[j]*sin(angle), Pz[j]); vertex(R[j+1]*cos(angle), R[j+1]*sin(angle), Pz[j+1]); angle += angleIncrement; } endShape(CLOSE); } popMatrix(); } void OB_A2()//Z軸回転体(胴体) { int sides=16;//分割数 int Sn=13;//面数 float Pz[]={-40,-46.9,-68.4,-88.2,-108.8,-129.4,-319.4,-346.2,-373,-391.2,-409.4,-433,-440};//z座標 float R[]={0.1,7.8,15.4,19.5,22.3,25,25,22.3,18.7,16,13.3,8.3,0.1};//回転物半径 int i,j; float angleIncrement = TWO_PI/sides; for(j=0;j<Sn-1;j++) { float angle = 0; beginShape(QUAD_STRIP); for (i = 0; i < sides + 1; ++i) { fill(240, 240, 230); if(j==1){fill(20, 20, 40);} if(j==5 && i==0){fill(20, 20, 40);} if(j==5 && i==8){fill(20, 20, 40);} if(j==5 && i==16){fill(20, 20, 40);} vertex(R[j]*cos(angle), R[j]*sin(angle), Pz[j]); vertex(R[j+1]*cos(angle), R[j+1]*sin(angle), Pz[j+1]); angle += angleIncrement; } endShape(CLOSE); } }