etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
color_check.cpp
[詳解]
1 
9 #include "ev3api.h"
10 
11 #include "Controller.h"
12 #include "Distinguisher.h"
13 #include "app.h"
14 
15 #if defined(BUILD_MODULE)
16 #include "module_cfg.h"
17 #else
18 #include "kernel_cfg.h"
19 #endif
20 
21 #define DEBUG
22 
23 #ifdef DEBUG
24 #define _debug(x) (x)
25 #else
26 #define _debug(x)
27 #endif
28 
29 static int g_bluetooth_command = 0; // Bluetoothコマンド 1:リモートスタート
30 static FILE* g_bluetooth = NULL; // Bluetoothファイルハンドル
31 
32 /* メインタスク */
33 void main_task(intptr_t unused)
34 {
35  /* Open Bluetooth file */
36  g_bluetooth = ev3_serial_open_file(EV3_SERIAL_BT);
37  assert(g_bluetooth != NULL);
38 
39  /* Bluetooth通信タスクの起動 */
40  act_tsk(BT_TASK);
41 
42  Controller controller;
43  Distinguisher distinguisher{ controller };
44  const char* color_name[7] = { "NONE", "BLACK", "WHITE", "RED", "BLUE", "YELLOW", "GREEN" };
45  controller.printDisplay(1, "ET-Robocon2018");
46  controller.printDisplay(2, " create from github.com/korosuke613/etrobocon2018");
47  rgb_raw_t rgb;
48 
49  while(1) {
50  if(controller.buttonIsPressedBack()) {
51  break;
52  }
53  // controller.getRawColor(rgb.r, rgb.g, rgb.b);
54  // controller.printDisplay(4, "RGB: %d, %d, %d", rgb.r, rgb.g, rgb.b);
55  auto result = distinguisher.getColor();
56  controller.printDisplay(4, "HSV: %3d, %3d, %3d", static_cast<int16_t>(distinguisher.hsv.h),
57  static_cast<int16_t>(distinguisher.hsv.s),
58  static_cast<int16_t>(distinguisher.hsv.v));
59  // controller.printDisplay(5, "Color Distance: %lf", distinguisher.last_distance);
60  controller.printDisplay(6, "Color Number: %d", result);
61  controller.printDisplay(7, "Color Name: %s", color_name[static_cast<int>(result)]);
62  controller.printDisplay(8, "%d", controller.getBrightness());
63 
64  controller.tslpTsk(4);
65  }
66 
67  ter_tsk(BT_TASK);
68  fclose(g_bluetooth);
69 
70  ext_tsk();
71 }
72 
80 void bt_task(intptr_t unused)
81 {
82  while(1) {
83  uint8_t c = fgetc(g_bluetooth); /* 受信 */
84  switch(c) {
85  case '1':
86  g_bluetooth_command = 1;
87  break;
88  default:
89  break;
90  }
91  fputc(c, g_bluetooth); /* エコーバック */
92  }
93 }
94 
95 void sensor_log_task(intptr_t unused)
96 {
97  FILE* file;
98  Controller controller;
99  int volt = 0;
100  int amp = 0;
101  int time_now = 0; // 開始時間からの経過時間を取得
102  int32_t left_motor_counts = 0; //左モータのオフセット付き角位置取得
103  int32_t right_motor_counts = 0; //右モータのオフセット付き角位置取得
104  int log_file_number = 0;
105  char log_file_name[16];
106  bool flag = true;
107  while(flag == true) {
108  sprintf(log_file_name, "%s%d%s", "/Log/log", log_file_number, ".csv");
109  controller.printDisplay(3, log_file_name);
110  file = fopen(log_file_name, "r");
111  if(file == NULL) { // ファイル名がダブらない場合
112  fclose(file);
113  file = fopen(log_file_name, "a");
114  fprintf(file, "Time(msec), Voltage, Ampere, leftMotorCounts, rightMotorCounts\n");
115  flag = false;
116 
117  } else { // 同じlogファイル名が存在する場合
118  log_file_number++;
119  }
120  }
121  ev3_speaker_play_tone(NOTE_FS6, 200);
122  while(1) {
123  if(ev3_button_is_pressed(BACK_BUTTON)) { // 戻るボタンを押すとlog取得終了
124  ev3_speaker_play_tone(NOTE_FS6, 500); // 終了音
125  fclose(file);
126  unl_mtx(LOG); //処理の終了
127  break;
128  }
129  time_now = controller.clock.now();
130  volt = ev3_battery_voltage_mV();
131  amp = ev3_battery_current_mA();
132  left_motor_counts = controller.leftWheel.getCount();
133  right_motor_counts = controller.rightWheel.getCount();
134  fprintf(file, "%d,%d,%d,%ld,%ld\n", time_now, volt, amp, left_motor_counts, right_motor_counts);
135  // 20msec周期起動
136  tslp_tsk(20);
137  }
138 }
void tslpTsk(int16_t time)
Definition: Controller.cpp:74
Clock clock
Definition: Controller.h:48
bool buttonIsPressedBack()
Definition: Controller.cpp:24
int16_t getBrightness()
Definition: Controller.cpp:59
void sensor_log_task(intptr_t unused)
Definition: color_check.cpp:95
走行体のカラーセンサを用いて、RGB情報より色を推定するクラス。
void bt_task(intptr_t unused)
Definition: color_check.cpp:80
Motor leftWheel
Definition: Controller.h:46
void main_task(intptr_t unused)
Definition: color_check.cpp:33
走行体のカラーセンサを用いて、RGB情報より色を推定するクラス。
Definition: Distinguisher.h:55
uint32_t now(void) const
Definition: Clock.cpp:26
void printDisplay(int8_t row, const char *format,...)
Definition: Controller.cpp:79
int32_t getCount(void) const
Definition: Motor.h:67
Motor rightWheel
Definition: Controller.h:45