Created
January 19, 2022 14:40
-
-
Save dj1711572002/f472a0f45c404c904bb97526359b7b41 to your computer and use it in GitHub Desktop.
Arduino Tennsyduino GNSS Ublox F9P-F9H MovingBase Logger for Teensy4.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple test of USB Host Mouse/Keyboard | |
// | |
// This example is in the public domain | |
//-----SD log-------------------------- | |
//#include <SD.h> | |
#include "SdFat.h" | |
#include <SPI.h> | |
// Setup for built in SD card. | |
#define SPI_CLOCK SD_SCK_MHZ(50) | |
#define SD_CONFIG SdioConfig(FIFO_SDIO) | |
// SdFat-beta usage | |
SdFs sd; | |
FsFile myFile; | |
int n;//counter | |
int i,j,k; | |
//const int chipSelect =BUILTIN_SDCARD;// 10; | |
//String dataString,dataString1; | |
int kaisu; | |
//-----USB HOST | |
#include "USBHost_t36.h" | |
#define USBBAUD 115200 | |
uint32_t baud = USBBAUD; | |
uint32_t format = USBHOST_SERIAL_8N1; | |
USBHost myusb; | |
USBHub hub1(myusb); | |
USBHub hub2(myusb); | |
USBHIDParser hid1(myusb); | |
USBHIDParser hid2(myusb); | |
USBHIDParser hid3(myusb); | |
USBSerial_BigBuffer userial(myusb); | |
USBSerial_BigBuffer userialb(myusb); | |
USBSerial_BigBuffer userialb2(myusb); | |
uint8_t buf1[200],buf2[200],buf3[200]; | |
uint8_t dummy; | |
char data0,datab,datab2; | |
USBDriver *drivers[] = {&hub1, &hub2, &hid1, &hid2, &hid3, &userial, &userialb, &userialb2}; | |
#define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0])) | |
const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "HID1", "HID2", "HID3", "USERIAL1", "USERIALB", "USERIALB2" }; | |
bool driver_active[CNT_DEVICES] = {false, false, false, false}; | |
//Data | |
uint8_t Buf172[172]; | |
uint8_t PVTdata[100]; | |
long PVTval[33]; | |
uint8_t RELPOSdata[72]; | |
long RELPOSval[21]; | |
uint8_t fdata[4]; | |
int Num1; | |
char keyin; | |
int Startflag=0; | |
//timestamp | |
char fnameB[30];//UBX Binary File name | |
char fnameV[30];//UBX Value File name NAV-PVT+NAV-RELPOSNED | |
int Sflag=0; | |
//================================================================= | |
void setup() | |
{ | |
Serial.begin(115200); | |
while (!Serial && (millis() < 5000)) ; // wait for Arduino Serial Monitor | |
Serial.println("\n\nUSB Host Testing - Serial"); | |
myusb.begin(); | |
Serial1.begin(115200); // We will echo stuff Through Serial1... | |
//-----------SD CARD Initialize------------------ | |
if (!sd.begin(SD_CONFIG)) { | |
Serial.println("initialization failed!"); | |
return; | |
} | |
Serial.println("initialization done."); | |
Sflag=0; | |
}//set up | |
//======================================================================================= | |
//==========LOOP ======================================================================== | |
//======================================================================================= | |
void loop(){ | |
digitalWrite(13, !digitalRead(13)); | |
myusb.Task(); | |
updateDeviceList(); | |
//*********************************************************** | |
//****************DATA ACQUSITION From USB HOST************** | |
//*********************************************************** | |
//---USB HOST Ports read-------------------------- | |
//2ポートのデータをBuf172[]にまとめて収納1周期トータル172byteなので順不同で配列に格納して後で、ばらす。 | |
while(userial.available()){ | |
Buf172[Num1]=userial.read(); | |
Num1++; | |
}//---USERIAL read finished--------------------- | |
//---USERIALB Port read------------------- | |
while(userialb.available()){ | |
Buf172[Num1]=userialb.read(); | |
Num1++; | |
}//useralb read finished | |
//:::::::::: Logging & Monitoring data process 172bytes:::::::::::::::::::::: | |
//1周期分のデータ取得終了であと90msecあるのでその間に各種処理を行う | |
if(Num1>=172){ //a MB data read finished | |
Num1=0; | |
//----PVT RELPOS Deviding--- 172byteのデータの先頭がPVTかRELPSOか判別して、PVTdata[]RELPOSdata[]に仕分ける----------------------------------------- | |
if(Buf172[0] == 0xB5 && Buf172[1] == 0x62 && Buf172[2] == 0x01 && Buf172[3] == 0x07){//1st PVT Header | |
for (i=0;i<100;i++){ | |
PVTdata[i]=Buf172[i]; | |
} | |
for (i=100;i<172;i++){ | |
RELPOSdata[i-100]=Buf172[i]; | |
} | |
}//if end | |
else if(Buf172[0] == 0xB5 && Buf172[1] == 0x62 && Buf172[2] == 0x01 && Buf172[3] == 0x3C){//1st RELPOS Header | |
for(i=0;i<72;i++){ | |
RELPOSdata[i]=Buf172[i]; | |
} | |
for(i=72;i<172;i++){ | |
PVTdata[i-72]=Buf172[i]; | |
} | |
}//else end | |
//PVTdataとRELPOSdataをUBX実データに変換してPVTval[33]とRELPOSval[21]に格納 | |
//==============PVTcnv,RELPOScnv バイナリを実数データ変換====== | |
int result=PVTcnv(PVTdata,PVTval); | |
result=RELPOScnv(RELPOSdata,RELPOSval); | |
//========================================================= | |
//*********ファイル名をタイムスタンプで作ってファイルOPEN**************************************************** | |
if (PVTval[11]==131 && Sflag==0){//start | |
Sflag=1; | |
//---PVTval[1]=Year/[2]=Month/[3]=Day/[4]=Hour/[5]=Min/[6]=sec--- | |
int JST=(PVTval[4]+9)%24;//UTC hourをJSTに変換 | |
String stime=String(PVTval[2],DEC)+"-"+String(PVTval[3],DEC)+"-"+String(JST,DEC)+"-"+String(PVTval[5],DEC);//MMDDHHMM | |
String stimeB="/"+stime+".ubx";//UBX Binary File | |
String stimeV="/"+stime+".txt";//UBX Value Text File | |
int slenB=stimeB.length()+1; | |
int slenV=stimeV.length()+1; | |
//ファイル名はchar配列なのでStringからchar配列変換 fname[]を得る | |
stimeB.toCharArray(fnameB,slenB);//stimeB to fnameB[] chara Array | |
stimeV.toCharArray(fnameV,slenV);//stimeV to fnameV[] chara Array | |
Serial.println(); | |
Serial.print("fnameB="); | |
Serial.print(stimeB); | |
Serial.print("/fnameV="); | |
Serial.println(stimeV); | |
Serial.print("TimeStamp:Sflag="); | |
Serial.println(Sflag); | |
//myFile = sd.open(fnameB, FILE_WRITE); | |
//myFile.truncate(0);//Appned | |
Sflag=1; | |
}//timeStamp making end | |
//*****************************SD Write********************************** | |
if (PVTval[11]==131 ){//start | |
n=0; | |
int stime; | |
stime=micros(); | |
myFile = sd.open(fnameB, FILE_WRITE); | |
while(n<172){//PVT write to SD card | |
if(myFile){//myFileチェックは、書き込み直前で行う。 | |
if(n<100){ | |
myFile.write(PVTdata[n]); | |
} | |
if(n>=100 && n<172){ | |
myFile.write(RELPOSdata[n-100]); | |
} | |
}else{ | |
Serial.print("myFile Error"); | |
} | |
n++; | |
}//while write finished | |
delay(10);//OPEN-CLOSE時間をあける | |
myFile.close(); //毎回クローズして万一のアクシデントに備える | |
//if(Serial.available() && Serial.read() >= 0) { | |
// Sflag=0; | |
// } | |
/* | |
if(kaisu>1000){//デバッグ用に千回でとめる | |
Serial.print("SD closed time="); | |
Serial.print(millis()); | |
exit(0);//loop exit | |
} | |
//Serial.println("SD closed write Finished"); | |
//kaisu++; | |
//Serial.print(kaisu); | |
//Serial.print(":SD write time="); | |
//Serial.print(micros()-stime); | |
*/ | |
}//SD Write end | |
//========Ucenter OUTPUT======================= | |
for(i=0;i<172;i++){ | |
if(i<100){ | |
Serial.write(PVTdata[i]); | |
} | |
if(i>=100 && i<172){ | |
Serial.write(RELPOSdata[i-100]); | |
} | |
} | |
/* | |
//======Serialprint Monitor PVTval RELPOSval========== | |
Serial.println(); | |
Serial.print("PVTval:"); | |
for(i=0;i<33;i++){ | |
Serial.print(PVTval[i]); | |
Serial.print(","); | |
} | |
Serial.println(); | |
Serial.println(); | |
Serial.print("RELPOSval:"); | |
for(i=0;i<21;i++){ | |
Serial.print(RELPOSval[i]); | |
Serial.print(","); | |
} | |
Serial.println(); | |
*/ | |
}// if 172 end | |
}//loop finished | |
//====================================================================================== | |
//==================LOOP END============================================================ | |
//====================================================================================== | |
void updateDeviceList() { | |
// Print out information about different devices. | |
for (uint8_t i = 0; i < CNT_DEVICES; i++) { | |
if (*drivers[i] != driver_active[i]) { | |
if (driver_active[i]) { | |
Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]); | |
driver_active[i] = false; | |
} else { | |
Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct()); | |
driver_active[i] = true; | |
const uint8_t *psz = drivers[i]->manufacturer(); | |
if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz); | |
psz = drivers[i]->product(); | |
if (psz && *psz) Serial.printf(" product: %s\n", psz); | |
psz = drivers[i]->serialNumber(); | |
if (psz && *psz) Serial.printf(" Serial: %s\n", psz); | |
// If this is a new Serial device. | |
if (drivers[i] == &userial) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userial.begin(baud); | |
} | |
if (drivers[i] == &userialb) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userialb.begin(baud); | |
} | |
if (drivers[i] == &userialb2) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userialb2.begin(baud); | |
} | |
} | |
} | |
} | |
} | |
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
int PVTcnv(uint8_t d[100],long pvt[33]){ | |
//PVT header[0-6] | |
//0:itow[6-9] | |
pvt[0]=B2L(d[9],d[8],d[7],d[6]); | |
//1:year[10-12] | |
pvt[1]=d[10]+d[11]*256; | |
//2:month[12] | |
pvt[2]=d[12]; | |
//3:day[13] | |
pvt[3]=d[13]; | |
//4:hour[14] | |
pvt[4]=d[14]; | |
//5:min[15] | |
pvt[5]=d[15]; | |
//6:sec[16] | |
pvt[6]=d[16]; | |
//7:valid[17] | |
pvt[7]=d[17]; | |
//8:tAcc[18-21] | |
pvt[8]=B2L(d[21],d[20],d[19],d[18]); | |
//9:nano[22-25] | |
pvt[9]=B2L(d[25],d[24],d[23],d[22]); | |
//10:fixType[26] | |
pvt[10]=d[26]; | |
//11:flags[27] This is Fix status 131 | |
pvt[11]=d[27]; | |
//12:flags2[28] | |
pvt[12]=d[28]; | |
//13:numSV[29] | |
pvt[13]=d[29]; | |
//14:lon[30-33] | |
pvt[14]=B2L(d[33],d[32],d[31],d[30]); | |
//15:lat[34-37] | |
pvt[15]=B2L(d[37],d[36],d[35],d[34]); | |
//16:height[38-41] | |
pvt[16]=B2L(d[41],d[40],d[39],d[38]); | |
//17:hMSL[42-45] | |
pvt[17]=B2L(d[45],d[44],d[43],d[42]); | |
//18:hAcc[46-49] | |
pvt[18]=B2L(d[49],d[48],d[47],d[46]); | |
//19:vAcc[50-53] | |
pvt[19]=B2L(d[53],d[52],d[51],d[50]); | |
//20:velN[54-57] | |
pvt[20]=B2L(d[57],d[56],d[55],d[54]); | |
//21:velE[58-61] | |
pvt[21]=B2L(d[61],d[60],d[59],d[58]); | |
//22:velD[62-65] | |
pvt[22]=B2L(d[65],d[64],d[63],d[62]); | |
//23:gSpeed[66-69] | |
pvt[23]=B2L(d[69],d[68],d[67],d[66]); | |
//24:headMot[70-73] | |
pvt[24]=B2L(d[73],d[72],d[71],d[70]); | |
//25:sAcc[74-77] | |
pvt[25]=B2L(d[77],d[76],d[75],d[74]); | |
//26:headAcc[78-81] | |
pvt[26]=B2L(d[81],d[80],d[79],d[78]); | |
//27:pDOP[82-83] | |
pvt[27]=d[82]+d[83]*256; | |
//28:flags3[84] | |
pvt[28]=d[84]; | |
//29:reserved1[85] | |
pvt[29]=d[85]; | |
//30:headVeh[86-89] | |
pvt[30]=B2L(d[89],d[88],d[87],d[86]); | |
//31:magDec[90-91] | |
pvt[31]=d[90]+d[91]*256; | |
//32:magAcc[92-93] | |
pvt[32]=d[92]+d[93]*256; | |
}//PVTcnv() end | |
//--RELPOScnv--------------------------- | |
int RELPOScnv(uint8_t d[72],long relpos[20]){ | |
//RELPOS header[0-5] | |
int s=0; | |
//0:ver[6] | |
relpos[0]=d[s+6]; | |
//1:reserved1[7] | |
relpos[1]=d[s+7]; | |
//2:refStationId[8] | |
relpos[2]=d[s+8]; | |
//3:itow[10-13] | |
relpos[3]=B2L(d[s+13],d[s+12],d[s+11],d[s+10]); | |
//4:relposN[14-17] | |
relpos[4]=B2L(d[s+17],d[s+16],d[s+15],d[s+14]); | |
//5:relposE[18-21] | |
relpos[5]=B2L(d[s+21],d[s+20],d[s+19],d[s+18]); | |
//6:relposD[22-25] | |
relpos[6]=B2L(d[s+25],d[s+24],d[s+23],d[s+22]); | |
//7:relposLength[26-29] | |
relpos[7]=B2L(d[s+29],d[s+28],d[s+27],d[s+26]); | |
//8:relposHeading[30-33] | |
relpos[8]=B2L(d[s+33],d[s+32],d[s+31],d[s+30]); | |
//9:reserved2[34] | |
relpos[9]=B2L(d[s+34],d[s+33],d[s+32],d[s+31]); | |
//10:relposHPN[35] | |
relpos[10]=127-int(d[s+35]); | |
//11:relposHPE[36] | |
relpos[11]=127-int(d[s+36]); | |
//12:relposHPD[37] | |
relpos[12]=127-int(d[s+37]); | |
//13:relposHPLength[38] | |
relpos[13]=127-int(d[s+38]); | |
//14:accN[38-41] | |
relpos[14]=B2L(d[s+41],d[s+40],d[s+39],d[s+38]); | |
//15:accE[42-45] | |
relpos[15]=B2L(d[s+45],d[s+44],d[s+43],d[s+42]); | |
//16:accD[46-49] | |
relpos[16]=B2L(d[s+49],d[s+48],d[s+47],d[s+46]); | |
//17:accLength[50-53] | |
relpos[17]=B2L(d[s+53],d[s+52],d[s+51],d[s+50]); | |
//18:accHeading[54-57] | |
relpos[18]=B2L(d[s+57],d[s+56],d[s+55],d[s+54]); | |
//19:reserved[57-60] | |
relpos[19]=B2L(d[s+60],d[s+59],d[s+58],d[s+57]); | |
//20:flags[60-63] | |
relpos[20]=B2L(d[s+63],d[s+62],d[s+61],d[s+60]); | |
} | |
//+++++++++++++4byte Binary to Long ++++++++++++++++++++++++++++++++++++++++++++++ | |
long B2L(uint8_t b4 , uint8_t b3 ,uint8_t b2 , uint8_t b1 ) { | |
//pc.printf("B2L IN=%s,%x,%x,%x,%x,b4&0x80=%d\n\r",sen,b4,b3,b2,b1,b4 &0x80); | |
//pc.printf("B2L IN=b4&0x80=%d\n\r",b4 & 0x80); | |
long su; | |
if ((b4 & 0x80) && 0x80){//最上位ビットたっていればマイナス | |
su = -(256-(int)b1)+(255-(int)b2)*256+(255-(int)b3)*65536+(255-(int)b4)*256*256*256; | |
//pc.printf("B2L-:sen=%s,%d,%d,%d,%d,%d\n\r",sen,b4,b3,b2,b1,su); | |
} | |
else { | |
su=(int)b1+(int)b2*256+(int)b3* 65536+(int)b4*256*256*256; | |
//pc.printf("B2L+:sen=%s,%d,%d,%d,%d,%d,%d\n\r",sen,b4,b3,b2,b1,su); | |
} | |
return su; | |
} | |
//================================================================================= | |
//+++++++++++++++i_to_char++++++++++++++++++++++++++++++++++++ | |
// i=IntegerValueData,*d=Array pointer, n=Array start No | |
void i_to_char(int i, uint8_t *d, int n) | |
{ | |
d[n] = i & 0x000000ff; | |
d[n + 1] = (i & 0x0000ff00) >> 8; | |
d[n + 2] = (i & 0x00ff0000) >> 16; | |
d[n + 3] = (i & 0xff000000) >> 24; | |
} | |
//++++++++++++++++String to CharArray Trans++++++++++++++++++++ | |
void str2char(char c[],String dataS) | |
{ | |
//String dataS; | |
//dataS="HELLO dataS"; | |
int dataS_len=dataS.length()+1; | |
char char_array[dataS_len]; | |
dataS.toCharArray(c,dataS_len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment