0.はじめに
本記事は、spike-rtでカラーセンサーから、16進法カラーコードと、大まかな色を取得するプログラムについての記事です。
spike-rtでのプロジェクトビルド方法、環境構築については以下の記事をご覧ください。
1.16進法カラーコード
コード
まず、コードを出します。
#include <stdlib.h>
#include <stdio.h>
#include <kernel.h>
#include <spike/hub/system.h>
#include <color_code.h>
#include "spike/pup/motor.h"
#include "spike/pup/colorsensor.h"
#include "spike/pup/forcesensor.h"
#include "spike/pup/ultrasonicsensor.h"
#include "spike/hub/battery.h"
#include "spike/hub/button.h"
#include "spike/hub/display.h"
#include "spike/hub/imu.h"
#include "spike/hub/light.h"
#include "spike/hub/speaker.h"
#include <pbio/color.h>
#include "kernel_cfg.h"
#include "syssvc/serial.h"
pup_device_t *colorA;
void Main(intptr_t exinf)
{
// ここからプログラムを書く
colorA = pup_color_sensor_get_device('A');
while(1){
pup_color_rgb_t rgb = pup_color_sensor_rgb(colorA);
int R = (rgb.r * 255) / 900;
int G = (rgb.g * 255) / 900;
int B = (rgb.b * 255) / 900;
char hex[16];
sprintf(hex, "#%02X%02X%02X", R, G, B);
int color_code = hex
hub_display_text_scroll(hex, 100);
dly_tsk(300 * 1000);
}
exit(0);
}
説明
このプログラムは、SPIKEプライムのポートAに接続したカラーセンサーを使って色を測定し、その色を16進カラーコード(#RRGGBB)として表示するものです。
最初に、pup_color_sensor_get_device('A') を使ってポートAのカラーセンサーを取得します。
次に、無限ループの中で pup_color_sensor_rgb(colorA) を実行してRGBの生値を取得します。RGBの値はそれぞれ0〜900程度の範囲で返されます。
取得した rgb.r、rgb.g、rgb.b を Webカラーコードで使われる0〜255の範囲に変換するために、(値 * 255) / 900 の計算を行います。これにより R、G、B の3つの整数値が得られます。
次に、sprintf(hex, "#%02X%02X%02X", R, G, B) を使って、R・G・B を16進数2桁ずつに変換し、#RRGGBB の形式の文字列を作成します。%02X は2桁の16進数を意味します。
作成した16進カラーコードの文字列を hub_display_text_scroll(hex, 100) でハブの画面にスクロール表示します。
dly_tsk(300 * 1000) により0.3秒待機し、再びループの先頭に戻って次の色を測定します。
この処理を繰り返すことで、カラーセンサーが読み取った色をリアルタイムで16進カラーコードとして確認できます。
2.大まかな色(15種)
コード
おなじようにまずコードを出します。
#include <stdlib.h>
#include <stdio.h>
#include <kernel.h>
#include <spike/hub/system.h>
#include <color_display.h>
#include "spike/pup/motor.h"
#include "spike/pup/colorsensor.h"
#include "spike/pup/forcesensor.h"
#include "spike/pup/ultrasonicsensor.h"
#include "spike/hub/battery.h"
#include "spike/hub/button.h"
#include "spike/hub/display.h"
#include "spike/hub/imu.h"
#include "spike/hub/light.h"
#include "spike/hub/speaker.h"
#include <pbio/color.h>
#include "kernel_cfg.h"
#include "syssvc/serial.h"
pup_device_t *colorA;
void Main(intptr_t exinf)
{
colorA = pup_color_sensor_get_device('A');
while(1){
pup_color_hsv_t hsv = pup_color_sensor_hsv(colorA, true);
int h = hsv.h;
int s = hsv.s;
int v = hsv.v;
const char *color = "Unknown";
const char *head = "?";
if(v < 40) {
color = "Black";
head = "Bk";
}
else if(s < 20 && v > 200) {
color = "White";
head = "W";
}
else if(s < 20) {
color = "Gray";
head = "Gy";
}
else if(h < 10 || h >= 350) {
color = "Red";
head = "R";
}
else if(h < 25) {
color = "Red-Orange";
head = "RO";
}
else if(h < 40) {
color = "Orange";
head = "O";
}
else if(h < 55) {
color = "Yellow-Orange";
head = "YO";
}
else if(h < 70) {
color = "Yellow";
head = "Y";
}
else if(h < 85) {
color = "Yellow-Green";
head = "YG";
}
else if(h < 110) {
color = "Green";
head = "G";
}
else if(h < 140) {
color = "Blue-Green";
head = "BG";
}
else if(h < 170) {
color = "Cyan";
head = "C";
}
else if(h < 230) {
color = "Blue";
head = "B";
}
else if(h < 290) {
color = "Violet";
head = "V";
}
else if(h < 320) {
color = "Magenta";
head = "M";
}
else if(h < 350) {
color = "Pink";
head = "P";
}
hub_display_text_scroll(head, 100);
dly_tsk(300 * 1000);
}
exit(0);
}
説明
カラーセンサーから取得した HSV の値を使って色を判定します。Hue は色相、Saturation は彩度、Value は明るさを表します。黒・白・灰色は Hue では判定できないため、彩度と明るさを使って先に判定します。Value が低いと黒になり、Saturation が低く Value が高いと白になり、Saturation が低く Value が中くらいだと灰色になります。
黒・白・灰色でなかった場合は Hue の値を使って色を分類します。Hue は 0〜360° の円環になっており、範囲ごとに色を割り当てています。判定された英語名は変数 color に入れ、表示用の短縮形は head に入れます。頭文字が重複する色は、Red-Orange を RO、Yellow-Green を YG、Blue-Green を BG のように2文字で区別しています。
最後に、短縮形をハブの画面にスクロール表示します。少し待機してから再び測定を行い、リアルタイムで色を判定し続けます。
対応表
| 判定条件 | 英語名 | 表示短縮形 |
|---|---|---|
| v < 40 | Black | Bk |
| s < 20, v > 200 | White | W |
| s < 20 | Gray | Gy |
| h < 10 または h ≥ 350 | Red | R |
| h < 25 | Red-Orange | RO |
| h < 40 | Orange | O |
| h < 55 | Yellow-Orange | YO |
| h < 70 | Yellow | Y |
| h < 85 | Yellow-Green | YG |
| h < 110 | Green | G |
| h < 140 | Blue-Green | BG |
| h < 170 | Cyan | C |
| h < 230 | Blue | B |
| h < 290 | Violet | V |
| h < 320 | Magenta | M |
| h < 350 | Pink | P |
3.おわりに
今回はカラーセンサーの使い方についてのコードを紹介しました。
を参考にしました!koushiro様ありがとうございます!