diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.h b/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.h new file mode 100644 index 0000000..385f8b5 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.h @@ -0,0 +1,26 @@ +#ifndef GPS_H +#define GPS_H + +#include +#include "./PIN_ASSIGN.h" + +/* +http://akizukidenshi.com/catalog/g/gK-09991/ +*/ + +// #################### GPS #################### + +void GPS_Init(); +void GPS_Update(); +void GPS_Print(); + +typedef struct { + float lat; + float lng; + float height; + uint16_t last_received_time; +} Gps_t; + + +#endif + diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.ino b/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.ino new file mode 100644 index 0000000..7b3ece3 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/GPS.ino @@ -0,0 +1,160 @@ +#include "./GPS.h" + +Gps_t gps; + +SoftwareSerial GpsSerial(PIN_GPS_TX, PIN_GPS_RX); + +// 非公開関数 +// String GPS_NMEA2DMS_(float val); +// String GPS_NMEA2DM_(float val); +// String GPS_NMEA2DD_(float val); +float GPS_NMEA2DDf_(float val); +// String GPS_UTC2GMT900_(String str); + + + +void GPS_Init() { + GpsSerial.begin(9600); + gps.lat = 0.0; + gps.lng = 0.0; + gps.height = 0.0; + gps.last_received_time = 0; + Serial.println(F("GPS init done")); +} + + +void GPS_Update() { + // 1つのセンテンスを読み込む + String line = GpsSerial.readStringUntil('\n'); + + if(line != ""){ + uint16_t i; + uint8_t index = 0; + uint16_t len = line.length(); + String str = ""; + + // StringListの生成(簡易) + // const uint8_t MAX_LIST = 30; // メモリ節約のために,極限まで小さくしたい! + const uint8_t MAX_LIST = 12; // メモリ節約のために,極限まで小さくしたい! [9]までしか使わないから小さくしても良いはず. + String list[MAX_LIST]; + for (i = 0; i < MAX_LIST; i++) { + list[i] = ""; + } + + // 「,」を区切り文字として文字列を配列にする + for (i = 0; i < len; i++) { + if (index >= MAX_LIST) { + break; + } + if (line[i] == ',') { + list[index++] = str; + str = ""; + continue; + } + str += line[i]; + } + + // $GPGGAセンテンスのみ読み込む + if (list[0] == "$GPGGA") { + + // ステータス + if(list[6] != "0") { + // // 現在時刻 + // Serial.print(GPS_UTC2GMT900_(list[1])); + + // 緯度 + // Serial.print(F(" 緯度:")); + // // Serial.print(GPS_NMEA2DMS_(list[2].toFloat())); + // Serial.print(F("(")); + // Serial.print(GPS_NMEA2DD_(list[2].toFloat())); + // Serial.print(F(")")); + + gps.lat = GPS_NMEA2DDf_(list[2].toFloat()); + + // 経度 + // Serial.print(F(" 経度:")); + // // Serial.print(GPS_NMEA2DMS_(list[4].toFloat())); + // Serial.print(F("(")); + // Serial.print(GPS_NMEA2DD_(list[4].toFloat())); + // Serial.print(F(")")); + + gps.lng = GPS_NMEA2DDf_(list[4].toFloat()); + + // 海抜 + // Serial.print(F(" 海抜:")); + // Serial.print(list[9]); + // // list[10].toLowerCase(); + // // Serial.print(list[10]); + // Serial.print(F(" m")); + + gps.height = list[9].toFloat(); + + gps.last_received_time = millis() / 1000; + // Serial.print(F("測位成功")); + Serial.print(F("GPS OK")); + } else { + // Serial.print(F("測位失敗")); + Serial.print(F("GPS NG")); + } + + Serial.println(F("")); + } + } +} + +void GPS_Print() { + Serial.print(F("GPS: Lat=")); + Serial.print(gps.lat,6); + Serial.print(F(", Lng=")); + Serial.print(gps.lng,6); + Serial.print(F(", Height=")); + Serial.print(gps.height,2); + Serial.print(F(", RecTime=")); + Serial.print(gps.last_received_time); + Serial.println(F("")); +} + + + + + +// // NMEAの緯度経度を「度分秒」(DMS)の文字列に変換する +// String GPS_NMEA2DMS_(float val) { +// int d = val / 100; +// int m = ((val / 100.0) - d) * 100.0; +// float s = ((((val / 100.0) - d) * 100.0) - m) * 60; +// return String(d) + "度" + String(m) + "分" + String(s, 1) + "秒"; +// } + +// // (未使用)NMEAの緯度経度を「度分」(DM)の文字列に変換する +// String GPS_NMEA2DM_(float val) { +// int d = val / 100; +// float m = ((val / 100.0) - d) * 100.0; +// return String(d) + "度" + String(m, 4) + "分"; +// } + +// // NMEAの緯度経度を「度」(DD)の文字列に変換する +// String GPS_NMEA2DD_(float val) { +// int d = val / 100; +// int m = (((val / 100.0) - d) * 100.0) / 60; +// float s = (((((val / 100.0) - d) * 100.0) - m) * 60) / (60 * 60); +// return String(d + m + s, 6); +// } + +// NMEAの緯度経度を「度」(DD)のfloatに変換する +// dddmm.mmmmmm +float GPS_NMEA2DDf_(float val) { + int d = (int)(val / 100); + float m = (((val / 100.0) - d) * 100.0) / 60; + // float s = (((((val / 100.0) - d) * 100.0) - m) * 60) / (60 * 60); + // return d + m + s; + return d + m; +} + +// // UTC時刻から日本の標準時刻に変換する(GMT+9:00) +// String GPS_UTC2GMT900_(String str) { +// int hh = (str.substring(0,2).toInt()) + 9; +// if(hh > 24) hh = hh - 24; +// return String(hh,DEC) + ":" + str.substring(2,4) + ":" + str.substring(4,6); +// } + diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.h b/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.h new file mode 100644 index 0000000..85f26a6 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.h @@ -0,0 +1,67 @@ +#ifndef IMU_H +#define IMU_H + + +/* + +http://akizukidenshi.com/catalog/g/gK-13010/ + +//================================================================// +// AE-BMX055 Arduino UNO // +// VCC +5V // +// GND GND // +// SDA A4(SDA) // +// SCL A5(SCL) // +// // +// (JP6,JP4,JP5はショートした状態) // +// http://akizukidenshi.com/catalog/g/gK-13010/ // +//================================================================// +*/ + +// #################### 9AXIS #################### +#include "./PIN_ASSIGN.h" + + +#include +// BMX055 加速度センサのI2Cアドレス +#define IMU_ADDR_ACCL 0x19 // (JP1,JP2,JP3 = Openの時) +// BMX055 ジャイロセンサのI2Cアドレス +#define IMU_ADDR_GYRO 0x69 // (JP1,JP2,JP3 = Openの時) +// BMX055 磁気センサのI2Cアドレス +#define IMU_ADDR_MAG 0x13 // (JP1,JP2,JP3 = Openの時) + + +void IMU_Init(); +void IMU_UpdateAll(); +void IMU_UpdateAcc(); +void IMU_UpdateGyr(); +void IMU_UpdateMag(); +void IMU_PrintAcc(); +void IMU_PrintGyr(); +void IMU_PrintMag(); + +float IMU_GetAccX(); +float IMU_GetAccY(); +float IMU_GetAccZ(); +float IMU_GetGyrX(); +float IMU_GetGyrY(); +float IMU_GetGyrZ(); +int IMU_GetMagX(); +int IMU_GetMagY(); +int IMU_GetMagZ(); + + +typedef struct { + float xAccl; + float yAccl; + float zAccl; + float xGyro; + float yGyro; + float zGyro; + int xMag ; + int yMag ; + int zMag ; +} Imu_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.ino b/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.ino new file mode 100644 index 0000000..90dc9d8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/IMU.ino @@ -0,0 +1,236 @@ +#include "./IMU.h" + +Imu_t imu; + + + +void IMU_Init() { + imu.xAccl = 0.0; + imu.yAccl = 0.0; + imu.zAccl = 0.0; + imu.xGyro = 0.0; + imu.yGyro = 0.0; + imu.zGyro = 0.0; + imu.xMag = 0; + imu.yMag = 0; + imu.zMag = 0; + + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x0F); // Select PMU_Range register + Wire.write(0x03); // Range = +/- 2g + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x10); // Select PMU_BW register + Wire.write(0x08); // Bandwidth = 7.81 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x11); // Select PMU_LPW register + Wire.write(0x00); // Normal mode, Sleep duration = 0.5ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x0F); // Select Range register + Wire.write(0x04); // Full scale = +/- 125 degree/s + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x10); // Select Bandwidth register + Wire.write(0x07); // ODR = 100 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x11); // Select LPM1 register + Wire.write(0x00); // Normal mode, Sleep duration = 2ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x83); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x01); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4C); // Select Mag register + Wire.write(0x00); // Normal Mode, ODR = 10 Hz + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4E); // Select Mag register + Wire.write(0x84); // X, Y, Z-Axis enabled + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x51); // Select Mag register + Wire.write(0x04); // No. of Repetitions for X-Y Axis = 9 + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x52); // Select Mag register + Wire.write(0x16); // No. of Repetitions for Z-Axis = 15 + Wire.endTransmission(); + + Serial.println(F("IMU init done")); +} + + +void IMU_UpdateAll() { + IMU_UpdateAcc(); + IMU_UpdateGyr(); + IMU_UpdateMag(); +} + + +void IMU_UpdateAcc() { + // int data[6]; + uint8_t data[6]; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write((2 + i));// Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_ACCL, 1);// Request 1 byte of data + // Read 6 bytes of data + // imu.xAccl lsb, imu.xAccl msb, imu.yAccl lsb, imu.yAccl msb, imu.zAccl lsb, imu.zAccl msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data to 12-bits + imu.xAccl = ((data[1] * 256) + (data[0] & 0xF0)) / 16; + if (imu.xAccl > 2047) imu.xAccl -= 4096; + imu.yAccl = ((data[3] * 256) + (data[2] & 0xF0)) / 16; + if (imu.yAccl > 2047) imu.yAccl -= 4096; + imu.zAccl = ((data[5] * 256) + (data[4] & 0xF0)) / 16; + if (imu.zAccl > 2047) imu.zAccl -= 4096; + imu.xAccl = imu.xAccl * 0.0098; // renge +-2g + imu.yAccl = imu.yAccl * 0.0098; // renge +-2g + imu.zAccl = imu.zAccl * 0.0098; // renge +-2g +} + + +void IMU_UpdateGyr() { + // int data[6]; + uint8_t data[6]; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write((2 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_GYRO, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xGyro lsb, imu.xGyro msb, imu.yGyro lsb, imu.yGyro msb, imu.zGyro lsb, imu.zGyro msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xGyro = (data[1] * 256) + data[0]; + if (imu.xGyro > 32767) imu.xGyro -= 65536; + imu.yGyro = (data[3] * 256) + data[2]; + if (imu.yGyro > 32767) imu.yGyro -= 65536; + imu.zGyro = (data[5] * 256) + data[4]; + if (imu.zGyro > 32767) imu.zGyro -= 65536; + + imu.xGyro = imu.xGyro * 0.0038; // Full scale = +/- 125 degree/s + imu.yGyro = imu.yGyro * 0.0038; // Full scale = +/- 125 degree/s + imu.zGyro = imu.zGyro * 0.0038; // Full scale = +/- 125 degree/s +} + + +void IMU_UpdateMag() { + // int data[8]; + uint8_t data[8]; + for (uint8_t i = 0; i < 8; i++) + { + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write((0x42 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_MAG, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xMag lsb, imu.xMag msb, imu.yMag lsb, imu.yMag msb, imu.zMag lsb, imu.zMag msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xMag = ((data[1] <<8) | (data[0]>>3)); + if (imu.xMag > 4095) imu.xMag -= 8192; + imu.yMag = ((data[3] <<8) | (data[2]>>3)); + if (imu.yMag > 4095) imu.yMag -= 8192; + imu.zMag = ((data[5] <<8) | (data[4]>>3)); + if (imu.zMag > 16383) imu.zMag -= 32768; +} + + +void IMU_PrintAcc() { + Serial.print(F("Acc= ")); + Serial.print(imu.xAccl); + Serial.print(F(",")); + Serial.print(imu.yAccl); + Serial.print(F(",")); + Serial.print(imu.zAccl); + Serial.println(F("")); +} + + +void IMU_PrintGyr() { + Serial.print(F("Gyr= ")); + Serial.print(imu.xGyro); + Serial.print(F(",")); + Serial.print(imu.yGyro); + Serial.print(F(",")); + Serial.print(imu.zGyro); + Serial.println(F("")); +} + + +void IMU_PrintMag() { + Serial.print(F("Mag= ")); + Serial.print(imu.xMag); + Serial.print(F(",")); + Serial.print(imu.yMag); + Serial.print(F(",")); + Serial.print(imu.zMag); + Serial.println(F("")); +} + + +float IMU_GetAccX() { + return imu.xAccl; +} +float IMU_GetAccY() { + return imu.yAccl; +} +float IMU_GetAccZ() { + return imu.zAccl; +} +float IMU_GetGyrX() { + return imu.xGyro; +} +float IMU_GetGyrY() { + return imu.yGyro; +} +float IMU_GetGyrZ() { + return imu.zGyro; +} +int IMU_GetMagX() { + return imu.xMag; +} +int IMU_GetMagY() { + return imu.yMag; +} +int IMU_GetMagZ() { + return imu.zMag; +} diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/02_TsurugaA/PIN_ASSIGN.h new file mode 100644 index 0000000..966d5ab --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/PIN_ASSIGN.h @@ -0,0 +1,29 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 +#define PIN_SRV2 5 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif + diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.h b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.h new file mode 100644 index 0000000..5ea55f9 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.h @@ -0,0 +1,27 @@ +#ifndef SRV_H +#define SRV_H + +#include "./PIN_ASSIGN.h" +#include + +/* +http://akizukidenshi.com/catalog/g/gM-08914/ +*/ + +// #################### Servo #################### + + +void SRV_Init(); +void SRV_SetPosition(uint8_t pos); +uint8_t SRV_GetPosition(); +void SRV_Run(); + +typedef struct { + Servo servo; + uint8_t setPosition; + uint8_t position; +} Servo_t; + + +#endif + diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.ino b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.ino new file mode 100644 index 0000000..98d43a5 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo.ino @@ -0,0 +1,25 @@ +#include "./Servo.h" + + +Servo_t servo; + +void SRV_Init() { + servo.position = 0; + servo.setPosition = 0; + + servo.servo.attach(PIN_SRV); + delay(1000); + + // Serial.println(F("SRV init done")); + Serial.println(F("SRV init done")); +} + +void SRV_SetPosition(uint8_t pos) { + servo.setPosition = pos; +} + + +void SRV_Run() { + servo.servo.write(servo.setPosition); + servo.position = servo.setPosition; +} diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.h b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.h new file mode 100644 index 0000000..5cd25ce --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.h @@ -0,0 +1,26 @@ +#ifndef SRV2_H +#define SRV2_H + +#include "./PIN_ASSIGN.h" +#include + +/* +http://akizukidenshi.com/catalog/g/gM-08914/ +*/ + +// #################### Servo #################### + + +void SRV2_Init(); +void SRV2_SetPosition(uint8_t pos); +uint8_t SRV2_GetPosition(); +void SRV2_Run(); + +// typedef struct { +// Servo servo; +// uint8_t setPosition; +// uint8_t position; +// } Servo_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.ino b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.ino new file mode 100644 index 0000000..5afdb40 --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/Servo2.ino @@ -0,0 +1,26 @@ +#include "./Servo.h" +#include "./Servo2.h" + + +Servo_t servo2; + +void SRV2_Init() { + servo2.position = 0; + servo2.setPosition = 0; + + servo2.servo.attach(PIN_SRV2); + delay(1000); + + // Serial.println(F("SRV2 init done")); + Serial.println(F("SRV2 init done")); +} + +void SRV2_SetPosition(uint8_t pos) { + servo2.setPosition = pos; +} + + +void SRV2_Run(){ + servo2.servo.write(servo2.setPosition); + servo2.position = servo2.setPosition; +} diff --git a/Archives/2020-02-22_Fukui/02_TsurugaA/Test.ino b/Archives/2020-02-22_Fukui/02_TsurugaA/Test.ino new file mode 100644 index 0000000..4a1a21a --- /dev/null +++ b/Archives/2020-02-22_Fukui/02_TsurugaA/Test.ino @@ -0,0 +1,74 @@ +#include "./IMU.h" +//#include "./GPS.h" +#include "./Servo.h" +#include "./Servo2.h" + + +void setup() +{ + // Wire(Arduino-I2C)の初期化 + Wire.begin(); + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + IMU_Init(); + //GPS_Init(); + SRV_Init(); + SRV2_Init(); + + Serial.println(F("Pos=0")); + SRV_SetPosition(0); + SRV2_SetPosition(180); + SRV_Run(); + SRV2_Run(); + delay(2000); + + Serial.println(F("Init done")); + delay(300); +} + +void loop(){ + IMU_UpdateAll(); + IMU_PrintAcc(); + //IMU_PrintGyr(); + //IMU_PrintMag(); + //GPS_Update(); + //GPS_Print(); + +float Acc = IMU_GetAccZ(); + if((Acc>9.7)&&(Acc<10)){ + + + Serial.println(F("Pos=0")); + SRV_SetPosition(0); + SRV2_SetPosition(180); + SRV_Run(); + SRV2_Run(); + delay(2000); + + Serial.println(F("Pos=180")); + SRV_SetPosition(180); + SRV2_SetPosition(0); + SRV_Run(); + SRV2_Run(); + delay(2000); + + +// Serial.println(F("Move Up!")); +// for(int pos = 0; pos <= 180; pos = pos + 180) { +// Serial.print(F("pos:")); +// Serial.println(pos); +// SRV_SetPosition(pos); +// SRV2_SetPosition(180-pos); +// SRV_Run(); +// SRV2_Run(); +// delay(1000); +// } + + + } + + Serial.println(F("END")); + while (1) { + } +} diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.cpp b/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.cpp new file mode 100644 index 0000000..e2a383a --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.cpp @@ -0,0 +1,484 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#include "Adafruit_VC0706.h" + +// Initialization code used by all constructor types +void Adafruit_VC0706::common_init(void) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + swSerial = NULL; +#endif + hwSerial = NULL; + frameptr = 0; + bufferLen = 0; + serialNum = 0; +} + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) +// Constructor when using SoftwareSerial or NewSoftSerial + #if ARDUINO >= 100 + Adafruit_VC0706::Adafruit_VC0706(SoftwareSerial *ser) { + #else + Adafruit_VC0706::Adafruit_VC0706(NewSoftSerial *ser) { + #endif + common_init(); // Set everything to common state, then... + swSerial = ser; // ...override swSerial with value passed. +} +#endif + + +// Constructor when using HardwareSerial +Adafruit_VC0706::Adafruit_VC0706(HardwareSerial *ser) { + common_init(); // Set everything to common state, then... + hwSerial = ser; // ...override hwSerial with value passed. +} + +boolean Adafruit_VC0706::begin(uint16_t baud) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) swSerial->begin(baud); + else +#endif + hwSerial->begin(baud); + return reset(); +} + +boolean Adafruit_VC0706::reset() { + uint8_t args[] = {0x0}; + + return runCommand(VC0706_RESET, args, 1, 5); +} + +// boolean Adafruit_VC0706::motionDetected() { +// if (readResponse(4, 200) != 4) { +// return false; +// } +// if (! verifyResponse(VC0706_COMM_MOTION_DETECTED)) +// return false; + +// return true; +// } + + +// boolean Adafruit_VC0706::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) { +// uint8_t args[] = {0x03, x, d1, d2}; + +// return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5); +// } + + +// uint8_t Adafruit_VC0706::getMotionStatus(uint8_t x) { +// uint8_t args[] = {0x01, x}; + +// return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5); +// } + + +// boolean Adafruit_VC0706::setMotionDetect(boolean flag) { +// if (! setMotionStatus(VC0706_MOTIONCONTROL, +// VC0706_UARTMOTION, VC0706_ACTIVATEMOTION)) +// return false; + +// uint8_t args[] = {0x01, flag}; + +// runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5); +// } + + + +// boolean Adafruit_VC0706::getMotionDetect(void) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6)) +// return false; + +// return camerabuff[5]; +// } + +// uint8_t Adafruit_VC0706::getImageSize() { +// uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19}; +// if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6)) +// return -1; + +// return camerabuff[5]; +// } + +boolean Adafruit_VC0706::setImageSize(uint8_t x) { + uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x}; + + return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +} + +/****************** downsize image control */ + +// uint8_t Adafruit_VC0706::getDownsize(void) { +// uint8_t args[] = {0x0}; +// if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6)) +// return -1; + +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setDownsize(uint8_t newsize) { +// uint8_t args[] = {0x01, newsize}; + +// return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5); +// } + +/***************** other high level commands */ + +// char * Adafruit_VC0706::getVersion(void) { +// uint8_t args[] = {0x01}; + +// sendCommand(VC0706_GEN_VERSION, args, 1); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + + +/***************** baud rate commands */ + +// char* Adafruit_VC0706::setBaud9600() { +// uint8_t args[] = {0x03, 0x01, 0xAE, 0xC8}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! + +// } + +// char* Adafruit_VC0706::setBaud19200() { +// uint8_t args[] = {0x03, 0x01, 0x56, 0xE4}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud38400() { +// uint8_t args[] = {0x03, 0x01, 0x2A, 0xF2}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud57600() { +// uint8_t args[] = {0x03, 0x01, 0x1C, 0x1C}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud115200() { +// uint8_t args[] = {0x03, 0x01, 0x0D, 0xA6}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +/****************** high level photo comamnds */ + +// void Adafruit_VC0706::OSD(uint8_t x, uint8_t y, char *str) { +// if (strlen(str) > 14) { str[13] = 0; } + +// uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)}; + +// for (uint8_t i=0; i= '0') && (c <= '9')) { +// str[i] -= '0'; +// } else if ((c >= 'A') && (c <= 'Z')) { +// str[i] -= 'A'; +// str[i] += 10; +// } else if ((c >= 'a') && (c <= 'z')) { +// str[i] -= 'a'; +// str[i] += 36; +// } + +// args[3+i] = str[i]; +// } + +// runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5); +// printBuff(); +// } + +// boolean Adafruit_VC0706::setCompression(uint8_t c) { +// uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c}; +// return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +// } + +// uint8_t Adafruit_VC0706::getCompression(void) { +// uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04}; +// runCommand(VC0706_READ_DATA, args, sizeof(args), 6); +// printBuff(); +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) { +// uint8_t args[] = {0x08, wz >> 8, wz, +// hz >> 8, wz, +// pan>>8, pan, +// tilt>>8, tilt}; + +// return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5)); +// } + + +// boolean Adafruit_VC0706::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16)) +// return false; +// printBuff(); + +// w = camerabuff[5]; +// w <<= 8; +// w |= camerabuff[6]; + +// h = camerabuff[7]; +// h <<= 8; +// h |= camerabuff[8]; + +// wz = camerabuff[9]; +// wz <<= 8; +// wz |= camerabuff[10]; + +// hz = camerabuff[11]; +// hz <<= 8; +// hz |= camerabuff[12]; + +// pan = camerabuff[13]; +// pan <<= 8; +// pan |= camerabuff[14]; + +// tilt = camerabuff[15]; +// tilt <<= 8; +// tilt |= camerabuff[16]; + +// return true; +// } + + +boolean Adafruit_VC0706::takePicture() { + frameptr = 0; + return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME); +} + +// boolean Adafruit_VC0706::resumeVideo() { +// return cameraFrameBuffCtrl(VC0706_RESUMEFRAME); +// } + +// boolean Adafruit_VC0706::TVon() { +// uint8_t args[] = {0x1, 0x1}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } +// boolean Adafruit_VC0706::TVoff() { +// uint8_t args[] = {0x1, 0x0}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } + +boolean Adafruit_VC0706::cameraFrameBuffCtrl(uint8_t command) { + uint8_t args[] = {0x1, command}; + return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5); +} + +uint32_t Adafruit_VC0706::frameLength(void) { + uint8_t args[] = {0x01, 0x00}; + if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9)) + return 0; + + uint32_t len; + len = camerabuff[5]; + len <<= 8; + len |= camerabuff[6]; + len <<= 8; + len |= camerabuff[7]; + len <<= 8; + len |= camerabuff[8]; + + return len; +} + + +uint8_t Adafruit_VC0706::available(void) { + return bufferLen; +} + + +uint8_t * Adafruit_VC0706::readPicture(uint8_t n) { + uint8_t args[] = {0x0C, 0x0, 0x0A, + 0, 0, frameptr >> 8, frameptr & 0xFF, + 0, 0, 0, n, + CAMERADELAY >> 8, CAMERADELAY & 0xFF}; + + if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false)) + return 0; + + + // read into the buffer PACKETLEN! + if (readResponse(n+5, CAMERADELAY) == 0) + return 0; + + + frameptr += n; + + return camerabuff; +} + +/**************** low level commands */ + + +boolean Adafruit_VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn, + uint8_t resplen, boolean flushflag) { + // flush out anything in the buffer? + if (flushflag) { + // readResponse(100, 10); + // ●●● 変えた!!! ●●● + readResponse(CAMERABUFFSIZ, 10); + } + + sendCommand(cmd, args, argn); + if (readResponse(resplen, 200) != resplen) + return false; + if (! verifyResponse(cmd)) + return false; + return true; +} + +void Adafruit_VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) { +#if ARDUINO >= 100 + swSerial->write((byte)0x56); + swSerial->write((byte)serialNum); + swSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + swSerial->print(0x56, BYTE); + swSerial->print(serialNum, BYTE); + swSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } + else +#endif + { +#if ARDUINO >= 100 + hwSerial->write((byte)0x56); + hwSerial->write((byte)serialNum); + hwSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + hwSerial->print(0x56, BYTE); + hwSerial->print(serialNum, BYTE); + hwSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } +//Serial.println(); +} + +uint8_t Adafruit_VC0706::readResponse(uint8_t numbytes, uint8_t timeout) { + uint8_t counter = 0; + bufferLen = 0; + int avail; + + while ((timeout != counter) && (bufferLen != numbytes)){ +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + avail = swSerial ? swSerial->available() : hwSerial->available(); +#else + avail = hwSerial->available(); +#endif + if (avail <= 0) { + delay(1); + counter++; + continue; + } + counter = 0; + // there's a byte! +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read(); +#else + camerabuff[bufferLen++] = hwSerial->read(); +#endif + // ●●● 安全コード.足した!!!●●● + if (bufferLen > (CAMERABUFFSIZ - 1)) { + Serial.println(F("CAM BUF WARNING!!!")); + } + } + //printBuff(); +//camerabuff[bufferLen] = 0; +//Serial.println((char*)camerabuff); + return bufferLen; +} + +boolean Adafruit_VC0706::verifyResponse(uint8_t command) { + if ((camerabuff[0] != 0x76) || + (camerabuff[1] != serialNum) || + (camerabuff[2] != command) || + (camerabuff[3] != 0x0)) + return false; + return true; +} + +// void Adafruit_VC0706::printBuff() { +// for (uint8_t i = 0; i< bufferLen; i++) { +// Serial.print(" 0x"); +// Serial.print(camerabuff[i], HEX); +// } +// Serial.println(); +// } diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.h b/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.h new file mode 100644 index 0000000..8ac62e8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/Adafruit_VC0706.h @@ -0,0 +1,132 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#if ARDUINO >= 100 + #include "Arduino.h" + #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #include + #endif +#else + #include "WProgram.h" + #include "NewSoftSerial.h" +#endif + +#define VC0706_RESET 0x26 +// #define VC0706_GEN_VERSION 0x11 +// #define VC0706_SET_PORT 0x24 +#define VC0706_READ_FBUF 0x32 +#define VC0706_GET_FBUF_LEN 0x34 +#define VC0706_FBUF_CTRL 0x36 +// #define VC0706_DOWNSIZE_CTRL 0x54 +// #define VC0706_DOWNSIZE_STATUS 0x55 +// #define VC0706_READ_DATA 0x30 +#define VC0706_WRITE_DATA 0x31 +// #define VC0706_COMM_MOTION_CTRL 0x37 +// #define VC0706_COMM_MOTION_STATUS 0x38 +// #define VC0706_COMM_MOTION_DETECTED 0x39 +// #define VC0706_MOTION_CTRL 0x42 +// #define VC0706_MOTION_STATUS 0x43 +// #define VC0706_TVOUT_CTRL 0x44 +// #define VC0706_OSD_ADD_CHAR 0x45 + +#define VC0706_STOPCURRENTFRAME 0x0 +// #define VC0706_STOPNEXTFRAME 0x1 +// #define VC0706_RESUMEFRAME 0x3 +// #define VC0706_STEPFRAME 0x2 + +#define VC0706_640x480 0x00 +#define VC0706_320x240 0x11 +#define VC0706_160x120 0x22 + +// #define VC0706_MOTIONCONTROL 0x0 +// #define VC0706_UARTMOTION 0x01 +// #define VC0706_ACTIVATEMOTION 0x01 + +// #define VC0706_SET_ZOOM 0x52 +// #define VC0706_GET_ZOOM 0x53 + +// #define CAMERABUFFSIZ 100 +#define CAMERABUFFSIZ 50 // 減らした.多分問題ないが,怖いので安全コードも足した. L.370, L.458あたり +#define CAMERADELAY 10 + + +class Adafruit_VC0706 { + public: +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + Adafruit_VC0706(SoftwareSerial *ser); // Constructor when using SoftwareSerial + #else + Adafruit_VC0706(NewSoftSerial *ser); // Constructor when using NewSoftSerial + #endif +#endif + Adafruit_VC0706(HardwareSerial *ser); // Constructor when using HardwareSerial + boolean begin(uint16_t baud = 38400); + boolean reset(void); + // boolean TVon(void); + // boolean TVoff(void); + boolean takePicture(void); + uint8_t *readPicture(uint8_t n); + // boolean resumeVideo(void); + uint32_t frameLength(void); + // char *getVersion(void); + uint8_t available(); + // uint8_t getDownsize(void); + // boolean setDownsize(uint8_t); + // uint8_t getImageSize(); + boolean setImageSize(uint8_t); + // boolean getMotionDetect(); + // uint8_t getMotionStatus(uint8_t); + // boolean motionDetected(); + // boolean setMotionDetect(boolean f); + // boolean setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2); + boolean cameraFrameBuffCtrl(uint8_t command); + // uint8_t getCompression(); + // boolean setCompression(uint8_t c); + + // boolean getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt); + // boolean setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); + + // void OSD(uint8_t x, uint8_t y, char *s); // isnt supported by the chip :( + +// char* setBaud9600(); +// char* setBaud19200(); +// char* setBaud38400(); +// char* setBaud57600(); +// char* setBaud115200(); + + private: + uint8_t serialNum; + uint8_t camerabuff[CAMERABUFFSIZ+1]; + uint8_t bufferLen; + uint16_t frameptr; + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + SoftwareSerial *swSerial; + #else + NewSoftSerial *swSerial; + #endif +#endif + HardwareSerial *hwSerial; + + void common_init(void); + boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp, boolean flushflag = true); + void sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn); + uint8_t readResponse(uint8_t numbytes, uint8_t timeout); + boolean verifyResponse(uint8_t command); + // void printBuff(void); +}; diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.h b/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.h new file mode 100644 index 0000000..f5b2aee --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.h @@ -0,0 +1,27 @@ +#ifndef CAMERA_H +#define CAMERA_H + +/* +https://www.switch-science.com/catalog/1241/ +https://www.adafruit.com/product/397 +*/ + +// #################### CAMERA #################### + + +#include "./PIN_ASSIGN.h" + +#include "./Adafruit_VC0706.h" +#include +#include +// #include + + +void CAM_Init(); +void CAM_TakePic(); + +typedef struct { +} Camera_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.ino b/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.ino new file mode 100644 index 0000000..11ec1e7 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/Camera.ino @@ -0,0 +1,105 @@ +#include "./Camera.h" +#include "./SD.h" + +Camera_t camera; + +// SoftwareSerial と Adafruit_VC0706 は初期値なしの初期化ができないので,仕方なくstructにはいれない. +SoftwareSerial CamSerial = SoftwareSerial(PIN_CAM_TX, PIN_CAM_RX); +Adafruit_VC0706 cam = Adafruit_VC0706(&CamSerial); + + +void CAM_Init() { + // Try to locate the camera + if (cam.begin()) { + Serial.println(F("Cam Found")); + } else { + Serial.println(F("No cam!!")); + return; + } + // // Print out the camera version information (optional) + // char *reply = cam.getVersion(); + // if (reply == 0) { + // Serial.println(F("Failed to get version")); + // } else { + // Serial.println(F("-----------------")); + // Serial.print(reply); + // Serial.println(F("-----------------")); + // } + + cam.setImageSize(VC0706_640x480); // biggest + // cam.setImageSize(VC0706_320x240); // medium + // cam.setImageSize(VC0706_160x120); // small + + + // バッファークリアの意味も兼ねて一発撮る + // 壊れた画像になるので確認. + CAM_TakePic(); +} + + +void CAM_TakePic() { + SD_Write(F("TakePic")); + + // You can read the size back from the camera (optional, but maybe useful?) + // uint8_t imgsize = cam.getImageSize(); + // Serial.print("Image size: "); + // if (imgsize == VC0706_640x480) Serial.println("640x480"); + // if (imgsize == VC0706_320x240) Serial.println("320x240"); + // if (imgsize == VC0706_160x120) Serial.println("160x120"); + + if (! cam.takePicture()) { + Serial.println(F("Snap NG")); + // return; + } else { + Serial.println(F("Snap OK")); + } + + // Create an image with the name IMGxx.JPG + char filename[12]; + strcpy(filename, "000.JPG"); + for (uint16_t i = 0; i < 1000; i++) { + filename[0] = '0' + i/100; + filename[1] = '0' + (i/10)%10; + filename[2] = '0' + i%10; + // create if does not exist, do not open existing, write, sync after write + if ( ! SD.exists(SD_GetDirName() + String(filename)) ) { + break; + } + } + + SD_Write("picname:" + SD_GetDirName() + String(filename)); + Serial.println(SD_GetDirName() + String(filename)); + + // Open the file for writing + File imgFile = SD.open(SD_GetDirName() + String(filename), FILE_WRITE); + + // Get the size of the image (frame) taken + uint16_t jpglen = cam.frameLength(); + Serial.print(F("Storing ")); + Serial.print(jpglen, DEC); + Serial.print(F(" byte.")); + + // int32_t time = millis(); + // pinMode(8, OUTPUT); + // Read all the data up to # bytes! + byte wCount = 0; // For counting # of writes + while (jpglen > 0) { + // read 32 bytes at a time; + uint8_t *buffer; + uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups! + buffer = cam.readPicture(bytesToRead); + imgFile.write(buffer, bytesToRead); + if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up + Serial.print('.'); + wCount = 0; + } + //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes"); + jpglen -= bytesToRead; + } + imgFile.close(); + + // time = millis() - time; + Serial.println(F("done!")); + // Serial.print(time); + // Serial.println(F(" ms elapsed")); +} diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/CanSat_Tsuruga_last.ino b/Archives/2020-02-22_Fukui/03_TsurugaB/CanSat_Tsuruga_last.ino new file mode 100644 index 0000000..8878535 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/CanSat_Tsuruga_last.ino @@ -0,0 +1,37 @@ +#include "./IMU.h" +#include "./GPS.h" +#include "./SD.h" +#include "./Camera.h" + +void setup() +{ + // Wire(Arduino-I2C)の初期化 + Wire.begin(); + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + SD_Init(); // これは絶対最初に初期化! + CAM_Init(); // SDの後! + IMU_Init(); + GPS_Init(); + + Serial.println(F("Init done")); + delay(300); +} + +void loop() +{ + IMU_UpdateAll(); + IMU_PrintAcc(); + IMU_PrintGyr(); + IMU_PrintMag(); + GPS_Update(); + float lat = GPS_GetLat(); + float lng = GPS_GetLng(); + GPS_Print(); + if ((lat > 36.1242) && (lat < 36.1244)) { + if ((lng > 136.220310) && (lng < 136.220330)) { + CAM_TakePic(); + } + } +} diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.h b/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.h new file mode 100644 index 0000000..75bc976 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.h @@ -0,0 +1,28 @@ +#ifndef GPS_H +#define GPS_H + +#include +#include "./PIN_ASSIGN.h" + +/* +http://akizukidenshi.com/catalog/g/gK-09991/ +*/ + +// #################### GPS #################### + +void GPS_Init(); +void GPS_Update(); +void GPS_Print(); +float GPS_GetLat(); +float GPS_GetLng(); +float GPS_GetHeight(); + +typedef struct { + float lat; + float lng; + float height; + uint16_t last_received_time; +} Gps_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.ino b/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.ino new file mode 100644 index 0000000..14e4dc5 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/GPS.ino @@ -0,0 +1,171 @@ +#include "./GPS.h" + +Gps_t gps; + +SoftwareSerial GpsSerial(PIN_GPS_TX, PIN_GPS_RX); + +// 非公開関数 +// String GPS_NMEA2DMS_(float val); +// String GPS_NMEA2DM_(float val); +// String GPS_NMEA2DD_(float val); +float GPS_NMEA2DDf_(float val); +// String GPS_UTC2GMT900_(String str); + + + +void GPS_Init() { + GpsSerial.begin(9600); + gps.lat = 0.0; + gps.lng = 0.0; + gps.height = 0.0; + gps.last_received_time = 0; + Serial.println(F("GPS init done")); +} + + +void GPS_Update() { + // 1つのセンテンスを読み込む + String line = GpsSerial.readStringUntil('\n'); + Serial.println(line); + + if(line != ""){ + uint16_t i; + uint8_t index = 0; + uint16_t len = line.length(); + String str = ""; + + // StringListの生成(簡易) + // const uint8_t MAX_LIST = 30; // メモリ節約のために,極限まで小さくしたい! + const uint8_t MAX_LIST = 12; // メモリ節約のために,極限まで小さくしたい! [9]までしか使わないから小さくしても良いはず. + String list[MAX_LIST]; + for (i = 0; i < MAX_LIST; i++) { + list[i] = ""; + } + + // 「,」を区切り文字として文字列を配列にする + for (i = 0; i < len; i++) { + if (index >= MAX_LIST) { + break; + } + if (line[i] == ',') { + list[index++] = str; + str = ""; + continue; + } + str += line[i]; + } + + // $GPGGAセンテンスのみ読み込む + if (list[0] == "$GPGGA") { + + // ステータス + if(list[6] != "0") { + // // 現在時刻 + // Serial.print(GPS_UTC2GMT900_(list[1])); + + // 緯度 + // Serial.print(F(" 緯度:")); + // // Serial.print(GPS_NMEA2DMS_(list[2].toFloat())); + // Serial.print(F("(")); + // Serial.print(GPS_NMEA2DD_(list[2].toFloat())); + // Serial.print(F(")")); + + gps.lat = GPS_NMEA2DDf_(list[2].toFloat()); + + // 経度 + // Serial.print(F(" 経度:")); + // // Serial.print(GPS_NMEA2DMS_(list[4].toFloat())); + // Serial.print(F("(")); + // Serial.print(GPS_NMEA2DD_(list[4].toFloat())); + // Serial.print(F(")")); + + gps.lng = GPS_NMEA2DDf_(list[4].toFloat()); + + // 海抜 + // Serial.print(F(" 海抜:")); + // Serial.print(list[9]); + // // list[10].toLowerCase(); + // // Serial.print(list[10]); + // Serial.print(F(" m")); + + gps.height = list[9].toFloat(); + + gps.last_received_time = millis() / 1000; + Serial.print(F("測位成功")); + // Serial.print(F("GPS OK")); + } else { + Serial.print(F("測位失敗")); + // Serial.print(F("GPS NG")); + } + + Serial.println(F("")); + } + } +} + +void GPS_Print() { + Serial.print(F("測位結果:")); + Serial.print(F("GPS: Lat=")); + Serial.print(GPS_GetLat(),6); + Serial.print(F(", Lng=")); + Serial.print(GPS_GetLng(),6); + Serial.print(F(", Height=")); + Serial.print(GPS_GetHeight(),2); + Serial.print(F(", RecTime=")); + Serial.print(gps.last_received_time); + Serial.println(F("")); +} + +float GPS_GetLat() { + return gps.lat; +} +float GPS_GetLng() { + return gps.lng; +} +float GPS_GetHeight() { + return gps.height; +} + + + + + +// // NMEAの緯度経度を「度分秒」(DMS)の文字列に変換する +// String GPS_NMEA2DMS_(float val) { +// int d = val / 100; +// int m = ((val / 100.0) - d) * 100.0; +// float s = ((((val / 100.0) - d) * 100.0) - m) * 60; +// return String(d) + "度" + String(m) + "分" + String(s, 1) + "秒"; +// } + +// // (未使用)NMEAの緯度経度を「度分」(DM)の文字列に変換する +// String GPS_NMEA2DM_(float val) { +// int d = val / 100; +// float m = ((val / 100.0) - d) * 100.0; +// return String(d) + "度" + String(m, 4) + "分"; +// } + +// // NMEAの緯度経度を「度」(DD)の文字列に変換する +// String GPS_NMEA2DD_(float val) { +// int d = val / 100; +// int m = (((val / 100.0) - d) * 100.0) / 60; +// float s = (((((val / 100.0) - d) * 100.0) - m) * 60) / (60 * 60); +// return String(d + m + s, 6); +// } + +// NMEAの緯度経度を「度」(DD)のfloatに変換する +// dddmm.mmmmmm +float GPS_NMEA2DDf_(float val) { + int d = (int)(val / 100); + float m = (((val / 100.0) - d) * 100.0) / 60; + // float s = (((((val / 100.0) - d) * 100.0) - m) * 60) / (60 * 60); + // return d + m + s; + return d + m; +} + +// // UTC時刻から日本の標準時刻に変換する(GMT+9:00) +// String GPS_UTC2GMT900_(String str) { +// int hh = (str.substring(0,2).toInt()) + 9; +// if(hh > 24) hh = hh - 24; +// return String(hh,DEC) + ":" + str.substring(2,4) + ":" + str.substring(4,6); +// } diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.h b/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.h new file mode 100644 index 0000000..b7be749 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.h @@ -0,0 +1,70 @@ +#ifndef IMU_H +#define IMU_H + + +/* + +http://akizukidenshi.com/catalog/g/gK-13010/ + +//================================================================// +// AE-BMX055 Arduino UNO // +// VCC +5V // +// GND GND // +// SDA A4(SDA) // +// SCL A5(SCL) // +// // +// (JP6,JP4,JP5はショートした状態) // +// http://akizukidenshi.com/catalog/g/gK-13010/ // +//================================================================// +*/ + +// #################### 9AXIS #################### +#include "./PIN_ASSIGN.h" + + +#include +// BMX055 加速度センサのI2Cアドレス +#define IMU_ADDR_ACCL 0x19 // (JP1,JP2,JP3 = Openの時) +// BMX055 ジャイロセンサのI2Cアドレス +#define IMU_ADDR_GYRO 0x69 // (JP1,JP2,JP3 = Openの時) +// BMX055 磁気センサのI2Cアドレス +#define IMU_ADDR_MAG 0x13 // (JP1,JP2,JP3 = Openの時) + + +#define IMU_COEF (100.0) + +void IMU_Init(); +void IMU_UpdateAll(); +void IMU_UpdateAcc(); +void IMU_UpdateGyr(); +void IMU_UpdateMag(); +void IMU_PrintAcc(); +void IMU_PrintGyr(); +void IMU_PrintMag(); + +float IMU_GetAccX(); +float IMU_GetAccY(); +float IMU_GetAccZ(); +float IMU_GetGyrX(); +float IMU_GetGyrY(); +float IMU_GetGyrZ(); +int16_t IMU_GetMagX(); +int16_t IMU_GetMagY(); +int16_t IMU_GetMagZ(); + + +typedef struct { + // floatだとメモリを多く消費するので,intで保持し,Getterで変換 + int16_t xAccl; + int16_t yAccl; + int16_t zAccl; + int16_t xGyro; + int16_t yGyro; + int16_t zGyro; + int16_t xMag ; + int16_t yMag ; + int16_t zMag ; +} Imu_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.ino b/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.ino new file mode 100644 index 0000000..4e0c3fa --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/IMU.ino @@ -0,0 +1,248 @@ +#include "./IMU.h" + +Imu_t imu; + + + +void IMU_Init() { + // Wire.begin();はされている前提! + imu.xAccl = 0; + imu.yAccl = 0; + imu.zAccl = 0; + imu.xGyro = 0; + imu.yGyro = 0; + imu.zGyro = 0; + imu.xMag = 0; + imu.yMag = 0; + imu.zMag = 0; + + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x0F); // Select PMU_Range register + Wire.write(0x03); // Range = +/- 2g + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x10); // Select PMU_BW register + Wire.write(0x08); // Bandwidth = 7.81 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x11); // Select PMU_LPW register + Wire.write(0x00); // Normal mode, Sleep duration = 0.5ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x0F); // Select Range register + Wire.write(0x04); // Full scale = +/- 125 degree/s + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x10); // Select Bandwidth register + Wire.write(0x07); // ODR = 100 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x11); // Select LPM1 register + Wire.write(0x00); // Normal mode, Sleep duration = 2ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x83); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x01); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4C); // Select Mag register + Wire.write(0x00); // Normal Mode, ODR = 10 Hz + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4E); // Select Mag register + Wire.write(0x84); // X, Y, Z-Axis enabled + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x51); // Select Mag register + Wire.write(0x04); // No. of Repetitions for X-Y Axis = 9 + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x52); // Select Mag register + Wire.write(0x16); // No. of Repetitions for Z-Axis = 15 + Wire.endTransmission(); + + Serial.println(F("IMU init done")); +} + + +void IMU_UpdateAll() { + IMU_UpdateAcc(); + IMU_UpdateGyr(); + IMU_UpdateMag(); +} + + +void IMU_UpdateAcc() { + // int data[6]; + uint8_t data[6]; + float temp; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write((2 + i));// Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_ACCL, 1);// Request 1 byte of data + // Read 6 bytes of data + // imu.xAccl lsb, imu.xAccl msb, imu.yAccl lsb, imu.yAccl msb, imu.zAccl lsb, imu.zAccl msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data to 12-bits + temp = ((data[1] * 256) + (data[0] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.xAccl = (int16_t)(temp * IMU_COEF); + + temp = ((data[3] * 256) + (data[2] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.yAccl = (int16_t)(temp * IMU_COEF); + + temp = ((data[5] * 256) + (data[4] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.zAccl = (int16_t)(temp * IMU_COEF); +} + + +void IMU_UpdateGyr() { + // int data[6]; + uint8_t data[6]; + float temp; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write((2 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_GYRO, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xGyro lsb, imu.xGyro msb, imu.yGyro lsb, imu.yGyro msb, imu.zGyro lsb, imu.zGyro msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + temp = (data[1] * 256) + data[0]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.xGyro = (int16_t)(temp * IMU_COEF); + + temp = (data[3] * 256) + data[2]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.yGyro = (int16_t)(temp * IMU_COEF); + + temp = (data[5] * 256) + data[4]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.zGyro = (int16_t)(temp * IMU_COEF); +} + + +void IMU_UpdateMag() { + // int data[8]; + uint8_t data[8]; + for (uint8_t i = 0; i < 8; i++) + { + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write((0x42 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_MAG, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xMag lsb, imu.xMag msb, imu.yMag lsb, imu.yMag msb, imu.zMag lsb, imu.zMag msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xMag = ((data[1] <<8) | (data[0]>>3)); + if (imu.xMag > 4095) imu.xMag -= 8192; + imu.yMag = ((data[3] <<8) | (data[2]>>3)); + if (imu.yMag > 4095) imu.yMag -= 8192; + imu.zMag = ((data[5] <<8) | (data[4]>>3)); + if (imu.zMag > 16383) imu.zMag -= 32768; +} + + +void IMU_PrintAcc() { + Serial.print(F("Acc= ")); + Serial.print(imu.xAccl); + Serial.print(F(",")); + Serial.print(imu.yAccl); + Serial.print(F(",")); + Serial.print(imu.zAccl); + Serial.println(F("")); +} + + +void IMU_PrintGyr() { + Serial.print(F("Gyr= ")); + Serial.print(imu.xGyro); + Serial.print(F(",")); + Serial.print(imu.yGyro); + Serial.print(F(",")); + Serial.print(imu.zGyro); + Serial.println(F("")); +} + + +void IMU_PrintMag() { + Serial.print(F("Mag= ")); + Serial.print(imu.xMag); + Serial.print(F(",")); + Serial.print(imu.yMag); + Serial.print(F(",")); + Serial.print(imu.zMag); + Serial.println(F("")); +} + + +float IMU_GetAccX() { + return imu.xAccl / IMU_COEF; +} +float IMU_GetAccY() { + return imu.yAccl / IMU_COEF; +} +float IMU_GetAccZ() { + return imu.zAccl / IMU_COEF; +} +float IMU_GetGyrX() { + return imu.xGyro / IMU_COEF; +} +float IMU_GetGyrY() { + return imu.yGyro / IMU_COEF; +} +float IMU_GetGyrZ() { + return imu.zGyro / IMU_COEF; +} +int16_t IMU_GetMagX() { + return imu.xMag; +} +int16_t IMU_GetMagY() { + return imu.yMag; +} +int16_t IMU_GetMagZ() { + return imu.zMag; +} diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/03_TsurugaB/PIN_ASSIGN.h new file mode 100644 index 0000000..09b9108 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/PIN_ASSIGN.h @@ -0,0 +1,27 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/SD.h b/Archives/2020-02-22_Fukui/03_TsurugaB/SD.h new file mode 100644 index 0000000..ef9e367 --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/SD.h @@ -0,0 +1,33 @@ +#ifndef SD_H +#define SD_H + + +#include +#include +#include + + +/* +http://akizukidenshi.com/catalog/g/gK-14015/ +http://akizukidenshi.com/download/ds/akizuki/AE-microSD-LLCNV_sch_20190218_01.pdf +*/ + +// #################### SD #################### +#include "./PIN_ASSIGN.h" + + + + +void SD_Init(); +void SD_Write(String str); +String SD_GetDirName(); + +typedef struct { + String logFileName; + // char logFileName[8]; + char DirName[6]; + // File logFile; +} Sd_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/03_TsurugaB/SD.ino b/Archives/2020-02-22_Fukui/03_TsurugaB/SD.ino new file mode 100644 index 0000000..aa8650b --- /dev/null +++ b/Archives/2020-02-22_Fukui/03_TsurugaB/SD.ino @@ -0,0 +1,82 @@ +#include "./SD.h" + +Sd_t sd; + + +// フォルダ分け or 接頭辞 +// #define SD_IS_MKDIR + + +void SD_Init() { + sd.logFileName = "log.txt"; + + pinMode(PIN_SD_SS, OUTPUT); + + if (!SD.begin(PIN_SD_SS)) { + Serial.println(F("SD: init failed!")); + while (1); + } + + + strcpy(sd.DirName, "D000"); + for (uint16_t i = 0; i <= 1000; i++) { + if (i == 1000) { + Serial.println(F("SD: Number of Folder is MAX!")); + while (1); + } + + sd.DirName[1] = '0' + i/100; + sd.DirName[2] = '0' + (i/10)%10; + sd.DirName[3] = '0' + i%10; + if (! SD.exists(SD_GetDirName() + sd.logFileName) ) { + #ifdef SD_IS_MKDIR + SD.mkdir(sd.DirName); // フォルダわけではなく,接頭辞の場合コメントアウト! + #else + #endif + break; + } + } + + + // File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + // if (logFile) { + // // logFile.println("testing 1, 2, 3."); + // logFile.println(F("START UP!!")); + // // logFile.close(); + // Serial.println(F("SD: Write done")); + // } else { + // // if the file didn't open, print an error: + // Serial.println(F("SD: error opening file")); + // } + // logFile.close(); + + SD_Write(F("START UP!!")); + + Serial.println(F("SD init done.")); +} + + +void SD_Write(String str) { + File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + + if (logFile) { + logFile.println("[" + String(millis()) + "]\t" + str); + // close the file: + logFile.close(); + Serial.println(F("SD: Write OK")); + } else { + // if the file didn't open, print an error: + Serial.println(F("SD: Open Error")); + } + + logFile.close(); +} + + +String SD_GetDirName() { + #ifdef SD_IS_MKDIR + return (String(sd.DirName) + "/"); + #else + return (String(sd.DirName) + "_"); + #endif +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.cpp b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.cpp new file mode 100644 index 0000000..e2a383a --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.cpp @@ -0,0 +1,484 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#include "Adafruit_VC0706.h" + +// Initialization code used by all constructor types +void Adafruit_VC0706::common_init(void) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + swSerial = NULL; +#endif + hwSerial = NULL; + frameptr = 0; + bufferLen = 0; + serialNum = 0; +} + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) +// Constructor when using SoftwareSerial or NewSoftSerial + #if ARDUINO >= 100 + Adafruit_VC0706::Adafruit_VC0706(SoftwareSerial *ser) { + #else + Adafruit_VC0706::Adafruit_VC0706(NewSoftSerial *ser) { + #endif + common_init(); // Set everything to common state, then... + swSerial = ser; // ...override swSerial with value passed. +} +#endif + + +// Constructor when using HardwareSerial +Adafruit_VC0706::Adafruit_VC0706(HardwareSerial *ser) { + common_init(); // Set everything to common state, then... + hwSerial = ser; // ...override hwSerial with value passed. +} + +boolean Adafruit_VC0706::begin(uint16_t baud) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) swSerial->begin(baud); + else +#endif + hwSerial->begin(baud); + return reset(); +} + +boolean Adafruit_VC0706::reset() { + uint8_t args[] = {0x0}; + + return runCommand(VC0706_RESET, args, 1, 5); +} + +// boolean Adafruit_VC0706::motionDetected() { +// if (readResponse(4, 200) != 4) { +// return false; +// } +// if (! verifyResponse(VC0706_COMM_MOTION_DETECTED)) +// return false; + +// return true; +// } + + +// boolean Adafruit_VC0706::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) { +// uint8_t args[] = {0x03, x, d1, d2}; + +// return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5); +// } + + +// uint8_t Adafruit_VC0706::getMotionStatus(uint8_t x) { +// uint8_t args[] = {0x01, x}; + +// return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5); +// } + + +// boolean Adafruit_VC0706::setMotionDetect(boolean flag) { +// if (! setMotionStatus(VC0706_MOTIONCONTROL, +// VC0706_UARTMOTION, VC0706_ACTIVATEMOTION)) +// return false; + +// uint8_t args[] = {0x01, flag}; + +// runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5); +// } + + + +// boolean Adafruit_VC0706::getMotionDetect(void) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6)) +// return false; + +// return camerabuff[5]; +// } + +// uint8_t Adafruit_VC0706::getImageSize() { +// uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19}; +// if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6)) +// return -1; + +// return camerabuff[5]; +// } + +boolean Adafruit_VC0706::setImageSize(uint8_t x) { + uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x}; + + return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +} + +/****************** downsize image control */ + +// uint8_t Adafruit_VC0706::getDownsize(void) { +// uint8_t args[] = {0x0}; +// if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6)) +// return -1; + +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setDownsize(uint8_t newsize) { +// uint8_t args[] = {0x01, newsize}; + +// return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5); +// } + +/***************** other high level commands */ + +// char * Adafruit_VC0706::getVersion(void) { +// uint8_t args[] = {0x01}; + +// sendCommand(VC0706_GEN_VERSION, args, 1); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + + +/***************** baud rate commands */ + +// char* Adafruit_VC0706::setBaud9600() { +// uint8_t args[] = {0x03, 0x01, 0xAE, 0xC8}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! + +// } + +// char* Adafruit_VC0706::setBaud19200() { +// uint8_t args[] = {0x03, 0x01, 0x56, 0xE4}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud38400() { +// uint8_t args[] = {0x03, 0x01, 0x2A, 0xF2}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud57600() { +// uint8_t args[] = {0x03, 0x01, 0x1C, 0x1C}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud115200() { +// uint8_t args[] = {0x03, 0x01, 0x0D, 0xA6}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +/****************** high level photo comamnds */ + +// void Adafruit_VC0706::OSD(uint8_t x, uint8_t y, char *str) { +// if (strlen(str) > 14) { str[13] = 0; } + +// uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)}; + +// for (uint8_t i=0; i= '0') && (c <= '9')) { +// str[i] -= '0'; +// } else if ((c >= 'A') && (c <= 'Z')) { +// str[i] -= 'A'; +// str[i] += 10; +// } else if ((c >= 'a') && (c <= 'z')) { +// str[i] -= 'a'; +// str[i] += 36; +// } + +// args[3+i] = str[i]; +// } + +// runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5); +// printBuff(); +// } + +// boolean Adafruit_VC0706::setCompression(uint8_t c) { +// uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c}; +// return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +// } + +// uint8_t Adafruit_VC0706::getCompression(void) { +// uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04}; +// runCommand(VC0706_READ_DATA, args, sizeof(args), 6); +// printBuff(); +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) { +// uint8_t args[] = {0x08, wz >> 8, wz, +// hz >> 8, wz, +// pan>>8, pan, +// tilt>>8, tilt}; + +// return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5)); +// } + + +// boolean Adafruit_VC0706::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16)) +// return false; +// printBuff(); + +// w = camerabuff[5]; +// w <<= 8; +// w |= camerabuff[6]; + +// h = camerabuff[7]; +// h <<= 8; +// h |= camerabuff[8]; + +// wz = camerabuff[9]; +// wz <<= 8; +// wz |= camerabuff[10]; + +// hz = camerabuff[11]; +// hz <<= 8; +// hz |= camerabuff[12]; + +// pan = camerabuff[13]; +// pan <<= 8; +// pan |= camerabuff[14]; + +// tilt = camerabuff[15]; +// tilt <<= 8; +// tilt |= camerabuff[16]; + +// return true; +// } + + +boolean Adafruit_VC0706::takePicture() { + frameptr = 0; + return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME); +} + +// boolean Adafruit_VC0706::resumeVideo() { +// return cameraFrameBuffCtrl(VC0706_RESUMEFRAME); +// } + +// boolean Adafruit_VC0706::TVon() { +// uint8_t args[] = {0x1, 0x1}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } +// boolean Adafruit_VC0706::TVoff() { +// uint8_t args[] = {0x1, 0x0}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } + +boolean Adafruit_VC0706::cameraFrameBuffCtrl(uint8_t command) { + uint8_t args[] = {0x1, command}; + return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5); +} + +uint32_t Adafruit_VC0706::frameLength(void) { + uint8_t args[] = {0x01, 0x00}; + if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9)) + return 0; + + uint32_t len; + len = camerabuff[5]; + len <<= 8; + len |= camerabuff[6]; + len <<= 8; + len |= camerabuff[7]; + len <<= 8; + len |= camerabuff[8]; + + return len; +} + + +uint8_t Adafruit_VC0706::available(void) { + return bufferLen; +} + + +uint8_t * Adafruit_VC0706::readPicture(uint8_t n) { + uint8_t args[] = {0x0C, 0x0, 0x0A, + 0, 0, frameptr >> 8, frameptr & 0xFF, + 0, 0, 0, n, + CAMERADELAY >> 8, CAMERADELAY & 0xFF}; + + if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false)) + return 0; + + + // read into the buffer PACKETLEN! + if (readResponse(n+5, CAMERADELAY) == 0) + return 0; + + + frameptr += n; + + return camerabuff; +} + +/**************** low level commands */ + + +boolean Adafruit_VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn, + uint8_t resplen, boolean flushflag) { + // flush out anything in the buffer? + if (flushflag) { + // readResponse(100, 10); + // ●●● 変えた!!! ●●● + readResponse(CAMERABUFFSIZ, 10); + } + + sendCommand(cmd, args, argn); + if (readResponse(resplen, 200) != resplen) + return false; + if (! verifyResponse(cmd)) + return false; + return true; +} + +void Adafruit_VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) { +#if ARDUINO >= 100 + swSerial->write((byte)0x56); + swSerial->write((byte)serialNum); + swSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + swSerial->print(0x56, BYTE); + swSerial->print(serialNum, BYTE); + swSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } + else +#endif + { +#if ARDUINO >= 100 + hwSerial->write((byte)0x56); + hwSerial->write((byte)serialNum); + hwSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + hwSerial->print(0x56, BYTE); + hwSerial->print(serialNum, BYTE); + hwSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } +//Serial.println(); +} + +uint8_t Adafruit_VC0706::readResponse(uint8_t numbytes, uint8_t timeout) { + uint8_t counter = 0; + bufferLen = 0; + int avail; + + while ((timeout != counter) && (bufferLen != numbytes)){ +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + avail = swSerial ? swSerial->available() : hwSerial->available(); +#else + avail = hwSerial->available(); +#endif + if (avail <= 0) { + delay(1); + counter++; + continue; + } + counter = 0; + // there's a byte! +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read(); +#else + camerabuff[bufferLen++] = hwSerial->read(); +#endif + // ●●● 安全コード.足した!!!●●● + if (bufferLen > (CAMERABUFFSIZ - 1)) { + Serial.println(F("CAM BUF WARNING!!!")); + } + } + //printBuff(); +//camerabuff[bufferLen] = 0; +//Serial.println((char*)camerabuff); + return bufferLen; +} + +boolean Adafruit_VC0706::verifyResponse(uint8_t command) { + if ((camerabuff[0] != 0x76) || + (camerabuff[1] != serialNum) || + (camerabuff[2] != command) || + (camerabuff[3] != 0x0)) + return false; + return true; +} + +// void Adafruit_VC0706::printBuff() { +// for (uint8_t i = 0; i< bufferLen; i++) { +// Serial.print(" 0x"); +// Serial.print(camerabuff[i], HEX); +// } +// Serial.println(); +// } diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.h b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.h new file mode 100644 index 0000000..8ac62e8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Adafruit_VC0706.h @@ -0,0 +1,132 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#if ARDUINO >= 100 + #include "Arduino.h" + #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #include + #endif +#else + #include "WProgram.h" + #include "NewSoftSerial.h" +#endif + +#define VC0706_RESET 0x26 +// #define VC0706_GEN_VERSION 0x11 +// #define VC0706_SET_PORT 0x24 +#define VC0706_READ_FBUF 0x32 +#define VC0706_GET_FBUF_LEN 0x34 +#define VC0706_FBUF_CTRL 0x36 +// #define VC0706_DOWNSIZE_CTRL 0x54 +// #define VC0706_DOWNSIZE_STATUS 0x55 +// #define VC0706_READ_DATA 0x30 +#define VC0706_WRITE_DATA 0x31 +// #define VC0706_COMM_MOTION_CTRL 0x37 +// #define VC0706_COMM_MOTION_STATUS 0x38 +// #define VC0706_COMM_MOTION_DETECTED 0x39 +// #define VC0706_MOTION_CTRL 0x42 +// #define VC0706_MOTION_STATUS 0x43 +// #define VC0706_TVOUT_CTRL 0x44 +// #define VC0706_OSD_ADD_CHAR 0x45 + +#define VC0706_STOPCURRENTFRAME 0x0 +// #define VC0706_STOPNEXTFRAME 0x1 +// #define VC0706_RESUMEFRAME 0x3 +// #define VC0706_STEPFRAME 0x2 + +#define VC0706_640x480 0x00 +#define VC0706_320x240 0x11 +#define VC0706_160x120 0x22 + +// #define VC0706_MOTIONCONTROL 0x0 +// #define VC0706_UARTMOTION 0x01 +// #define VC0706_ACTIVATEMOTION 0x01 + +// #define VC0706_SET_ZOOM 0x52 +// #define VC0706_GET_ZOOM 0x53 + +// #define CAMERABUFFSIZ 100 +#define CAMERABUFFSIZ 50 // 減らした.多分問題ないが,怖いので安全コードも足した. L.370, L.458あたり +#define CAMERADELAY 10 + + +class Adafruit_VC0706 { + public: +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + Adafruit_VC0706(SoftwareSerial *ser); // Constructor when using SoftwareSerial + #else + Adafruit_VC0706(NewSoftSerial *ser); // Constructor when using NewSoftSerial + #endif +#endif + Adafruit_VC0706(HardwareSerial *ser); // Constructor when using HardwareSerial + boolean begin(uint16_t baud = 38400); + boolean reset(void); + // boolean TVon(void); + // boolean TVoff(void); + boolean takePicture(void); + uint8_t *readPicture(uint8_t n); + // boolean resumeVideo(void); + uint32_t frameLength(void); + // char *getVersion(void); + uint8_t available(); + // uint8_t getDownsize(void); + // boolean setDownsize(uint8_t); + // uint8_t getImageSize(); + boolean setImageSize(uint8_t); + // boolean getMotionDetect(); + // uint8_t getMotionStatus(uint8_t); + // boolean motionDetected(); + // boolean setMotionDetect(boolean f); + // boolean setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2); + boolean cameraFrameBuffCtrl(uint8_t command); + // uint8_t getCompression(); + // boolean setCompression(uint8_t c); + + // boolean getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt); + // boolean setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); + + // void OSD(uint8_t x, uint8_t y, char *s); // isnt supported by the chip :( + +// char* setBaud9600(); +// char* setBaud19200(); +// char* setBaud38400(); +// char* setBaud57600(); +// char* setBaud115200(); + + private: + uint8_t serialNum; + uint8_t camerabuff[CAMERABUFFSIZ+1]; + uint8_t bufferLen; + uint16_t frameptr; + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + SoftwareSerial *swSerial; + #else + NewSoftSerial *swSerial; + #endif +#endif + HardwareSerial *hwSerial; + + void common_init(void); + boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp, boolean flushflag = true); + void sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn); + uint8_t readResponse(uint8_t numbytes, uint8_t timeout); + boolean verifyResponse(uint8_t command); + // void printBuff(void); +}; diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.h b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.h new file mode 100644 index 0000000..f5b2aee --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.h @@ -0,0 +1,27 @@ +#ifndef CAMERA_H +#define CAMERA_H + +/* +https://www.switch-science.com/catalog/1241/ +https://www.adafruit.com/product/397 +*/ + +// #################### CAMERA #################### + + +#include "./PIN_ASSIGN.h" + +#include "./Adafruit_VC0706.h" +#include +#include +// #include + + +void CAM_Init(); +void CAM_TakePic(); + +typedef struct { +} Camera_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.ino new file mode 100644 index 0000000..69a009b --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Camera.ino @@ -0,0 +1,112 @@ +#include "./Camera.h" +#include "./SD.h" +//#include "./Servo.h" + + +Camera_t camera; + +// SoftwareSerial と Adafruit_VC0706 は初期値なしの初期化ができないので,仕方なくstructにはいれない. +SoftwareSerial CamSerial = SoftwareSerial(PIN_CAM_TX, PIN_CAM_RX); +Adafruit_VC0706 cam = Adafruit_VC0706(&CamSerial); + + +void CAM_Init() { + // Try to locate the camera + if (cam.begin()) { + Serial.println(F("Cam Found")); + } else { + Serial.println(F("No cam!!")); + return; + } + // // Print out the camera version information (optional) + // char *reply = cam.getVersion(); + // if (reply == 0) { + // Serial.println(F("Failed to get version")); + // } else { + // Serial.println(F("-----------------")); + // Serial.print(reply); + // Serial.println(F("-----------------")); + // } + + cam.setImageSize(VC0706_640x480); // biggest + // cam.setImageSize(VC0706_320x240); // medium + // cam.setImageSize(VC0706_160x120); // small + + + // バッファークリアの意味も兼ねて一発撮る + // 壊れた画像になるので確認. + CAM_TakePic(); +} + + +void CAM_TakePic() { + SD_Write(F("TakePic")); + + // You can read the size back from the camera (optional, but maybe useful?) + // uint8_t imgsize = cam.getImageSize(); + // Serial.print("Image size: "); + // if (imgsize == VC0706_640x480) Serial.println("640x480"); + // if (imgsize == VC0706_320x240) Serial.println("320x240"); + // if (imgsize == VC0706_160x120) Serial.println("160x120"); + + if (! cam.takePicture()) { + Serial.println(F("Snap NG")); + // return; + } else { + Serial.println(F("Snap OK")); + } + + // Create an image with the name IMGxx.JPG + char filename[12]; + strcpy(filename, "000.JPG"); + for (uint16_t i = 0; i < 1000; i++) { + filename[0] = '0' + i/100; + filename[1] = '0' + (i/10)%10; + filename[2] = '0' + i%10; + // create if does not exist, do not open existing, write, sync after write + if ( ! SD.exists(SD_GetDirName() + String(filename)) ) { + break; + } + } + + SD_Write("picname:" + SD_GetDirName() + String(filename)); + Serial.println(SD_GetDirName() + String(filename)); + + // Open the file for writing + File imgFile = SD.open(SD_GetDirName() + String(filename), FILE_WRITE); + + // Get the size of the image (frame) taken + uint16_t jpglen = cam.frameLength(); + Serial.print(F("Storing ")); + Serial.print(jpglen, DEC); + Serial.print(F(" byte.")); + + // サーボのプルプルを防ぐ +// SRV_Detach(); + + // int32_t time = millis(); + // pinMode(8, OUTPUT); + // Read all the data up to # bytes! + byte wCount = 0; // For counting # of writes + while (jpglen > 0) { + // read 32 bytes at a time; + uint8_t *buffer; + uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups! + buffer = cam.readPicture(bytesToRead); + imgFile.write(buffer, bytesToRead); + if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up + Serial.print('.'); + wCount = 0; + } + //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes"); + jpglen -= bytesToRead; + } + imgFile.close(); + +// SRV_Attach(); + + // time = millis() - time; + Serial.println(F("done!")); + // Serial.print(time); + // Serial.println(F(" ms elapsed")); +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.h b/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.h new file mode 100644 index 0000000..b7be749 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.h @@ -0,0 +1,70 @@ +#ifndef IMU_H +#define IMU_H + + +/* + +http://akizukidenshi.com/catalog/g/gK-13010/ + +//================================================================// +// AE-BMX055 Arduino UNO // +// VCC +5V // +// GND GND // +// SDA A4(SDA) // +// SCL A5(SCL) // +// // +// (JP6,JP4,JP5はショートした状態) // +// http://akizukidenshi.com/catalog/g/gK-13010/ // +//================================================================// +*/ + +// #################### 9AXIS #################### +#include "./PIN_ASSIGN.h" + + +#include +// BMX055 加速度センサのI2Cアドレス +#define IMU_ADDR_ACCL 0x19 // (JP1,JP2,JP3 = Openの時) +// BMX055 ジャイロセンサのI2Cアドレス +#define IMU_ADDR_GYRO 0x69 // (JP1,JP2,JP3 = Openの時) +// BMX055 磁気センサのI2Cアドレス +#define IMU_ADDR_MAG 0x13 // (JP1,JP2,JP3 = Openの時) + + +#define IMU_COEF (100.0) + +void IMU_Init(); +void IMU_UpdateAll(); +void IMU_UpdateAcc(); +void IMU_UpdateGyr(); +void IMU_UpdateMag(); +void IMU_PrintAcc(); +void IMU_PrintGyr(); +void IMU_PrintMag(); + +float IMU_GetAccX(); +float IMU_GetAccY(); +float IMU_GetAccZ(); +float IMU_GetGyrX(); +float IMU_GetGyrY(); +float IMU_GetGyrZ(); +int16_t IMU_GetMagX(); +int16_t IMU_GetMagY(); +int16_t IMU_GetMagZ(); + + +typedef struct { + // floatだとメモリを多く消費するので,intで保持し,Getterで変換 + int16_t xAccl; + int16_t yAccl; + int16_t zAccl; + int16_t xGyro; + int16_t yGyro; + int16_t zGyro; + int16_t xMag ; + int16_t yMag ; + int16_t zMag ; +} Imu_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.ino new file mode 100644 index 0000000..4e0c3fa --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/IMU.ino @@ -0,0 +1,248 @@ +#include "./IMU.h" + +Imu_t imu; + + + +void IMU_Init() { + // Wire.begin();はされている前提! + imu.xAccl = 0; + imu.yAccl = 0; + imu.zAccl = 0; + imu.xGyro = 0; + imu.yGyro = 0; + imu.zGyro = 0; + imu.xMag = 0; + imu.yMag = 0; + imu.zMag = 0; + + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x0F); // Select PMU_Range register + Wire.write(0x03); // Range = +/- 2g + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x10); // Select PMU_BW register + Wire.write(0x08); // Bandwidth = 7.81 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x11); // Select PMU_LPW register + Wire.write(0x00); // Normal mode, Sleep duration = 0.5ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x0F); // Select Range register + Wire.write(0x04); // Full scale = +/- 125 degree/s + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x10); // Select Bandwidth register + Wire.write(0x07); // ODR = 100 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x11); // Select LPM1 register + Wire.write(0x00); // Normal mode, Sleep duration = 2ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x83); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x01); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4C); // Select Mag register + Wire.write(0x00); // Normal Mode, ODR = 10 Hz + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4E); // Select Mag register + Wire.write(0x84); // X, Y, Z-Axis enabled + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x51); // Select Mag register + Wire.write(0x04); // No. of Repetitions for X-Y Axis = 9 + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x52); // Select Mag register + Wire.write(0x16); // No. of Repetitions for Z-Axis = 15 + Wire.endTransmission(); + + Serial.println(F("IMU init done")); +} + + +void IMU_UpdateAll() { + IMU_UpdateAcc(); + IMU_UpdateGyr(); + IMU_UpdateMag(); +} + + +void IMU_UpdateAcc() { + // int data[6]; + uint8_t data[6]; + float temp; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write((2 + i));// Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_ACCL, 1);// Request 1 byte of data + // Read 6 bytes of data + // imu.xAccl lsb, imu.xAccl msb, imu.yAccl lsb, imu.yAccl msb, imu.zAccl lsb, imu.zAccl msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data to 12-bits + temp = ((data[1] * 256) + (data[0] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.xAccl = (int16_t)(temp * IMU_COEF); + + temp = ((data[3] * 256) + (data[2] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.yAccl = (int16_t)(temp * IMU_COEF); + + temp = ((data[5] * 256) + (data[4] & 0xF0)) / 16; + if (temp > 2047) temp -= 4096; + temp = temp * 0.0098; // renge +-2g + imu.zAccl = (int16_t)(temp * IMU_COEF); +} + + +void IMU_UpdateGyr() { + // int data[6]; + uint8_t data[6]; + float temp; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write((2 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_GYRO, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xGyro lsb, imu.xGyro msb, imu.yGyro lsb, imu.yGyro msb, imu.zGyro lsb, imu.zGyro msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + temp = (data[1] * 256) + data[0]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.xGyro = (int16_t)(temp * IMU_COEF); + + temp = (data[3] * 256) + data[2]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.yGyro = (int16_t)(temp * IMU_COEF); + + temp = (data[5] * 256) + data[4]; + if (temp > 32767) temp -= 65536; + temp = temp * 0.0038; // Full scale = +/- 125 degree/s + imu.zGyro = (int16_t)(temp * IMU_COEF); +} + + +void IMU_UpdateMag() { + // int data[8]; + uint8_t data[8]; + for (uint8_t i = 0; i < 8; i++) + { + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write((0x42 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_MAG, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xMag lsb, imu.xMag msb, imu.yMag lsb, imu.yMag msb, imu.zMag lsb, imu.zMag msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xMag = ((data[1] <<8) | (data[0]>>3)); + if (imu.xMag > 4095) imu.xMag -= 8192; + imu.yMag = ((data[3] <<8) | (data[2]>>3)); + if (imu.yMag > 4095) imu.yMag -= 8192; + imu.zMag = ((data[5] <<8) | (data[4]>>3)); + if (imu.zMag > 16383) imu.zMag -= 32768; +} + + +void IMU_PrintAcc() { + Serial.print(F("Acc= ")); + Serial.print(imu.xAccl); + Serial.print(F(",")); + Serial.print(imu.yAccl); + Serial.print(F(",")); + Serial.print(imu.zAccl); + Serial.println(F("")); +} + + +void IMU_PrintGyr() { + Serial.print(F("Gyr= ")); + Serial.print(imu.xGyro); + Serial.print(F(",")); + Serial.print(imu.yGyro); + Serial.print(F(",")); + Serial.print(imu.zGyro); + Serial.println(F("")); +} + + +void IMU_PrintMag() { + Serial.print(F("Mag= ")); + Serial.print(imu.xMag); + Serial.print(F(",")); + Serial.print(imu.yMag); + Serial.print(F(",")); + Serial.print(imu.zMag); + Serial.println(F("")); +} + + +float IMU_GetAccX() { + return imu.xAccl / IMU_COEF; +} +float IMU_GetAccY() { + return imu.yAccl / IMU_COEF; +} +float IMU_GetAccZ() { + return imu.zAccl / IMU_COEF; +} +float IMU_GetGyrX() { + return imu.xGyro / IMU_COEF; +} +float IMU_GetGyrY() { + return imu.yGyro / IMU_COEF; +} +float IMU_GetGyrZ() { + return imu.zGyro / IMU_COEF; +} +int16_t IMU_GetMagX() { + return imu.xMag; +} +int16_t IMU_GetMagY() { + return imu.yMag; +} +int16_t IMU_GetMagZ() { + return imu.zMag; +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/05_Koshi/Test01/PIN_ASSIGN.h new file mode 100644 index 0000000..0f3e877 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/PIN_ASSIGN.h @@ -0,0 +1,28 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif + diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.h b/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.h new file mode 100644 index 0000000..ef9e367 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.h @@ -0,0 +1,33 @@ +#ifndef SD_H +#define SD_H + + +#include +#include +#include + + +/* +http://akizukidenshi.com/catalog/g/gK-14015/ +http://akizukidenshi.com/download/ds/akizuki/AE-microSD-LLCNV_sch_20190218_01.pdf +*/ + +// #################### SD #################### +#include "./PIN_ASSIGN.h" + + + + +void SD_Init(); +void SD_Write(String str); +String SD_GetDirName(); + +typedef struct { + String logFileName; + // char logFileName[8]; + char DirName[6]; + // File logFile; +} Sd_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.ino new file mode 100644 index 0000000..aa8650b --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/SD.ino @@ -0,0 +1,82 @@ +#include "./SD.h" + +Sd_t sd; + + +// フォルダ分け or 接頭辞 +// #define SD_IS_MKDIR + + +void SD_Init() { + sd.logFileName = "log.txt"; + + pinMode(PIN_SD_SS, OUTPUT); + + if (!SD.begin(PIN_SD_SS)) { + Serial.println(F("SD: init failed!")); + while (1); + } + + + strcpy(sd.DirName, "D000"); + for (uint16_t i = 0; i <= 1000; i++) { + if (i == 1000) { + Serial.println(F("SD: Number of Folder is MAX!")); + while (1); + } + + sd.DirName[1] = '0' + i/100; + sd.DirName[2] = '0' + (i/10)%10; + sd.DirName[3] = '0' + i%10; + if (! SD.exists(SD_GetDirName() + sd.logFileName) ) { + #ifdef SD_IS_MKDIR + SD.mkdir(sd.DirName); // フォルダわけではなく,接頭辞の場合コメントアウト! + #else + #endif + break; + } + } + + + // File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + // if (logFile) { + // // logFile.println("testing 1, 2, 3."); + // logFile.println(F("START UP!!")); + // // logFile.close(); + // Serial.println(F("SD: Write done")); + // } else { + // // if the file didn't open, print an error: + // Serial.println(F("SD: error opening file")); + // } + // logFile.close(); + + SD_Write(F("START UP!!")); + + Serial.println(F("SD init done.")); +} + + +void SD_Write(String str) { + File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + + if (logFile) { + logFile.println("[" + String(millis()) + "]\t" + str); + // close the file: + logFile.close(); + Serial.println(F("SD: Write OK")); + } else { + // if the file didn't open, print an error: + Serial.println(F("SD: Open Error")); + } + + logFile.close(); +} + + +String SD_GetDirName() { + #ifdef SD_IS_MKDIR + return (String(sd.DirName) + "/"); + #else + return (String(sd.DirName) + "_"); + #endif +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test01/Test01.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Test01.ino new file mode 100644 index 0000000..6f0493e --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test01/Test01.ino @@ -0,0 +1,35 @@ +#include "./IMU.h" +#include "./SD.h" +#include "./Camera.h" + +void setup() +{ + // Wire(Arduino-I2C)の初期化 + Wire.begin(); + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + SD_Init(); // これは絶対最初に初期化! + CAM_Init(); // SDの後! + IMU_Init(); + + Serial.println(F("Init done")); + delay(300); +} + +void loop() +{ + IMU_UpdateAll(); + IMU_PrintAcc(); + IMU_PrintGyr(); + IMU_PrintMag(); + + float zAcc = IMU_GetAccZ(); + + if (zAcc<-7) { + CAM_TakePic(); + } + //SD_Write(String(loop_count)); + + delay(1000); +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.cpp b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.cpp new file mode 100644 index 0000000..e2a383a --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.cpp @@ -0,0 +1,484 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#include "Adafruit_VC0706.h" + +// Initialization code used by all constructor types +void Adafruit_VC0706::common_init(void) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + swSerial = NULL; +#endif + hwSerial = NULL; + frameptr = 0; + bufferLen = 0; + serialNum = 0; +} + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) +// Constructor when using SoftwareSerial or NewSoftSerial + #if ARDUINO >= 100 + Adafruit_VC0706::Adafruit_VC0706(SoftwareSerial *ser) { + #else + Adafruit_VC0706::Adafruit_VC0706(NewSoftSerial *ser) { + #endif + common_init(); // Set everything to common state, then... + swSerial = ser; // ...override swSerial with value passed. +} +#endif + + +// Constructor when using HardwareSerial +Adafruit_VC0706::Adafruit_VC0706(HardwareSerial *ser) { + common_init(); // Set everything to common state, then... + hwSerial = ser; // ...override hwSerial with value passed. +} + +boolean Adafruit_VC0706::begin(uint16_t baud) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) swSerial->begin(baud); + else +#endif + hwSerial->begin(baud); + return reset(); +} + +boolean Adafruit_VC0706::reset() { + uint8_t args[] = {0x0}; + + return runCommand(VC0706_RESET, args, 1, 5); +} + +// boolean Adafruit_VC0706::motionDetected() { +// if (readResponse(4, 200) != 4) { +// return false; +// } +// if (! verifyResponse(VC0706_COMM_MOTION_DETECTED)) +// return false; + +// return true; +// } + + +// boolean Adafruit_VC0706::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) { +// uint8_t args[] = {0x03, x, d1, d2}; + +// return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5); +// } + + +// uint8_t Adafruit_VC0706::getMotionStatus(uint8_t x) { +// uint8_t args[] = {0x01, x}; + +// return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5); +// } + + +// boolean Adafruit_VC0706::setMotionDetect(boolean flag) { +// if (! setMotionStatus(VC0706_MOTIONCONTROL, +// VC0706_UARTMOTION, VC0706_ACTIVATEMOTION)) +// return false; + +// uint8_t args[] = {0x01, flag}; + +// runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5); +// } + + + +// boolean Adafruit_VC0706::getMotionDetect(void) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6)) +// return false; + +// return camerabuff[5]; +// } + +// uint8_t Adafruit_VC0706::getImageSize() { +// uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19}; +// if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6)) +// return -1; + +// return camerabuff[5]; +// } + +boolean Adafruit_VC0706::setImageSize(uint8_t x) { + uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x}; + + return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +} + +/****************** downsize image control */ + +// uint8_t Adafruit_VC0706::getDownsize(void) { +// uint8_t args[] = {0x0}; +// if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6)) +// return -1; + +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setDownsize(uint8_t newsize) { +// uint8_t args[] = {0x01, newsize}; + +// return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5); +// } + +/***************** other high level commands */ + +// char * Adafruit_VC0706::getVersion(void) { +// uint8_t args[] = {0x01}; + +// sendCommand(VC0706_GEN_VERSION, args, 1); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + + +/***************** baud rate commands */ + +// char* Adafruit_VC0706::setBaud9600() { +// uint8_t args[] = {0x03, 0x01, 0xAE, 0xC8}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! + +// } + +// char* Adafruit_VC0706::setBaud19200() { +// uint8_t args[] = {0x03, 0x01, 0x56, 0xE4}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud38400() { +// uint8_t args[] = {0x03, 0x01, 0x2A, 0xF2}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud57600() { +// uint8_t args[] = {0x03, 0x01, 0x1C, 0x1C}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud115200() { +// uint8_t args[] = {0x03, 0x01, 0x0D, 0xA6}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +/****************** high level photo comamnds */ + +// void Adafruit_VC0706::OSD(uint8_t x, uint8_t y, char *str) { +// if (strlen(str) > 14) { str[13] = 0; } + +// uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)}; + +// for (uint8_t i=0; i= '0') && (c <= '9')) { +// str[i] -= '0'; +// } else if ((c >= 'A') && (c <= 'Z')) { +// str[i] -= 'A'; +// str[i] += 10; +// } else if ((c >= 'a') && (c <= 'z')) { +// str[i] -= 'a'; +// str[i] += 36; +// } + +// args[3+i] = str[i]; +// } + +// runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5); +// printBuff(); +// } + +// boolean Adafruit_VC0706::setCompression(uint8_t c) { +// uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c}; +// return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +// } + +// uint8_t Adafruit_VC0706::getCompression(void) { +// uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04}; +// runCommand(VC0706_READ_DATA, args, sizeof(args), 6); +// printBuff(); +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) { +// uint8_t args[] = {0x08, wz >> 8, wz, +// hz >> 8, wz, +// pan>>8, pan, +// tilt>>8, tilt}; + +// return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5)); +// } + + +// boolean Adafruit_VC0706::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16)) +// return false; +// printBuff(); + +// w = camerabuff[5]; +// w <<= 8; +// w |= camerabuff[6]; + +// h = camerabuff[7]; +// h <<= 8; +// h |= camerabuff[8]; + +// wz = camerabuff[9]; +// wz <<= 8; +// wz |= camerabuff[10]; + +// hz = camerabuff[11]; +// hz <<= 8; +// hz |= camerabuff[12]; + +// pan = camerabuff[13]; +// pan <<= 8; +// pan |= camerabuff[14]; + +// tilt = camerabuff[15]; +// tilt <<= 8; +// tilt |= camerabuff[16]; + +// return true; +// } + + +boolean Adafruit_VC0706::takePicture() { + frameptr = 0; + return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME); +} + +// boolean Adafruit_VC0706::resumeVideo() { +// return cameraFrameBuffCtrl(VC0706_RESUMEFRAME); +// } + +// boolean Adafruit_VC0706::TVon() { +// uint8_t args[] = {0x1, 0x1}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } +// boolean Adafruit_VC0706::TVoff() { +// uint8_t args[] = {0x1, 0x0}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } + +boolean Adafruit_VC0706::cameraFrameBuffCtrl(uint8_t command) { + uint8_t args[] = {0x1, command}; + return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5); +} + +uint32_t Adafruit_VC0706::frameLength(void) { + uint8_t args[] = {0x01, 0x00}; + if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9)) + return 0; + + uint32_t len; + len = camerabuff[5]; + len <<= 8; + len |= camerabuff[6]; + len <<= 8; + len |= camerabuff[7]; + len <<= 8; + len |= camerabuff[8]; + + return len; +} + + +uint8_t Adafruit_VC0706::available(void) { + return bufferLen; +} + + +uint8_t * Adafruit_VC0706::readPicture(uint8_t n) { + uint8_t args[] = {0x0C, 0x0, 0x0A, + 0, 0, frameptr >> 8, frameptr & 0xFF, + 0, 0, 0, n, + CAMERADELAY >> 8, CAMERADELAY & 0xFF}; + + if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false)) + return 0; + + + // read into the buffer PACKETLEN! + if (readResponse(n+5, CAMERADELAY) == 0) + return 0; + + + frameptr += n; + + return camerabuff; +} + +/**************** low level commands */ + + +boolean Adafruit_VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn, + uint8_t resplen, boolean flushflag) { + // flush out anything in the buffer? + if (flushflag) { + // readResponse(100, 10); + // ●●● 変えた!!! ●●● + readResponse(CAMERABUFFSIZ, 10); + } + + sendCommand(cmd, args, argn); + if (readResponse(resplen, 200) != resplen) + return false; + if (! verifyResponse(cmd)) + return false; + return true; +} + +void Adafruit_VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) { +#if ARDUINO >= 100 + swSerial->write((byte)0x56); + swSerial->write((byte)serialNum); + swSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + swSerial->print(0x56, BYTE); + swSerial->print(serialNum, BYTE); + swSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } + else +#endif + { +#if ARDUINO >= 100 + hwSerial->write((byte)0x56); + hwSerial->write((byte)serialNum); + hwSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + hwSerial->print(0x56, BYTE); + hwSerial->print(serialNum, BYTE); + hwSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } +//Serial.println(); +} + +uint8_t Adafruit_VC0706::readResponse(uint8_t numbytes, uint8_t timeout) { + uint8_t counter = 0; + bufferLen = 0; + int avail; + + while ((timeout != counter) && (bufferLen != numbytes)){ +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + avail = swSerial ? swSerial->available() : hwSerial->available(); +#else + avail = hwSerial->available(); +#endif + if (avail <= 0) { + delay(1); + counter++; + continue; + } + counter = 0; + // there's a byte! +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read(); +#else + camerabuff[bufferLen++] = hwSerial->read(); +#endif + // ●●● 安全コード.足した!!!●●● + if (bufferLen > (CAMERABUFFSIZ - 1)) { + Serial.println(F("CAM BUF WARNING!!!")); + } + } + //printBuff(); +//camerabuff[bufferLen] = 0; +//Serial.println((char*)camerabuff); + return bufferLen; +} + +boolean Adafruit_VC0706::verifyResponse(uint8_t command) { + if ((camerabuff[0] != 0x76) || + (camerabuff[1] != serialNum) || + (camerabuff[2] != command) || + (camerabuff[3] != 0x0)) + return false; + return true; +} + +// void Adafruit_VC0706::printBuff() { +// for (uint8_t i = 0; i< bufferLen; i++) { +// Serial.print(" 0x"); +// Serial.print(camerabuff[i], HEX); +// } +// Serial.println(); +// } diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.h b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.h new file mode 100644 index 0000000..8ac62e8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Adafruit_VC0706.h @@ -0,0 +1,132 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#if ARDUINO >= 100 + #include "Arduino.h" + #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #include + #endif +#else + #include "WProgram.h" + #include "NewSoftSerial.h" +#endif + +#define VC0706_RESET 0x26 +// #define VC0706_GEN_VERSION 0x11 +// #define VC0706_SET_PORT 0x24 +#define VC0706_READ_FBUF 0x32 +#define VC0706_GET_FBUF_LEN 0x34 +#define VC0706_FBUF_CTRL 0x36 +// #define VC0706_DOWNSIZE_CTRL 0x54 +// #define VC0706_DOWNSIZE_STATUS 0x55 +// #define VC0706_READ_DATA 0x30 +#define VC0706_WRITE_DATA 0x31 +// #define VC0706_COMM_MOTION_CTRL 0x37 +// #define VC0706_COMM_MOTION_STATUS 0x38 +// #define VC0706_COMM_MOTION_DETECTED 0x39 +// #define VC0706_MOTION_CTRL 0x42 +// #define VC0706_MOTION_STATUS 0x43 +// #define VC0706_TVOUT_CTRL 0x44 +// #define VC0706_OSD_ADD_CHAR 0x45 + +#define VC0706_STOPCURRENTFRAME 0x0 +// #define VC0706_STOPNEXTFRAME 0x1 +// #define VC0706_RESUMEFRAME 0x3 +// #define VC0706_STEPFRAME 0x2 + +#define VC0706_640x480 0x00 +#define VC0706_320x240 0x11 +#define VC0706_160x120 0x22 + +// #define VC0706_MOTIONCONTROL 0x0 +// #define VC0706_UARTMOTION 0x01 +// #define VC0706_ACTIVATEMOTION 0x01 + +// #define VC0706_SET_ZOOM 0x52 +// #define VC0706_GET_ZOOM 0x53 + +// #define CAMERABUFFSIZ 100 +#define CAMERABUFFSIZ 50 // 減らした.多分問題ないが,怖いので安全コードも足した. L.370, L.458あたり +#define CAMERADELAY 10 + + +class Adafruit_VC0706 { + public: +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + Adafruit_VC0706(SoftwareSerial *ser); // Constructor when using SoftwareSerial + #else + Adafruit_VC0706(NewSoftSerial *ser); // Constructor when using NewSoftSerial + #endif +#endif + Adafruit_VC0706(HardwareSerial *ser); // Constructor when using HardwareSerial + boolean begin(uint16_t baud = 38400); + boolean reset(void); + // boolean TVon(void); + // boolean TVoff(void); + boolean takePicture(void); + uint8_t *readPicture(uint8_t n); + // boolean resumeVideo(void); + uint32_t frameLength(void); + // char *getVersion(void); + uint8_t available(); + // uint8_t getDownsize(void); + // boolean setDownsize(uint8_t); + // uint8_t getImageSize(); + boolean setImageSize(uint8_t); + // boolean getMotionDetect(); + // uint8_t getMotionStatus(uint8_t); + // boolean motionDetected(); + // boolean setMotionDetect(boolean f); + // boolean setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2); + boolean cameraFrameBuffCtrl(uint8_t command); + // uint8_t getCompression(); + // boolean setCompression(uint8_t c); + + // boolean getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt); + // boolean setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); + + // void OSD(uint8_t x, uint8_t y, char *s); // isnt supported by the chip :( + +// char* setBaud9600(); +// char* setBaud19200(); +// char* setBaud38400(); +// char* setBaud57600(); +// char* setBaud115200(); + + private: + uint8_t serialNum; + uint8_t camerabuff[CAMERABUFFSIZ+1]; + uint8_t bufferLen; + uint16_t frameptr; + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + SoftwareSerial *swSerial; + #else + NewSoftSerial *swSerial; + #endif +#endif + HardwareSerial *hwSerial; + + void common_init(void); + boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp, boolean flushflag = true); + void sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn); + uint8_t readResponse(uint8_t numbytes, uint8_t timeout); + boolean verifyResponse(uint8_t command); + // void printBuff(void); +}; diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.h b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.h new file mode 100644 index 0000000..f5b2aee --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.h @@ -0,0 +1,27 @@ +#ifndef CAMERA_H +#define CAMERA_H + +/* +https://www.switch-science.com/catalog/1241/ +https://www.adafruit.com/product/397 +*/ + +// #################### CAMERA #################### + + +#include "./PIN_ASSIGN.h" + +#include "./Adafruit_VC0706.h" +#include +#include +// #include + + +void CAM_Init(); +void CAM_TakePic(); + +typedef struct { +} Camera_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.ino new file mode 100644 index 0000000..1571f3f --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Camera.ino @@ -0,0 +1,108 @@ +#include "./Camera.h" +#include "./SD.h" + + + +Camera_t camera; + +// SoftwareSerial と Adafruit_VC0706 は初期値なしの初期化ができないので,仕方なくstructにはいれない. +SoftwareSerial CamSerial = SoftwareSerial(PIN_CAM_TX, PIN_CAM_RX); +Adafruit_VC0706 cam = Adafruit_VC0706(&CamSerial); + + +void CAM_Init() { + // Try to locate the camera + if (cam.begin()) { + Serial.println(F("Cam Found")); + } else { + Serial.println(F("No cam!!")); + return; + } + // // Print out the camera version information (optional) + // char *reply = cam.getVersion(); + // if (reply == 0) { + // Serial.println(F("Failed to get version")); + // } else { + // Serial.println(F("-----------------")); + // Serial.print(reply); + // Serial.println(F("-----------------")); + // } + + cam.setImageSize(VC0706_640x480); // biggest + // cam.setImageSize(VC0706_320x240); // medium + // cam.setImageSize(VC0706_160x120); // small + + + // バッファークリアの意味も兼ねて一発撮る + // 壊れた画像になるので確認. + CAM_TakePic(); +} + + +void CAM_TakePic() { + SD_Write(F("TakePic")); + + // You can read the size back from the camera (optional, but maybe useful?) + // uint8_t imgsize = cam.getImageSize(); + // Serial.print("Image size: "); + // if (imgsize == VC0706_640x480) Serial.println("640x480"); + // if (imgsize == VC0706_320x240) Serial.println("320x240"); + // if (imgsize == VC0706_160x120) Serial.println("160x120"); + + if (! cam.takePicture()) { + Serial.println(F("Snap NG")); + // return; + } else { + Serial.println(F("Snap OK")); + } + + // Create an image with the name IMGxx.JPG + char filename[12]; + strcpy(filename, "000.JPG"); + for (uint16_t i = 0; i < 1000; i++) { + filename[0] = '0' + i/100; + filename[1] = '0' + (i/10)%10; + filename[2] = '0' + i%10; + // create if does not exist, do not open existing, write, sync after write + if ( ! SD.exists(SD_GetDirName() + String(filename)) ) { + break; + } + } + + SD_Write("picname:" + SD_GetDirName() + String(filename)); + Serial.println(SD_GetDirName() + String(filename)); + + // Open the file for writing + File imgFile = SD.open(SD_GetDirName() + String(filename), FILE_WRITE); + + // Get the size of the image (frame) taken + uint16_t jpglen = cam.frameLength(); + Serial.print(F("Storing ")); + Serial.print(jpglen, DEC); + Serial.print(F(" byte.")); + + // int32_t time = millis(); + // pinMode(8, OUTPUT); + // Read all the data up to # bytes! + byte wCount = 0; // For counting # of writes + while (jpglen > 0) { + // read 32 bytes at a time; + uint8_t *buffer; + uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups! + buffer = cam.readPicture(bytesToRead); + imgFile.write(buffer, bytesToRead); + if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up + Serial.print('.'); + wCount = 0; + } + //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes"); + jpglen -= bytesToRead; + } + imgFile.close(); + + + // time = millis() - time; + Serial.println(F("done!")); + // Serial.print(time); + // Serial.println(F(" ms elapsed")); +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.h b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.h new file mode 100644 index 0000000..76103b6 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.h @@ -0,0 +1,23 @@ +#ifndef LIGHT_H +#define LIGHT_H + + +/* +http://akizukidenshi.com/catalog/g/gI-00110/ +*/ + + +// #################### LIGHT #################### + + +#include "./PIN_ASSIGN.h" + +void LIT_Init(); +int LIT_Get(); +void LIT_Print(); + +typedef struct { +} Light_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.ino new file mode 100644 index 0000000..8bab4a1 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Light.ino @@ -0,0 +1,21 @@ +#include "./Light.h" + +Light_t light; + + + +void LIT_Init() { +} + +int LIT_Get() { + // たくさんとって平均してもいいかも? + int value = analogRead(PIN_LIT); + return value; +} + +void LIT_Print() { + int value = LIT_Get(); + Serial.print(F("Lit= ")); + Serial.print(value); + Serial.println(F("")); +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/05_Koshi/Test02/PIN_ASSIGN.h new file mode 100644 index 0000000..0f3e877 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/PIN_ASSIGN.h @@ -0,0 +1,28 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif + diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.h b/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.h new file mode 100644 index 0000000..ef9e367 --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.h @@ -0,0 +1,33 @@ +#ifndef SD_H +#define SD_H + + +#include +#include +#include + + +/* +http://akizukidenshi.com/catalog/g/gK-14015/ +http://akizukidenshi.com/download/ds/akizuki/AE-microSD-LLCNV_sch_20190218_01.pdf +*/ + +// #################### SD #################### +#include "./PIN_ASSIGN.h" + + + + +void SD_Init(); +void SD_Write(String str); +String SD_GetDirName(); + +typedef struct { + String logFileName; + // char logFileName[8]; + char DirName[6]; + // File logFile; +} Sd_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.ino new file mode 100644 index 0000000..c7cf8ab --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/SD.ino @@ -0,0 +1,82 @@ +#include "./SD.h" + +Sd_t sd; + + +// フォルダ分け or 接頭辞 +// #define SD_IS_MKDIR + + +void SD_Init() { + sd.logFileName = "log.txt"; + + pinMode(PIN_SD_SS, OUTPUT); + + if (!SD.begin(PIN_SD_SS)) { + Serial.println(F("SD: init failed!")); + while (1); + } + + + strcpy(sd.DirName, "D000"); + for (uint16_t i = 0; i <= 1000; i++) { + if (i == 1000) { + Serial.println(F("SD: Number of Folder is MAX!")); + while (1); + } + + sd.DirName[1] = '0' + i/100; + sd.DirName[2] = '0' + (i/10)%10; + sd.DirName[3] = '0' + i%10; + if (! SD.exists(SD_GetDirName() + sd.logFileName) ) { + #ifdef SD_IS_MKDIR + SD.mkdir(sd.DirName); // フォルダわけではなく,接頭辞の場合コメントアウト! + #else + #endif + break; + } + } + + + // File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + // if (logFile) { + // // logFile.println("testing 1, 2, 3."); + // logFile.println(F("START UP!!")); + // // logFile.close(); + // Serial.println(F("SD: Write done")); + // } else { + // // if the file didn't open, print an error: + // Serial.println(F("SD: error opening file")); + // } + // logFile.close(); + + SD_Write(F("START UP!!")); + + Serial.println(F("SD init done.")); +} + + +void SD_Write(String str) { + File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + + if (logFile) { + logFile.println("[" + String(millis()) + "]\t" + str); + // close the file: + logFile.close(); + Serial.println(F("SD: Write OK")); + } else { + // if the file didn't open, print an error: + Serial.println(F("SD: Open Error")); + } + + logFile.close(); +} + + +String SD_GetDirName() { + #ifdef SD_IS_MKDIR + return (String(sd.DirName) + "/"); + #else + return (String(sd.DirName) + "_"); + #endif +} diff --git a/Archives/2020-02-22_Fukui/05_Koshi/Test02/Test02.ino b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Test02.ino new file mode 100644 index 0000000..982ed0a --- /dev/null +++ b/Archives/2020-02-22_Fukui/05_Koshi/Test02/Test02.ino @@ -0,0 +1,41 @@ +#include "./SD.h" +#include "./Light.h" +#include "./Camera.h" + +uint16_t loop_count = 0; + +void setup() +{ + // Wire(Arduino-I2C)の初期化 + Wire.begin(); + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + SD_Init(); // これは絶対最初に初期化! + CAM_Init(); // SDの後! + LIT_Init(); + + + Serial.println(F("Init done")); + delay(10000); +} + +void loop() +{ + uint32_t light = 0; + + { + light = light + LIT_Get(); + } + + LIT_Print(); + if (light <100) { + Serial.println(F("Light is low")); + CAM_TakePic(); + } else { + Serial.println(F("Light is high")); + } + SD_Write(String(loop_count)); +} + + diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/Light.h b/Archives/2020-02-22_Fukui/06_Mikuni/Light.h new file mode 100644 index 0000000..76103b6 --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/Light.h @@ -0,0 +1,23 @@ +#ifndef LIGHT_H +#define LIGHT_H + + +/* +http://akizukidenshi.com/catalog/g/gI-00110/ +*/ + + +// #################### LIGHT #################### + + +#include "./PIN_ASSIGN.h" + +void LIT_Init(); +int LIT_Get(); +void LIT_Print(); + +typedef struct { +} Light_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/Light.ino b/Archives/2020-02-22_Fukui/06_Mikuni/Light.ino new file mode 100644 index 0000000..769690d --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/Light.ino @@ -0,0 +1,21 @@ +#include "./Light.h" + +Light_t light; + + + +void LIT_Init() { +} + +int LIT_Get() { + // たくさんとって平均してもいいかも? + int value = analogRead(PIN_LIT); + return value; +} + +void LIT_Print() { + int value = LIT_Get(); + Serial.print(F("Lit= ")); + Serial.print(value); + Serial.println(F("")); +} diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/06_Mikuni/PIN_ASSIGN.h new file mode 100644 index 0000000..0f3e877 --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/PIN_ASSIGN.h @@ -0,0 +1,28 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif + diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/Servo.h b/Archives/2020-02-22_Fukui/06_Mikuni/Servo.h new file mode 100644 index 0000000..979516a --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/Servo.h @@ -0,0 +1,29 @@ +#ifndef SRV_H +#define SRV_H + +#include "./PIN_ASSIGN.h" +#include + +/* +http://akizukidenshi.com/catalog/g/gM-08914/ +*/ + +// #################### Servo #################### + + +void SRV_Init(); +void SRV_SetPosition(uint8_t pos); +uint8_t SRV_GetPosition(); +void SRV_Run(); +void SRV_Detach(); +void SRV_Attach(); + +typedef struct { + Servo servo; + uint8_t setPosition; + uint8_t position; +} Servo_t; + + +#endif + diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/Servo.ino b/Archives/2020-02-22_Fukui/06_Mikuni/Servo.ino new file mode 100644 index 0000000..51e3db5 --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/Servo.ino @@ -0,0 +1,38 @@ +#include "./Servo.h" + + +Servo_t servo; + +void SRV_Init() { + servo.position = 0; + servo.setPosition = 0; + + // servo.servo.attach(PIN_SRV); + SRV_Attach(); + SRV_Run(); + + // Serial.println(F("SRV init done")); + Serial.println(F("SRV init done")); +} + +void SRV_SetPosition(uint8_t pos) { + servo.setPosition = pos; +} + +uint8_t SRV_GetPosition() { + return servo.position; +} + + +void SRV_Run() { + servo.servo.write(servo.setPosition); + servo.position = servo.setPosition; +} + +void SRV_Detach() { + servo.servo.detach(); +} +void SRV_Attach() { + servo.servo.attach(PIN_SRV); + delay(5000); +} \ No newline at end of file diff --git a/Archives/2020-02-22_Fukui/06_Mikuni/Test02.ino b/Archives/2020-02-22_Fukui/06_Mikuni/Test02.ino new file mode 100644 index 0000000..74390ba --- /dev/null +++ b/Archives/2020-02-22_Fukui/06_Mikuni/Test02.ino @@ -0,0 +1,66 @@ +// サーボモータのライブラリ(便利なコードまとめ)を使うよ! と宣言 +#include "./Servo.h" +// すると, +// Servo.h に宣言されている +// void SRV_Init(); +// void SRV_SetPosition(uint8_t pos); +// uint8_t SRV_GetPosition(); +// void SRV_Run(); +// void SRV_Detach(); +// void SRV_Attach(); +// という関数が使えるようになる +// 関数の中身は Servo.c にかかれている + +// 光センサのライブラリ(便利なコードまとめ)を使うよ! と宣言 +#include "./Light.h" +// すると, +// Light.h に宣言されている +// void LIT_Init(); +// int LIT_Get(); +// void LIT_Print(); +// という関数が使えるようになる +// 関数の中身は Light.c にかかれている + + +void setup() +{ + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + // サーボモータを初期化 + SRV_Init(); + // 光センサを初期化 + LIT_Init(); + + Serial.println(F("Init done")); + delay(300); +} + +void loop() +{ + + while (1) { + Serial.print(F("Light: ")); + Serial.println(LIT_Get()); + if ( LIT_Get() > 300) { + break; + } + delay(100); + } + + delay(1000); + + SRV_SetPosition(180); + SRV_Run(); + + delay(1500); + + SRV_SetPosition(0); + SRV_Run(); + + + + Serial.println(F("END")); + while (1) { + } +} diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.cpp b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.cpp new file mode 100644 index 0000000..e2a383a --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.cpp @@ -0,0 +1,484 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#include "Adafruit_VC0706.h" + +// Initialization code used by all constructor types +void Adafruit_VC0706::common_init(void) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + swSerial = NULL; +#endif + hwSerial = NULL; + frameptr = 0; + bufferLen = 0; + serialNum = 0; +} + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) +// Constructor when using SoftwareSerial or NewSoftSerial + #if ARDUINO >= 100 + Adafruit_VC0706::Adafruit_VC0706(SoftwareSerial *ser) { + #else + Adafruit_VC0706::Adafruit_VC0706(NewSoftSerial *ser) { + #endif + common_init(); // Set everything to common state, then... + swSerial = ser; // ...override swSerial with value passed. +} +#endif + + +// Constructor when using HardwareSerial +Adafruit_VC0706::Adafruit_VC0706(HardwareSerial *ser) { + common_init(); // Set everything to common state, then... + hwSerial = ser; // ...override hwSerial with value passed. +} + +boolean Adafruit_VC0706::begin(uint16_t baud) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) swSerial->begin(baud); + else +#endif + hwSerial->begin(baud); + return reset(); +} + +boolean Adafruit_VC0706::reset() { + uint8_t args[] = {0x0}; + + return runCommand(VC0706_RESET, args, 1, 5); +} + +// boolean Adafruit_VC0706::motionDetected() { +// if (readResponse(4, 200) != 4) { +// return false; +// } +// if (! verifyResponse(VC0706_COMM_MOTION_DETECTED)) +// return false; + +// return true; +// } + + +// boolean Adafruit_VC0706::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) { +// uint8_t args[] = {0x03, x, d1, d2}; + +// return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5); +// } + + +// uint8_t Adafruit_VC0706::getMotionStatus(uint8_t x) { +// uint8_t args[] = {0x01, x}; + +// return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5); +// } + + +// boolean Adafruit_VC0706::setMotionDetect(boolean flag) { +// if (! setMotionStatus(VC0706_MOTIONCONTROL, +// VC0706_UARTMOTION, VC0706_ACTIVATEMOTION)) +// return false; + +// uint8_t args[] = {0x01, flag}; + +// runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5); +// } + + + +// boolean Adafruit_VC0706::getMotionDetect(void) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6)) +// return false; + +// return camerabuff[5]; +// } + +// uint8_t Adafruit_VC0706::getImageSize() { +// uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19}; +// if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6)) +// return -1; + +// return camerabuff[5]; +// } + +boolean Adafruit_VC0706::setImageSize(uint8_t x) { + uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x}; + + return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +} + +/****************** downsize image control */ + +// uint8_t Adafruit_VC0706::getDownsize(void) { +// uint8_t args[] = {0x0}; +// if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6)) +// return -1; + +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setDownsize(uint8_t newsize) { +// uint8_t args[] = {0x01, newsize}; + +// return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5); +// } + +/***************** other high level commands */ + +// char * Adafruit_VC0706::getVersion(void) { +// uint8_t args[] = {0x01}; + +// sendCommand(VC0706_GEN_VERSION, args, 1); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + + +/***************** baud rate commands */ + +// char* Adafruit_VC0706::setBaud9600() { +// uint8_t args[] = {0x03, 0x01, 0xAE, 0xC8}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! + +// } + +// char* Adafruit_VC0706::setBaud19200() { +// uint8_t args[] = {0x03, 0x01, 0x56, 0xE4}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud38400() { +// uint8_t args[] = {0x03, 0x01, 0x2A, 0xF2}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud57600() { +// uint8_t args[] = {0x03, 0x01, 0x1C, 0x1C}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +// char* Adafruit_VC0706::setBaud115200() { +// uint8_t args[] = {0x03, 0x01, 0x0D, 0xA6}; + +// sendCommand(VC0706_SET_PORT, args, sizeof(args)); +// // get reply +// if (!readResponse(CAMERABUFFSIZ, 200)) +// return 0; +// camerabuff[bufferLen] = 0; // end it! +// return (char *)camerabuff; // return it! +// } + +/****************** high level photo comamnds */ + +// void Adafruit_VC0706::OSD(uint8_t x, uint8_t y, char *str) { +// if (strlen(str) > 14) { str[13] = 0; } + +// uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)}; + +// for (uint8_t i=0; i= '0') && (c <= '9')) { +// str[i] -= '0'; +// } else if ((c >= 'A') && (c <= 'Z')) { +// str[i] -= 'A'; +// str[i] += 10; +// } else if ((c >= 'a') && (c <= 'z')) { +// str[i] -= 'a'; +// str[i] += 36; +// } + +// args[3+i] = str[i]; +// } + +// runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5); +// printBuff(); +// } + +// boolean Adafruit_VC0706::setCompression(uint8_t c) { +// uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c}; +// return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5); +// } + +// uint8_t Adafruit_VC0706::getCompression(void) { +// uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04}; +// runCommand(VC0706_READ_DATA, args, sizeof(args), 6); +// printBuff(); +// return camerabuff[5]; +// } + +// boolean Adafruit_VC0706::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) { +// uint8_t args[] = {0x08, wz >> 8, wz, +// hz >> 8, wz, +// pan>>8, pan, +// tilt>>8, tilt}; + +// return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5)); +// } + + +// boolean Adafruit_VC0706::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) { +// uint8_t args[] = {0x0}; + +// if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16)) +// return false; +// printBuff(); + +// w = camerabuff[5]; +// w <<= 8; +// w |= camerabuff[6]; + +// h = camerabuff[7]; +// h <<= 8; +// h |= camerabuff[8]; + +// wz = camerabuff[9]; +// wz <<= 8; +// wz |= camerabuff[10]; + +// hz = camerabuff[11]; +// hz <<= 8; +// hz |= camerabuff[12]; + +// pan = camerabuff[13]; +// pan <<= 8; +// pan |= camerabuff[14]; + +// tilt = camerabuff[15]; +// tilt <<= 8; +// tilt |= camerabuff[16]; + +// return true; +// } + + +boolean Adafruit_VC0706::takePicture() { + frameptr = 0; + return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME); +} + +// boolean Adafruit_VC0706::resumeVideo() { +// return cameraFrameBuffCtrl(VC0706_RESUMEFRAME); +// } + +// boolean Adafruit_VC0706::TVon() { +// uint8_t args[] = {0x1, 0x1}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } +// boolean Adafruit_VC0706::TVoff() { +// uint8_t args[] = {0x1, 0x0}; +// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5); +// } + +boolean Adafruit_VC0706::cameraFrameBuffCtrl(uint8_t command) { + uint8_t args[] = {0x1, command}; + return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5); +} + +uint32_t Adafruit_VC0706::frameLength(void) { + uint8_t args[] = {0x01, 0x00}; + if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9)) + return 0; + + uint32_t len; + len = camerabuff[5]; + len <<= 8; + len |= camerabuff[6]; + len <<= 8; + len |= camerabuff[7]; + len <<= 8; + len |= camerabuff[8]; + + return len; +} + + +uint8_t Adafruit_VC0706::available(void) { + return bufferLen; +} + + +uint8_t * Adafruit_VC0706::readPicture(uint8_t n) { + uint8_t args[] = {0x0C, 0x0, 0x0A, + 0, 0, frameptr >> 8, frameptr & 0xFF, + 0, 0, 0, n, + CAMERADELAY >> 8, CAMERADELAY & 0xFF}; + + if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false)) + return 0; + + + // read into the buffer PACKETLEN! + if (readResponse(n+5, CAMERADELAY) == 0) + return 0; + + + frameptr += n; + + return camerabuff; +} + +/**************** low level commands */ + + +boolean Adafruit_VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn, + uint8_t resplen, boolean flushflag) { + // flush out anything in the buffer? + if (flushflag) { + // readResponse(100, 10); + // ●●● 変えた!!! ●●● + readResponse(CAMERABUFFSIZ, 10); + } + + sendCommand(cmd, args, argn); + if (readResponse(resplen, 200) != resplen) + return false; + if (! verifyResponse(cmd)) + return false; + return true; +} + +void Adafruit_VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) { +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + if(swSerial) { +#if ARDUINO >= 100 + swSerial->write((byte)0x56); + swSerial->write((byte)serialNum); + swSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + swSerial->print(0x56, BYTE); + swSerial->print(serialNum, BYTE); + swSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } + else +#endif + { +#if ARDUINO >= 100 + hwSerial->write((byte)0x56); + hwSerial->write((byte)serialNum); + hwSerial->write((byte)cmd); + + for (uint8_t i=0; iwrite((byte)args[i]); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#else + hwSerial->print(0x56, BYTE); + hwSerial->print(serialNum, BYTE); + hwSerial->print(cmd, BYTE); + + for (uint8_t i=0; iprint(args[i], BYTE); + //Serial.print(" 0x"); + //Serial.print(args[i], HEX); + } +#endif + } +//Serial.println(); +} + +uint8_t Adafruit_VC0706::readResponse(uint8_t numbytes, uint8_t timeout) { + uint8_t counter = 0; + bufferLen = 0; + int avail; + + while ((timeout != counter) && (bufferLen != numbytes)){ +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + avail = swSerial ? swSerial->available() : hwSerial->available(); +#else + avail = hwSerial->available(); +#endif + if (avail <= 0) { + delay(1); + counter++; + continue; + } + counter = 0; + // there's a byte! +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read(); +#else + camerabuff[bufferLen++] = hwSerial->read(); +#endif + // ●●● 安全コード.足した!!!●●● + if (bufferLen > (CAMERABUFFSIZ - 1)) { + Serial.println(F("CAM BUF WARNING!!!")); + } + } + //printBuff(); +//camerabuff[bufferLen] = 0; +//Serial.println((char*)camerabuff); + return bufferLen; +} + +boolean Adafruit_VC0706::verifyResponse(uint8_t command) { + if ((camerabuff[0] != 0x76) || + (camerabuff[1] != serialNum) || + (camerabuff[2] != command) || + (camerabuff[3] != 0x0)) + return false; + return true; +} + +// void Adafruit_VC0706::printBuff() { +// for (uint8_t i = 0; i< bufferLen; i++) { +// Serial.print(" 0x"); +// Serial.print(camerabuff[i], HEX); +// } +// Serial.println(); +// } diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.h new file mode 100644 index 0000000..8ac62e8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Adafruit_VC0706.h @@ -0,0 +1,132 @@ +/*************************************************** + This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset) + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/397 + + These displays use Serial to communicate, 2 pins are required to interface + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + +#if ARDUINO >= 100 + #include "Arduino.h" + #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #include + #endif +#else + #include "WProgram.h" + #include "NewSoftSerial.h" +#endif + +#define VC0706_RESET 0x26 +// #define VC0706_GEN_VERSION 0x11 +// #define VC0706_SET_PORT 0x24 +#define VC0706_READ_FBUF 0x32 +#define VC0706_GET_FBUF_LEN 0x34 +#define VC0706_FBUF_CTRL 0x36 +// #define VC0706_DOWNSIZE_CTRL 0x54 +// #define VC0706_DOWNSIZE_STATUS 0x55 +// #define VC0706_READ_DATA 0x30 +#define VC0706_WRITE_DATA 0x31 +// #define VC0706_COMM_MOTION_CTRL 0x37 +// #define VC0706_COMM_MOTION_STATUS 0x38 +// #define VC0706_COMM_MOTION_DETECTED 0x39 +// #define VC0706_MOTION_CTRL 0x42 +// #define VC0706_MOTION_STATUS 0x43 +// #define VC0706_TVOUT_CTRL 0x44 +// #define VC0706_OSD_ADD_CHAR 0x45 + +#define VC0706_STOPCURRENTFRAME 0x0 +// #define VC0706_STOPNEXTFRAME 0x1 +// #define VC0706_RESUMEFRAME 0x3 +// #define VC0706_STEPFRAME 0x2 + +#define VC0706_640x480 0x00 +#define VC0706_320x240 0x11 +#define VC0706_160x120 0x22 + +// #define VC0706_MOTIONCONTROL 0x0 +// #define VC0706_UARTMOTION 0x01 +// #define VC0706_ACTIVATEMOTION 0x01 + +// #define VC0706_SET_ZOOM 0x52 +// #define VC0706_GET_ZOOM 0x53 + +// #define CAMERABUFFSIZ 100 +#define CAMERABUFFSIZ 50 // 減らした.多分問題ないが,怖いので安全コードも足した. L.370, L.458あたり +#define CAMERADELAY 10 + + +class Adafruit_VC0706 { + public: +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + Adafruit_VC0706(SoftwareSerial *ser); // Constructor when using SoftwareSerial + #else + Adafruit_VC0706(NewSoftSerial *ser); // Constructor when using NewSoftSerial + #endif +#endif + Adafruit_VC0706(HardwareSerial *ser); // Constructor when using HardwareSerial + boolean begin(uint16_t baud = 38400); + boolean reset(void); + // boolean TVon(void); + // boolean TVoff(void); + boolean takePicture(void); + uint8_t *readPicture(uint8_t n); + // boolean resumeVideo(void); + uint32_t frameLength(void); + // char *getVersion(void); + uint8_t available(); + // uint8_t getDownsize(void); + // boolean setDownsize(uint8_t); + // uint8_t getImageSize(); + boolean setImageSize(uint8_t); + // boolean getMotionDetect(); + // uint8_t getMotionStatus(uint8_t); + // boolean motionDetected(); + // boolean setMotionDetect(boolean f); + // boolean setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2); + boolean cameraFrameBuffCtrl(uint8_t command); + // uint8_t getCompression(); + // boolean setCompression(uint8_t c); + + // boolean getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt); + // boolean setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); + + // void OSD(uint8_t x, uint8_t y, char *s); // isnt supported by the chip :( + +// char* setBaud9600(); +// char* setBaud19200(); +// char* setBaud38400(); +// char* setBaud57600(); +// char* setBaud115200(); + + private: + uint8_t serialNum; + uint8_t camerabuff[CAMERABUFFSIZ+1]; + uint8_t bufferLen; + uint16_t frameptr; + +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #if ARDUINO >= 100 + SoftwareSerial *swSerial; + #else + NewSoftSerial *swSerial; + #endif +#endif + HardwareSerial *hwSerial; + + void common_init(void); + boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp, boolean flushflag = true); + void sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn); + uint8_t readResponse(uint8_t numbytes, uint8_t timeout); + boolean verifyResponse(uint8_t command); + // void printBuff(void); +}; diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.h new file mode 100644 index 0000000..f5b2aee --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.h @@ -0,0 +1,27 @@ +#ifndef CAMERA_H +#define CAMERA_H + +/* +https://www.switch-science.com/catalog/1241/ +https://www.adafruit.com/product/397 +*/ + +// #################### CAMERA #################### + + +#include "./PIN_ASSIGN.h" + +#include "./Adafruit_VC0706.h" +#include +#include +// #include + + +void CAM_Init(); +void CAM_TakePic(); + +typedef struct { +} Camera_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.ino b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.ino new file mode 100644 index 0000000..b255b23 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Camera.ino @@ -0,0 +1,112 @@ +#include "./Camera.h" +#include "./SD.h" +#include "./Servo.h" + + +Camera_t camera; + +// SoftwareSerial と Adafruit_VC0706 は初期値なしの初期化ができないので,仕方なくstructにはいれない. +SoftwareSerial CamSerial = SoftwareSerial(PIN_CAM_TX, PIN_CAM_RX); +Adafruit_VC0706 cam = Adafruit_VC0706(&CamSerial); + + +void CAM_Init() { + // Try to locate the camera + if (cam.begin()) { + Serial.println(F("Cam Found")); + } else { + Serial.println(F("No cam!!")); + return; + } + // // Print out the camera version information (optional) + // char *reply = cam.getVersion(); + // if (reply == 0) { + // Serial.println(F("Failed to get version")); + // } else { + // Serial.println(F("-----------------")); + // Serial.print(reply); + // Serial.println(F("-----------------")); + // } + + cam.setImageSize(VC0706_640x480); // biggest + // cam.setImageSize(VC0706_320x240); // medium + // cam.setImageSize(VC0706_160x120); // small + + + // バッファークリアの意味も兼ねて一発撮る + // 壊れた画像になるので確認. + CAM_TakePic(); +} + + +void CAM_TakePic() { + SD_Write(F("TakePic")); + + // You can read the size back from the camera (optional, but maybe useful?) + // uint8_t imgsize = cam.getImageSize(); + // Serial.print("Image size: "); + // if (imgsize == VC0706_640x480) Serial.println("640x480"); + // if (imgsize == VC0706_320x240) Serial.println("320x240"); + // if (imgsize == VC0706_160x120) Serial.println("160x120"); + + if (! cam.takePicture()) { + Serial.println(F("Snap NG")); + // return; + } else { + Serial.println(F("Snap OK")); + } + + // Create an image with the name IMGxx.JPG + char filename[12]; + strcpy(filename, "000.JPG"); + for (uint16_t i = 0; i < 1000; i++) { + filename[0] = '0' + i/100; + filename[1] = '0' + (i/10)%10; + filename[2] = '0' + i%10; + // create if does not exist, do not open existing, write, sync after write + if ( ! SD.exists(SD_GetDirName() + String(filename)) ) { + break; + } + } + + SD_Write("picname:" + SD_GetDirName() + String(filename)); + Serial.println(SD_GetDirName() + String(filename)); + + // Open the file for writing + File imgFile = SD.open(SD_GetDirName() + String(filename), FILE_WRITE); + + // Get the size of the image (frame) taken + uint16_t jpglen = cam.frameLength(); + Serial.print(F("Storing ")); + Serial.print(jpglen, DEC); + Serial.print(F(" byte.")); + + // サーボのプルプルを防ぐ + SRV_Detach(); + + // int32_t time = millis(); + // pinMode(8, OUTPUT); + // Read all the data up to # bytes! + byte wCount = 0; // For counting # of writes + while (jpglen > 0) { + // read 32 bytes at a time; + uint8_t *buffer; + uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups! + buffer = cam.readPicture(bytesToRead); + imgFile.write(buffer, bytesToRead); + if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up + Serial.print('.'); + wCount = 0; + } + //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes"); + jpglen -= bytesToRead; + } + imgFile.close(); + + SRV_Attach(); + + // time = millis() - time; + Serial.println(F("done!")); + // Serial.print(time); + // Serial.println(F(" ms elapsed")); +} \ No newline at end of file diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.h new file mode 100644 index 0000000..85f26a6 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.h @@ -0,0 +1,67 @@ +#ifndef IMU_H +#define IMU_H + + +/* + +http://akizukidenshi.com/catalog/g/gK-13010/ + +//================================================================// +// AE-BMX055 Arduino UNO // +// VCC +5V // +// GND GND // +// SDA A4(SDA) // +// SCL A5(SCL) // +// // +// (JP6,JP4,JP5はショートした状態) // +// http://akizukidenshi.com/catalog/g/gK-13010/ // +//================================================================// +*/ + +// #################### 9AXIS #################### +#include "./PIN_ASSIGN.h" + + +#include +// BMX055 加速度センサのI2Cアドレス +#define IMU_ADDR_ACCL 0x19 // (JP1,JP2,JP3 = Openの時) +// BMX055 ジャイロセンサのI2Cアドレス +#define IMU_ADDR_GYRO 0x69 // (JP1,JP2,JP3 = Openの時) +// BMX055 磁気センサのI2Cアドレス +#define IMU_ADDR_MAG 0x13 // (JP1,JP2,JP3 = Openの時) + + +void IMU_Init(); +void IMU_UpdateAll(); +void IMU_UpdateAcc(); +void IMU_UpdateGyr(); +void IMU_UpdateMag(); +void IMU_PrintAcc(); +void IMU_PrintGyr(); +void IMU_PrintMag(); + +float IMU_GetAccX(); +float IMU_GetAccY(); +float IMU_GetAccZ(); +float IMU_GetGyrX(); +float IMU_GetGyrY(); +float IMU_GetGyrZ(); +int IMU_GetMagX(); +int IMU_GetMagY(); +int IMU_GetMagZ(); + + +typedef struct { + float xAccl; + float yAccl; + float zAccl; + float xGyro; + float yGyro; + float zGyro; + int xMag ; + int yMag ; + int zMag ; +} Imu_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.ino b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.ino new file mode 100644 index 0000000..90dc9d8 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/IMU.ino @@ -0,0 +1,236 @@ +#include "./IMU.h" + +Imu_t imu; + + + +void IMU_Init() { + imu.xAccl = 0.0; + imu.yAccl = 0.0; + imu.zAccl = 0.0; + imu.xGyro = 0.0; + imu.yGyro = 0.0; + imu.zGyro = 0.0; + imu.xMag = 0; + imu.yMag = 0; + imu.zMag = 0; + + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x0F); // Select PMU_Range register + Wire.write(0x03); // Range = +/- 2g + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x10); // Select PMU_BW register + Wire.write(0x08); // Bandwidth = 7.81 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write(0x11); // Select PMU_LPW register + Wire.write(0x00); // Normal mode, Sleep duration = 0.5ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x0F); // Select Range register + Wire.write(0x04); // Full scale = +/- 125 degree/s + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x10); // Select Bandwidth register + Wire.write(0x07); // ODR = 100 Hz + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write(0x11); // Select LPM1 register + Wire.write(0x00); // Normal mode, Sleep duration = 2ms + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x83); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4B); // Select Mag register + Wire.write(0x01); // Soft reset + Wire.endTransmission(); + delay(100); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4C); // Select Mag register + Wire.write(0x00); // Normal Mode, ODR = 10 Hz + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x4E); // Select Mag register + Wire.write(0x84); // X, Y, Z-Axis enabled + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x51); // Select Mag register + Wire.write(0x04); // No. of Repetitions for X-Y Axis = 9 + Wire.endTransmission(); + //------------------------------------------------------------// + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write(0x52); // Select Mag register + Wire.write(0x16); // No. of Repetitions for Z-Axis = 15 + Wire.endTransmission(); + + Serial.println(F("IMU init done")); +} + + +void IMU_UpdateAll() { + IMU_UpdateAcc(); + IMU_UpdateGyr(); + IMU_UpdateMag(); +} + + +void IMU_UpdateAcc() { + // int data[6]; + uint8_t data[6]; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_ACCL); + Wire.write((2 + i));// Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_ACCL, 1);// Request 1 byte of data + // Read 6 bytes of data + // imu.xAccl lsb, imu.xAccl msb, imu.yAccl lsb, imu.yAccl msb, imu.zAccl lsb, imu.zAccl msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data to 12-bits + imu.xAccl = ((data[1] * 256) + (data[0] & 0xF0)) / 16; + if (imu.xAccl > 2047) imu.xAccl -= 4096; + imu.yAccl = ((data[3] * 256) + (data[2] & 0xF0)) / 16; + if (imu.yAccl > 2047) imu.yAccl -= 4096; + imu.zAccl = ((data[5] * 256) + (data[4] & 0xF0)) / 16; + if (imu.zAccl > 2047) imu.zAccl -= 4096; + imu.xAccl = imu.xAccl * 0.0098; // renge +-2g + imu.yAccl = imu.yAccl * 0.0098; // renge +-2g + imu.zAccl = imu.zAccl * 0.0098; // renge +-2g +} + + +void IMU_UpdateGyr() { + // int data[6]; + uint8_t data[6]; + for (uint8_t i = 0; i < 6; i++) + { + Wire.beginTransmission(IMU_ADDR_GYRO); + Wire.write((2 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_GYRO, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xGyro lsb, imu.xGyro msb, imu.yGyro lsb, imu.yGyro msb, imu.zGyro lsb, imu.zGyro msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xGyro = (data[1] * 256) + data[0]; + if (imu.xGyro > 32767) imu.xGyro -= 65536; + imu.yGyro = (data[3] * 256) + data[2]; + if (imu.yGyro > 32767) imu.yGyro -= 65536; + imu.zGyro = (data[5] * 256) + data[4]; + if (imu.zGyro > 32767) imu.zGyro -= 65536; + + imu.xGyro = imu.xGyro * 0.0038; // Full scale = +/- 125 degree/s + imu.yGyro = imu.yGyro * 0.0038; // Full scale = +/- 125 degree/s + imu.zGyro = imu.zGyro * 0.0038; // Full scale = +/- 125 degree/s +} + + +void IMU_UpdateMag() { + // int data[8]; + uint8_t data[8]; + for (uint8_t i = 0; i < 8; i++) + { + Wire.beginTransmission(IMU_ADDR_MAG); + Wire.write((0x42 + i)); // Select data register + Wire.endTransmission(); + Wire.requestFrom(IMU_ADDR_MAG, 1); // Request 1 byte of data + // Read 6 bytes of data + // imu.xMag lsb, imu.xMag msb, imu.yMag lsb, imu.yMag msb, imu.zMag lsb, imu.zMag msb + if (Wire.available() == 1) + data[i] = Wire.read(); + } + // Convert the data + imu.xMag = ((data[1] <<8) | (data[0]>>3)); + if (imu.xMag > 4095) imu.xMag -= 8192; + imu.yMag = ((data[3] <<8) | (data[2]>>3)); + if (imu.yMag > 4095) imu.yMag -= 8192; + imu.zMag = ((data[5] <<8) | (data[4]>>3)); + if (imu.zMag > 16383) imu.zMag -= 32768; +} + + +void IMU_PrintAcc() { + Serial.print(F("Acc= ")); + Serial.print(imu.xAccl); + Serial.print(F(",")); + Serial.print(imu.yAccl); + Serial.print(F(",")); + Serial.print(imu.zAccl); + Serial.println(F("")); +} + + +void IMU_PrintGyr() { + Serial.print(F("Gyr= ")); + Serial.print(imu.xGyro); + Serial.print(F(",")); + Serial.print(imu.yGyro); + Serial.print(F(",")); + Serial.print(imu.zGyro); + Serial.println(F("")); +} + + +void IMU_PrintMag() { + Serial.print(F("Mag= ")); + Serial.print(imu.xMag); + Serial.print(F(",")); + Serial.print(imu.yMag); + Serial.print(F(",")); + Serial.print(imu.zMag); + Serial.println(F("")); +} + + +float IMU_GetAccX() { + return imu.xAccl; +} +float IMU_GetAccY() { + return imu.yAccl; +} +float IMU_GetAccZ() { + return imu.zAccl; +} +float IMU_GetGyrX() { + return imu.xGyro; +} +float IMU_GetGyrY() { + return imu.yGyro; +} +float IMU_GetGyrZ() { + return imu.zGyro; +} +int IMU_GetMagX() { + return imu.xMag; +} +int IMU_GetMagY() { + return imu.yMag; +} +int IMU_GetMagZ() { + return imu.zMag; +} diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/PIN_ASSIGN.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/PIN_ASSIGN.h new file mode 100644 index 0000000..0f3e877 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/PIN_ASSIGN.h @@ -0,0 +1,28 @@ + +#ifndef PIN_ASSIGN_H +#define PIN_ASSIGN_H + + +#define PIN_SRV 4 + +#define PIN_LIT A0 + +#define PIN_GPS_TX 8 +#define PIN_GPS_RX 9 +#define PIN_SD_SS 10 +// SD MOSI 11 +// SD MISO 12 +// SD CLK 13 + +#define PIN_CAM_TX 2 +#define PIN_CAM_RX 3 + +#define PIN_SW 5 + + +// #define SDA A4 +// #define SCL A5 + + +#endif + diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.h new file mode 100644 index 0000000..ef9e367 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.h @@ -0,0 +1,33 @@ +#ifndef SD_H +#define SD_H + + +#include +#include +#include + + +/* +http://akizukidenshi.com/catalog/g/gK-14015/ +http://akizukidenshi.com/download/ds/akizuki/AE-microSD-LLCNV_sch_20190218_01.pdf +*/ + +// #################### SD #################### +#include "./PIN_ASSIGN.h" + + + + +void SD_Init(); +void SD_Write(String str); +String SD_GetDirName(); + +typedef struct { + String logFileName; + // char logFileName[8]; + char DirName[6]; + // File logFile; +} Sd_t; + + +#endif diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.ino b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.ino new file mode 100644 index 0000000..aa8650b --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/SD.ino @@ -0,0 +1,82 @@ +#include "./SD.h" + +Sd_t sd; + + +// フォルダ分け or 接頭辞 +// #define SD_IS_MKDIR + + +void SD_Init() { + sd.logFileName = "log.txt"; + + pinMode(PIN_SD_SS, OUTPUT); + + if (!SD.begin(PIN_SD_SS)) { + Serial.println(F("SD: init failed!")); + while (1); + } + + + strcpy(sd.DirName, "D000"); + for (uint16_t i = 0; i <= 1000; i++) { + if (i == 1000) { + Serial.println(F("SD: Number of Folder is MAX!")); + while (1); + } + + sd.DirName[1] = '0' + i/100; + sd.DirName[2] = '0' + (i/10)%10; + sd.DirName[3] = '0' + i%10; + if (! SD.exists(SD_GetDirName() + sd.logFileName) ) { + #ifdef SD_IS_MKDIR + SD.mkdir(sd.DirName); // フォルダわけではなく,接頭辞の場合コメントアウト! + #else + #endif + break; + } + } + + + // File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + // if (logFile) { + // // logFile.println("testing 1, 2, 3."); + // logFile.println(F("START UP!!")); + // // logFile.close(); + // Serial.println(F("SD: Write done")); + // } else { + // // if the file didn't open, print an error: + // Serial.println(F("SD: error opening file")); + // } + // logFile.close(); + + SD_Write(F("START UP!!")); + + Serial.println(F("SD init done.")); +} + + +void SD_Write(String str) { + File logFile = SD.open(SD_GetDirName() + sd.logFileName, FILE_WRITE); + + if (logFile) { + logFile.println("[" + String(millis()) + "]\t" + str); + // close the file: + logFile.close(); + Serial.println(F("SD: Write OK")); + } else { + // if the file didn't open, print an error: + Serial.println(F("SD: Open Error")); + } + + logFile.close(); +} + + +String SD_GetDirName() { + #ifdef SD_IS_MKDIR + return (String(sd.DirName) + "/"); + #else + return (String(sd.DirName) + "_"); + #endif +} diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.h b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.h new file mode 100644 index 0000000..979516a --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.h @@ -0,0 +1,29 @@ +#ifndef SRV_H +#define SRV_H + +#include "./PIN_ASSIGN.h" +#include + +/* +http://akizukidenshi.com/catalog/g/gM-08914/ +*/ + +// #################### Servo #################### + + +void SRV_Init(); +void SRV_SetPosition(uint8_t pos); +uint8_t SRV_GetPosition(); +void SRV_Run(); +void SRV_Detach(); +void SRV_Attach(); + +typedef struct { + Servo servo; + uint8_t setPosition; + uint8_t position; +} Servo_t; + + +#endif + diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.ino b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.ino new file mode 100644 index 0000000..51e3db5 --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Servo.ino @@ -0,0 +1,38 @@ +#include "./Servo.h" + + +Servo_t servo; + +void SRV_Init() { + servo.position = 0; + servo.setPosition = 0; + + // servo.servo.attach(PIN_SRV); + SRV_Attach(); + SRV_Run(); + + // Serial.println(F("SRV init done")); + Serial.println(F("SRV init done")); +} + +void SRV_SetPosition(uint8_t pos) { + servo.setPosition = pos; +} + +uint8_t SRV_GetPosition() { + return servo.position; +} + + +void SRV_Run() { + servo.servo.write(servo.setPosition); + servo.position = servo.setPosition; +} + +void SRV_Detach() { + servo.servo.detach(); +} +void SRV_Attach() { + servo.servo.attach(PIN_SRV); + delay(5000); +} \ No newline at end of file diff --git a/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Test.ino b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Test.ino new file mode 100644 index 0000000..d3894fd --- /dev/null +++ b/Archives/2020-02-22_Fukui/07_Kagakugijutsu/Test.ino @@ -0,0 +1,53 @@ +#include "./IMU.h" +#include "./SD.h" +#include "./Servo.h" +#include "./Camera.h" + +void setup() +{ + // Wire(Arduino-I2C)の初期化 + Wire.begin(); + // デバック用シリアル通信は9600bps + Serial.begin(9600); + + //SD_Init(); // これは絶対最初に初期化! + //CAM_Init(); // SDの後! + IMU_Init(); + //SRV_Init(); + + + Serial.println(F("Init done")); + delay(300); +} + +void loop() +{ + + while (1) { + IMU_UpdateAll(); + int mag_x=IMU_GetMagX(); + int mag_y=IMU_GetMagY(); + + Serial.println(mag_x); + Serial.println(mag_y); + + float magnet_radian=atan2((float)mag_y,(float)mag_x); + float magnet_degree=magnet_radian*180.0/PI; + Serial.print(F("Direction: ")); + Serial.print(magnet_degree); + Serial.println(F("[deg]")); + + if (magnet_degree<30&&magnet_degree>-30){ + Serial.println("kita"); + break; + } + + delay(1000); + } + CAM_TakePic(); // 写真を撮る + delay(1000); + + Serial.println(F("END")); + while (1) { + } +} diff --git a/Archives/2020-02-22_Fukui/README.md b/Archives/2020-02-22_Fukui/README.md index 6052fed..dad73f1 100644 --- a/Archives/2020-02-22_Fukui/README.md +++ b/Archives/2020-02-22_Fukui/README.md @@ -1,14 +1,14 @@ # 2020年02月22~23日 缶サットHigh School 「缶サット電子系講座」 ## 各高校のプログラム一覧 -+ [武生東](./01_Takefuhigashi) -+ [敦賀A](./02_TsurugaA) -+ [敦賀B](./03_TsurugaB) -+ [藤島](./04_Fujishima) -+ [高志](./05_Koshi) -+ [三国](./06_Mikuni) -+ [科学技術](./07_Kagakugijutsu) -+ [武生工業](./08_Takefukougyou) -+ [武生](./09_Takefu) -+ [福井工業大学附福井](./10_Fukuikougyoudaigakufukui) -+ [若狭](./11_Wakasa) ++ [01班 武生東](./01_Takefuhigashi) ++ [02班 敦賀A](./02_TsurugaA) ++ [03班 敦賀B](./03_TsurugaB) ++ [04班 藤島](./04_Fujishima) ++ [05班 高志](./05_Koshi) ++ [06班 三国](./06_Mikuni) ++ [07班 科学技術](./07_Kagakugijutsu) ++ [08班 武生工業](./08_Takefukougyou) ++ [09班 武生](./09_Takefu) ++ [10班 福井工業大学附福井](./10_Fukuikougyoudaigakufukui) ++ [11班 若狭](./11_Wakasa)