Skip to content

Instantly share code, notes, and snippets.

@Tomokatsu-Sakamoto
Created January 24, 2018 11:38
Show Gist options
  • Save Tomokatsu-Sakamoto/a18d0f4b93b8a4099034ad591d0a6ed2 to your computer and use it in GitHub Desktop.
Save Tomokatsu-Sakamoto/a18d0f4b93b8a4099034ad591d0a6ed2 to your computer and use it in GitHub Desktop.
ゲーム機風のコントローラで操作する車
///////////////////////////////////////////////////////////////////////
// ESP-WROOM-02 のチップIDを用いて SSID を生成する
void getSSID(
char *buff ) //
{
sprintf( buff, "ESP_%6X", ESP.getChipId( ) );
}
// ESP8266 Car ② 
// ライブラリの読込
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUDP.h>
#include <ST7032.h>
#define BUFF_SIZE 16
char lineBuff[ BUFF_SIZE ]; // LCDの1行分バッファ
// UDP setting
WiFiUDP UDP;
// WiFi setting
IPAddress myIP(192, 168, 5, 1);
ST7032 lcd;
const int LM1Pin = 16; // TA7291P 左モータ IN1
const int LM2Pin = 14; // TA7291P 左モータ IN2
const int RM1Pin = 12; // TA7291P 右モータ IN1
const int RM2Pin = 13; // TA7291P 右モータ IN2
const int LedPin = 15; // LEDピン
const int SwPin = 2; // SWピン
const int LFMax = 800; //左モーター正転補正値
const int RFMax = 800; //右モーター正転補正値
const int LRMax = 800; //左モーター逆転補正値
const int RRMax = 800; //右モーター逆転補正値
///////////////////////////////////////////////////////////////////////
// モータの状況表示
void motorLCD(
int Lp, // 左モーター(-100~0~100)
int Rp ) // 右モーター(-100~0~100)
{
lcd.setCursor( 0, 0 );
sprintf( lineBuff, "%c%3d%c%3d",
( Lp < 0 ) ? 'B' : 'F', abs( Lp ),
( Rp < 0 ) ? 'B' : 'F', abs( Rp ) );
lcd.print( lineBuff );
}
///////////////////////////////////////////////////////////////////////
// ツインモーター制御処理
void TMcont(
int Lp, // 左モーター(-100~0~100)
int Rp ) // 右モーター(-100~0~100)
{
motorLCD( Lp, Rp );
if ( Lp >= 0) {
analogWrite( LM1Pin, map( Lp, 0, 100, 0, LFMax ) );
analogWrite( LM2Pin, 0 );
}
else {
analogWrite( LM1Pin, 0 );
analogWrite( LM2Pin, map( abs( Lp ), 0, 100, 0, LRMax ) );
}
if ( Rp >= 0 ) {
analogWrite( RM1Pin, map( Rp, 0, 100, 0, RFMax ) );
analogWrite( RM2Pin, 0 );
}
else {
analogWrite( RM1Pin, 0 );
analogWrite( RM2Pin, map( abs( Rp ), 0, 100, 0, RRMax ) );
}
}
///////////////////////////////////////////////////////////////////////
// UDP 受信処理
void rcvWiFi( char *cBuff, int iSize )
{
static unsigned char cCount = 0;
char wkBuff[ BUFF_SIZE ];
cCount++;
if ( iSize > 8 ) {
iSize = 8;
}
UDP.read( cBuff, iSize );
cBuff[ iSize ] = '\0'; // 表示用に '\0' を付加
Serial.println( cBuff );
UDP.flush( );
lcd.setCursor( 0, 1 );
sprintf( wkBuff, "%02X:%c%c%c",
cCount, cBuff[ 0 ], cBuff[ 1 ], cBuff[ 2 ] );
lcd.print( wkBuff );
}
///////////////////////////////////////////////////////////////////////
// 初期処理 setup
void setup( )
{
ESP.eraseConfig( );
delay( 500 );
///////////////////////////////////////////////////////////////////////
// 入出力ピンの設定
delay( 1000 );
pinMode( LedPin, OUTPUT ); // LedPin をデジタル出力モードに設定
pinMode( SwPin, INPUT ); // SwPin をデジタル入力モードに設定
///////////////////////////////////////////////////////////////////////
// シリアルポートの設定
Serial.begin( 115200 );
Serial.println( "" );
///////////////////////////////////////////////////////////////////////
// LCD の初期化
lcd.begin( 8, 2 );
lcd.setContrast( 30 ); // コントラスト設定
lcd.blink( );
///////////////////////////////////////////////////////////////////////
// Wi-Fi の初期化
WiFi.mode( WIFI_AP ); // Wi-Fi のアクセスポイントとして動作
getSSID( lineBuff );
WiFi.softAP( lineBuff );
WiFi.config( myIP, WiFi.gatewayIP( ), WiFi.subnetMask( ) );
UDP.begin( 893 ); //
Serial.println( "" );
WiFi.printDiag( Serial );
Serial.println( "UDP.begin!" );
Serial.println( "" );
digitalWrite( LedPin, 0 ); // Ledを消灯
TMcont( 0, 0 ); // 停止状態からはじめる
}
void loop( )
{
int iSize, x, y, l, r;
if ( ( iSize = UDP.parsePacket( ) ) > 0 ) {
Serial.printf( "Receive : %d ", iSize );
rcvWiFi( lineBuff, iSize );
x = lineBuff[ 0 ] - 'A';
y = lineBuff[ 1 ] - 'A';
// Serial.printf( "ch0:%d, ch1:%d ", x, y );
if ( x >= 8 ) {
x = x - 8;
}
else {
x = x - 7;
}
x = ( x * 100 ) / 7 ;
if ( y >= 8 ) {
y = y - 8;
}
else {
y = y - 7;
}
y = ( y * 100 ) / 7 ;
// Serial.printf( "x:%d, y:%d ", x, y );
l = -y;
r = -y;
if ( x < 0 ) {
r = ( r * ( 100 + x ) ) / 100;
}
else {
l = ( l * ( 100 - x ) ) / 100;
}
Serial.printf( "L:%d, R:%d ", l, r );
if ( lineBuff[ 2 ] == '3' ) {
digitalWrite( LedPin, 0 );
}
else {
digitalWrite( LedPin, 1 );
}
TMcont( l, r );
}
delay( 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment