GR-SAKURA
GR-KURUMI
GR-COTTON
GR-CITRUS
GR-PEACH
GR-KAEDE
GR-ADZUKI
GR-LYCHEE
GR-ROSE
GR-MANGO(*)
SNShield
Web Compiler
IDE for GR
TOPPERS関連
女子美コラボ
その他
※プロデューサミーティング中
作り方使い方資料
イベント関連
作品記事
体験記事
ライブラリ
ツール
その他・過去ファイル
がじぇるね岡宮です。
GR-ROSEにはPMOD用インタフェースがありますが、ジョイスティック「JSTK2」のサンプルを掲載します。
(PMOD JSTK2)https://store.digilentinc.com/pmod-jstk2-two-axis-joystick/
接続は写真の通り、PMODのSPI片側の6ピンをピンtoピンでつないでいるだけです。サンプルを実行するとジョイスティックの傾きと、トリガーボタン2種類の値を表示します。
サンプルとしてはジョイスティックの値だけ取り出す処理をタスクtask_joystickとして、JSTK2という構造体変数joyを定期的にアップデートするだけにしてます。
#include <Arduino.h>#include <SPI.h>#include "FreeRTOS.h"#include "task.h"#define JOY_LED_ON#define JOY_XMIN 150#define JOY_XMAX 850#define JOY_YMIN 140#define JOY_YMAX 890#define JOY_XCEN (JOY_XMAX+JOY_XMIN)/2#define JOY_YCEN (JOY_YMAX+JOY_YMIN)/2struct JSTK2 { int x; int y; bool trigger; bool joystick;};JSTK2 joy;void task_joystick(void *pvParameters) { SPI.begin(); SPISettings settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); SPI.beginTransaction(settings); pinMode(SS, OUTPUT); while (1) { uint8_t buf[7]; digitalWrite(SS, LOW); // activation of SS line delayMicroseconds(15); // wait 15us after activation of SS line for (int i = 0; i < 6; i++) { buf[i] = SPI.transfer(0); // send 6 data to get back data from the module delayMicroseconds(10); // wait 10us after sending each data } digitalWrite(SS, HIGH); // deactivation of SS line joy.x = buf[0]; // X is on 10 bit format joy.x |= (buf[1] << 8); joy.y = buf[2]; // Y is on 10 bit format joy.y |= (buf[3] << 8); joy.trigger = buf[4] & 0x01; joy.joystick = buf[4] & 0x02;#ifdef JOY_LED_ON digitalWrite(SS, LOW); // activation of SS line SPI.transfer(0x84); delayMicroseconds(15); SPI.transfer(map(joy.x, JOY_XMIN, JOY_XMAX, 0, 255));// LED Red delayMicroseconds(15); SPI.transfer(map(joy.y, JOY_YMIN, JOY_YMAX, 0, 255));// LED Green delayMicroseconds(15); SPI.transfer(map((joy.trigger|joy.joystick), 0, 1, 0, 255));// LED Blue delayMicroseconds(15); digitalWrite(SS, HIGH);// deactivation of SS line#endif delay(10); // 100Hz }}void setup() { Serial.begin(9600); xTaskCreate(task_joystick, "JOYSTICK", 512, NULL, 1, NULL);}void loop() { Serial.print("X="); Serial.print(joy.x); Serial.print('\t'); Serial.print("Y="); Serial.print(joy.y); Serial.print('\t'); Serial.print("T="); Serial.print(joy.trigger); Serial.print('\t'); Serial.print("J="); Serial.print(joy.joystick); Serial.println(); delay(50);}
次のサンプルはICSライブラリと組み合わせてサーボを動かすサンプルです。前記のサンプルとほぼ一緒ですが、joy.xとjoy.yをmapしてサーボに設定しているだけです。
#include <Arduino.h>#include <SPI.h>#include "FreeRTOS.h"#include "task.h"#include <ICS.h>#define JOY_LED_ON#define JOY_XMIN 150#define JOY_XMAX 850#define JOY_YMIN 140#define JOY_YMAX 890#define JOY_XCEN (JOY_XMAX+JOY_XMIN)/2#define JOY_YCEN (JOY_YMAX+JOY_YMIN)/2struct JSTK2 { int x; int y; bool trigger; bool joystick;};JSTK2 joy;IcsController ICS(Serial1);IcsServo servo1;IcsServo servo2;void task_joystick(void *pvParameters) { SPI.begin(); SPISettings settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); SPI.beginTransaction(settings); pinMode(SS, OUTPUT); while (1) { uint8_t buf[7]; digitalWrite(SS, LOW); // activation of SS line delayMicroseconds(15); // wait 15us after activation of SS line for (int i = 0; i < 6; i++) { buf[i] = SPI.transfer(0); // send 6 data to get back data from the module delayMicroseconds(10); // wait 10us after sending each data } digitalWrite(SS, HIGH); // deactivation of SS line joy.x = buf[0]; // X is on 10 bit format joy.x |= (buf[1] << 8); joy.y = buf[2]; // Y is on 10 bit format joy.y |= (buf[3] << 8); joy.trigger = buf[4] & 0x01; joy.joystick = buf[4] & 0x02;#ifdef JOY_LED_ON digitalWrite(SS, LOW); // activation of SS line SPI.transfer(0x84); delayMicroseconds(15); SPI.transfer(map(joy.x, JOY_XMIN, JOY_XMAX, 0, 255));// LED Red delayMicroseconds(15); SPI.transfer(map(joy.y, JOY_YMIN, JOY_YMAX, 0, 255));// LED Green delayMicroseconds(15); SPI.transfer(map((joy.trigger|joy.joystick), 0, 1, 0, 255));// LED Blue delayMicroseconds(15); digitalWrite(SS, HIGH);// deactivation of SS line#endif delay(10); // 100Hz }}void setup() { Serial.begin(9600); ICS.begin(); servo1.attach(ICS, 0x02); servo2.attach(ICS, 0x05); servo1.setPosition(7500); servo2.setPosition(7500); delay(1000); xTaskCreate(task_joystick, "JOYSTICK", 512, NULL, 1, NULL);}void loop() { Serial.print("X="); Serial.print(joy.x); Serial.print('\t'); Serial.print("Y="); Serial.print(joy.y); Serial.print('\t'); Serial.print("T="); Serial.print(joy.trigger); Serial.print('\t'); Serial.print("J="); Serial.print(joy.joystick); Serial.println(); servo1.setPosition(map(joy.x, JOY_XMIN, JOY_XMAX, 3500, 11500)); servo2.setPosition(map(joy.y, JOY_YMIN, JOY_YMAX, 3500, 11500)); delay(50);}