BugC와 JoyC에서 놀았습니다.
BugC란?
BugC는 M5StickC를 연결하여 사용하는 로봇 기반입니다. 이용에는 별도 M5StickC가 필요합니다.
본 제품에는 M5StickC는 포함되어 있지 않으므로 주의해 주십시오.
BugC는 4개의 DC 모터, 모터 드라이버, 2개의 RGB LED, 배터리, 배터리 홀더, 전원 스위치로 구성되어 있습니다.
스위치 과학에서 살 수 있습니다.
htps //w w. 슈 tch-s 시엔세. 코 m/타타 g/6208/
JoyC란?
JoyC는 M5StickC용으로 설계된 컨트롤러 모듈입니다. 이용에는 별도 M5StickC가 필요합니다.
본 제품에는 M5StickC는 포함되어 있지 않으므로 주의해 주십시오.
M5StickC는 I2C로 연결하여 M5StickC에서 데이터를 전송할 수 있습니다.
스위치 과학에서 살 수 있습니다.
htps //w w. 슈 tch-s 시엔세. 코 m/카타g/6207/
시도한 것
JoyC는 M5StickC용으로 설계된 컨트롤러 모듈입니다. 이용에는 별도 M5StickC가 필요합니다.
본 제품에는 M5StickC는 포함되어 있지 않으므로 주의해 주십시오.
M5StickC는 I2C로 연결하여 M5StickC에서 데이터를 전송할 수 있습니다.
스위치 과학에서 살 수 있습니다.
htps //w w. 슈 tch-s 시엔세. 코 m/카타g/6207/
시도한 것
조작방법
달려 보았다.
똑바로 달리지 않았다,,,.
축 마다 계수라든지 주어 보았는데 어떻게 되지 않아. 접지면이 너무 작기 때문에? 모터의 개체 차이?
기어 라든지 바퀴라든지 붙여 봐도 좋을지도.
소스 코드
Arduino IDE에서 개발
BugC 측
BugC.ino#include "M5StickC.h"
#include "bugC.h"
#include "BluetoothSerial.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_err.h"
int rotate = 0;
BluetoothSerial SerialBT;
void setup()
{
M5.begin();
Wire.begin(0, 26, 400000);
M5.Lcd.setTextColor(TFT_GREEN);
M5.Lcd.setRotation(1);
M5.Lcd.drawCentreString("BUGC SPP", 80, 30, 2);
// if add battery, need increase charge current
M5.Axp.SetChargeCurrent(CURRENT_360MA);
// シリアル接続
Serial.begin(115200);
SerialBT.begin("ESP32BugC"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop()
{
M5.update();
if (SerialBT.available()) {
String rcvData = SerialBT.readStringUntil(';');
Serial.print(rcvData);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(1, 0, 1);
M5.Lcd.print("string: " + rcvData);
int len = rcvData.length();
M5.Lcd.setCursor(1, 10, 1);
M5.Lcd.printf("length: %d", len);
int idx = rcvData.indexOf(',');
M5.Lcd.setCursor(1, 20, 1);
M5.Lcd.printf("idx: %d", idx);
if (idx != -1)
{
String firstStr = rcvData.substring(0, idx);
M5.Lcd.setCursor(1, 30, 1);
M5.Lcd.print(firstStr);
String secondStr = rcvData.substring(idx+1, rcvData.length());
M5.Lcd.setCursor(1, 40, 1);
M5.Lcd.print(secondStr);
int firstSpeed = firstStr.toInt();
int secondSpeed = secondStr.toInt();
BugCSetColor(0x100000, 0x001000);
BugCSetAllSpeed(firstSpeed, -secondSpeed, firstSpeed, -secondSpeed);
}
}
if(M5.BtnB.wasPressed())
{
BugCSetColor(0x000000, 0x000000);
BugCSetAllSpeed(0, 0, 0, 0);
}
if(M5.BtnA.wasPressed())
{
initBluetooth();
}
delay(10);
}
bool initBluetooth()
{
if(!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if(esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if(esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
Serial.println("Success to initialize controller");
return true;
}
JoyC 측
JoyC.ino#ifndef _M5_STICK_C_
#define _M5_STICK_C_
#include <M5StickC.h>
#endif
#include <BluetoothSerial.h>
#include "JoyC.h"
JoyC joyc;
TFT_eSprite img = TFT_eSprite(&M5.Lcd);
BluetoothSerial SerialBT;
String MACadd = "XX:XX:XX:XX:XX:XX";
uint8_t address[6] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX};
String name = "OBDII";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
void setup() {
M5.begin();
Wire.begin(0, 26, 400000);
img.createSprite(80, 160);
Serial.begin(115200);
SerialBT.begin("ESP32test", true);
Serial.println("The device started in master mode, make sure remote BT device is on!");
M5.Lcd.printf("The device started in master mode, make sure remote BT device is on!");
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
//connected = SerialBT.connect(name);
connected = SerialBT.connect(address);
if(connected) {
Serial.println("Connected Succesfully!");
M5.Lcd.printf("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
M5.Lcd.printf("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
connected = SerialBT.connect();
if(connected) {
Serial.println("Connected Succesfully!");
M5.Lcd.printf("Connected Succesfully!");
}
else
{
Serial.println("Failed to connect.");
M5.Lcd.printf("Failed to connect.");
}
}
void loop() {
char text_buff[100];
M5.update();
if (!connected)
{
delay(100);
return;
}
img.fillSprite(TFT_BLACK);
img.drawCentreString("angle left", 40, 6, 1);
sprintf(text_buff, "%d", joyc.GetAngle(0));
img.drawCentreString(text_buff, 40, 20, 1);
img.drawCentreString("dis left", 40, 34, 1);
sprintf(text_buff, "%d", joyc.GetDistance(0));
img.drawCentreString(text_buff, 40, 48, 1);
img.drawCentreString("angle right", 40, 62, 1);
sprintf(text_buff, "%d", joyc.GetAngle(1));
img.drawCentreString(text_buff, 40, 76, 1);
img.drawCentreString("dis right", 40, 90, 1);
sprintf(text_buff, "%d", joyc.GetDistance(1));
img.drawCentreString(text_buff, 40, 104, 1);
int leftAngle = joyc.GetAngle(0);
int leftSpeed = joyc.GetDistance(0);
int rightAngle = joyc.GetAngle(1);
int rightSpeed = joyc.GetDistance(1);
if (leftAngle >= 90 && leftAngle < 270)
{
leftSpeed = leftSpeed * -1;
}
if (rightAngle >= 90 && rightAngle < 270)
{
rightSpeed = rightSpeed * -1;
}
String sendStr = "";
sendStr = String(leftSpeed) + "," + String(rightSpeed) + ";";
img.drawCentreString("String", 40, 118, 1);
img.drawCentreString(sendStr, 40, 132, 1);
int len = sendStr.length();
img.drawCentreString(String(len), 40, 146, 1);
img.pushSprite(0, 0);
byte byteBuff[len + 1];
sendStr.getBytes(byteBuff, len + 1);
SerialBT.write(byteBuff, len);
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(100);
}
「MACadd」에는 BugC에 연결한 M5StickC의 MAC 주소를 지정.
Reference
이 문제에 관하여(BugC와 JoyC에서 놀았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/moritakuya/items/6ac95099812f3b46f4ba
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Arduino IDE에서 개발
BugC 측
BugC.ino
#include "M5StickC.h"
#include "bugC.h"
#include "BluetoothSerial.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_err.h"
int rotate = 0;
BluetoothSerial SerialBT;
void setup()
{
M5.begin();
Wire.begin(0, 26, 400000);
M5.Lcd.setTextColor(TFT_GREEN);
M5.Lcd.setRotation(1);
M5.Lcd.drawCentreString("BUGC SPP", 80, 30, 2);
// if add battery, need increase charge current
M5.Axp.SetChargeCurrent(CURRENT_360MA);
// シリアル接続
Serial.begin(115200);
SerialBT.begin("ESP32BugC"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop()
{
M5.update();
if (SerialBT.available()) {
String rcvData = SerialBT.readStringUntil(';');
Serial.print(rcvData);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(1, 0, 1);
M5.Lcd.print("string: " + rcvData);
int len = rcvData.length();
M5.Lcd.setCursor(1, 10, 1);
M5.Lcd.printf("length: %d", len);
int idx = rcvData.indexOf(',');
M5.Lcd.setCursor(1, 20, 1);
M5.Lcd.printf("idx: %d", idx);
if (idx != -1)
{
String firstStr = rcvData.substring(0, idx);
M5.Lcd.setCursor(1, 30, 1);
M5.Lcd.print(firstStr);
String secondStr = rcvData.substring(idx+1, rcvData.length());
M5.Lcd.setCursor(1, 40, 1);
M5.Lcd.print(secondStr);
int firstSpeed = firstStr.toInt();
int secondSpeed = secondStr.toInt();
BugCSetColor(0x100000, 0x001000);
BugCSetAllSpeed(firstSpeed, -secondSpeed, firstSpeed, -secondSpeed);
}
}
if(M5.BtnB.wasPressed())
{
BugCSetColor(0x000000, 0x000000);
BugCSetAllSpeed(0, 0, 0, 0);
}
if(M5.BtnA.wasPressed())
{
initBluetooth();
}
delay(10);
}
bool initBluetooth()
{
if(!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if(esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if(esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
Serial.println("Success to initialize controller");
return true;
}
JoyC 측
JoyC.ino
#ifndef _M5_STICK_C_
#define _M5_STICK_C_
#include <M5StickC.h>
#endif
#include <BluetoothSerial.h>
#include "JoyC.h"
JoyC joyc;
TFT_eSprite img = TFT_eSprite(&M5.Lcd);
BluetoothSerial SerialBT;
String MACadd = "XX:XX:XX:XX:XX:XX";
uint8_t address[6] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX};
String name = "OBDII";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
void setup() {
M5.begin();
Wire.begin(0, 26, 400000);
img.createSprite(80, 160);
Serial.begin(115200);
SerialBT.begin("ESP32test", true);
Serial.println("The device started in master mode, make sure remote BT device is on!");
M5.Lcd.printf("The device started in master mode, make sure remote BT device is on!");
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
//connected = SerialBT.connect(name);
connected = SerialBT.connect(address);
if(connected) {
Serial.println("Connected Succesfully!");
M5.Lcd.printf("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
M5.Lcd.printf("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
connected = SerialBT.connect();
if(connected) {
Serial.println("Connected Succesfully!");
M5.Lcd.printf("Connected Succesfully!");
}
else
{
Serial.println("Failed to connect.");
M5.Lcd.printf("Failed to connect.");
}
}
void loop() {
char text_buff[100];
M5.update();
if (!connected)
{
delay(100);
return;
}
img.fillSprite(TFT_BLACK);
img.drawCentreString("angle left", 40, 6, 1);
sprintf(text_buff, "%d", joyc.GetAngle(0));
img.drawCentreString(text_buff, 40, 20, 1);
img.drawCentreString("dis left", 40, 34, 1);
sprintf(text_buff, "%d", joyc.GetDistance(0));
img.drawCentreString(text_buff, 40, 48, 1);
img.drawCentreString("angle right", 40, 62, 1);
sprintf(text_buff, "%d", joyc.GetAngle(1));
img.drawCentreString(text_buff, 40, 76, 1);
img.drawCentreString("dis right", 40, 90, 1);
sprintf(text_buff, "%d", joyc.GetDistance(1));
img.drawCentreString(text_buff, 40, 104, 1);
int leftAngle = joyc.GetAngle(0);
int leftSpeed = joyc.GetDistance(0);
int rightAngle = joyc.GetAngle(1);
int rightSpeed = joyc.GetDistance(1);
if (leftAngle >= 90 && leftAngle < 270)
{
leftSpeed = leftSpeed * -1;
}
if (rightAngle >= 90 && rightAngle < 270)
{
rightSpeed = rightSpeed * -1;
}
String sendStr = "";
sendStr = String(leftSpeed) + "," + String(rightSpeed) + ";";
img.drawCentreString("String", 40, 118, 1);
img.drawCentreString(sendStr, 40, 132, 1);
int len = sendStr.length();
img.drawCentreString(String(len), 40, 146, 1);
img.pushSprite(0, 0);
byte byteBuff[len + 1];
sendStr.getBytes(byteBuff, len + 1);
SerialBT.write(byteBuff, len);
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(100);
}
「MACadd」에는 BugC에 연결한 M5StickC의 MAC 주소를 지정.
Reference
이 문제에 관하여(BugC와 JoyC에서 놀았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/moritakuya/items/6ac95099812f3b46f4ba텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)