Skip to content

Instantly share code, notes, and snippets.

@binderclip
Last active April 4, 2016 09:01
Show Gist options
  • Save binderclip/c6105c511215bf60729f to your computer and use it in GitHub Desktop.
Save binderclip/c6105c511215bf60729f to your computer and use it in GitHub Desktop.
Arduino WiFi 小车
int led = 13;
int m1a = 5;
int m1b = 6;
int m2a = 10;
int m2b = 11;
int s1q = 220;
int s1s = 150;
int s2q = 220;
int s2s = 120;
int STAY = 400;
int stay_ms = 0;
int inByte = 0;
void setup() {
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
blinkcode();
blinkcode();
Serial.print("AT+CIPMUX=1\r\n");
delay(800);
Serial.print("AT+CIPSERVER=1,1313\r\n");
blinkcode();
}
void loop() {
delay(1);
if (stay_ms > 0) {
stay_ms--;
}
else {
stand_up();
}
if (Serial.available() > 0) {
inByte = Serial.read();
switch(inByte) {
case '@': stand_up(); stay_ms = STAY; break;
case '<': left_forward(); stay_ms = STAY; break;
case '^': forward(); stay_ms = STAY; break;
case '>': right_forward(); stay_ms = STAY; break;
case '/': left_back(); stay_ms = STAY; break;
case '_': back(); stay_ms = STAY; break;
case '\\': right_back(); stay_ms = STAY; break;
default : break;
}
}
}
void stand_up() {
analogWrite(m1a, 0);
analogWrite(m1b, 0);
analogWrite(m2a, 0);
analogWrite(m2b, 0);
}
void forward() {
analogWrite(m1a, 0);
analogWrite(m1b, s1q);
analogWrite(m2a, 0);
analogWrite(m2b, s2q);
}
void left_forward() {
analogWrite(m1a, 0);
analogWrite(m1b, s1q);
analogWrite(m2a, 0);
analogWrite(m2b, s2s);
}
void right_forward() {
analogWrite(m1a, 0);
analogWrite(m1b, s1s);
analogWrite(m2a, 0);
analogWrite(m2b, s2q);
}
void back() {
analogWrite(m1a, s1q);
analogWrite(m1b, 0);
analogWrite(m2a, s2q);
analogWrite(m2b, 0);
}
void left_back() {
analogWrite(m1a, s1q);
analogWrite(m1b, 0);
analogWrite(m2a, s2s);
analogWrite(m2b, 0);
}
void right_back() {
analogWrite(m1a, s1s);
analogWrite(m1b, 0);
analogWrite(m2a, s2q);
analogWrite(m2b, 0);
}
void blinkcode() {
//Simply loop a few times flashing the status light (this is used during boot up)
int i;
for (i = 1; i <= 10; i++){
delay (100);
digitalWrite(led,HIGH);
delay (100);
digitalWrite(led,LOW);
}
}
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
import socket
# TCP_IP = '127.0.0.1'
# TCP_PORT = 5005
TCP_IP = '192.168.1.63'
TCP_PORT = 1313
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print 'Yeeeeeeeah!!! Connected!'
print """
Control with your keyboard!
----------------------------------------
left front front right front
[q] [w] [e]
STOP [s]
[z] [x] [c]
left back back right back
----------------------------------------
[#] to quit
"""
def transend(c):
b = ''
if c == 'q':
b = '<'
elif c == 'w':
b = '^'
elif c == 'e':
b = '>'
elif c == 's':
b = '@'
elif c == 'z':
b = '/'
elif c == 'x':
b = '_'
elif c == 'c':
b = '\\'
else:
b = c
return b
while True:
c = getch()
if c == '#':
break
s.send(transend(c))
s.close()
print '--- Bye~ ---'
@binderclip
Copy link
Author

请连接 xcf_2.4GHz 路由器!

然后用 Python 2 执行上面的 cli_car.py 脚本就可以玩了!

arduino_car.ino 是车上的程序。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment