Created
November 19, 2011 02:19
-
-
Save sweemeng/1378327 to your computer and use it in GitHub Desktop.
HSKL Demo Bot Test Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Tkinter import * | |
import serial | |
import time | |
class RobotUI: | |
def __init__(self,master,port): | |
self.port = port | |
frame = Frame(master) | |
frame.pack() | |
self.forward_button = Button(frame,text="Forward",command=self.move_forward) | |
self.forward_button.pack(side=TOP) | |
self.backward_button = Button(frame,text="Backward",command=self.move_backward) | |
self.backward_button.pack(side=BOTTOM) | |
self.left_button = Button(frame,text="Rotate Left",command=self.rotate_left) | |
self.left_button.pack(side=LEFT) | |
self.right_button = Button(frame,text="Rotate Right",command=self.rotate_right) | |
self.right_button.pack(side=LEFT) | |
def move_forward(self): | |
print "forward" | |
self.move_('w') | |
def move_backward(self): | |
print "backward" | |
self.move_('s') | |
def rotate_left(self): | |
print "left" | |
self.move_('a') | |
def rotate_right(self): | |
print "right" | |
self.move_('d') | |
def move_(self,direction): | |
s = serial.Serial(self.port) | |
time.sleep(2) | |
s.write(direction) | |
s.close() | |
root = Tk() | |
app = RobotUI(root,2) | |
root.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewSoftSerial.h> | |
uint8_t pinRx=2; | |
uint8_t pinTx=4; | |
long BaudRate = 57600; | |
NewSoftSerial mySerial(pinRx,pinTx); | |
void setup(){ | |
Serial.begin(BaudRate); | |
mySerial.begin(BaudRate); | |
} | |
void loop(){ | |
if(mySerial.available()){ | |
int val = mySerial.read(); | |
mySerial.print(val); | |
Serial.print(val); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//int left_1 = 3; | |
int left_1 = 9; | |
//int left_2 = 2; | |
int left_2 = 8; | |
//int right_1 = 5; | |
int right_1 = 13; | |
//int right_2 = 4; | |
int right_2 = 12; | |
int speed_left = 10; | |
int speed_right = 11; | |
void setup(){ | |
pinMode(left_1,OUTPUT); | |
pinMode(left_2,OUTPUT); | |
pinMode(right_1,OUTPUT); | |
pinMode(right_2,OUTPUT); | |
pinMode(speed_left,OUTPUT); | |
pinMode(speed_right,OUTPUT); | |
} | |
void loop(){ | |
forward(); | |
delay(2000); | |
halt(); | |
backward(); | |
delay(2000); | |
halt(); | |
turn_left(); | |
delay(2000); | |
halt(); | |
turn_right(); | |
delay(2000); | |
halt(); | |
} | |
void halt(){ | |
wheel_halt(right_2,right_1); | |
wheel_halt(left_2,left_1); | |
} | |
void forward(){ | |
wheel_forward(right_1,right_2,speed_right); | |
wheel_forward(left_1,left_2,speed_left); | |
} | |
void turn_left(){ | |
wheel_forward(right_1,right_2,speed_right); | |
wheel_forward(left_2,left_1,speed_left); | |
} | |
void turn_right(){ | |
wheel_forward(right_2,right_1,speed_right); | |
wheel_forward(left_1,left_2,speed_left); | |
} | |
void backward(){ | |
wheel_forward(right_2,right_1,speed_right); | |
wheel_forward(left_2,left_1,speed_left); | |
} | |
void wheel_forward(int pin_1,int pin_2,int spd){ | |
analogWrite(spd,100); | |
digitalWrite(pin_1,HIGH); | |
digitalWrite(pin_2,LOW); | |
} | |
void wheel_backward(int pin_1,int pin_2){ | |
digitalWrite(pin_1,LOW); | |
digitalWrite(pin_2,HIGH); | |
} | |
void wheel_halt(int pin_1,int pin_2){ | |
digitalWrite(pin_1,LOW); | |
digitalWrite(pin_2,LOW); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int left_1 = 9; | |
int left_2 = 8; | |
int right_1 = 13; | |
int right_2 = 12; | |
int speed_left = 10; | |
int speed_right = 11; | |
void setup(){ | |
pinMode(left_1,OUTPUT); | |
pinMode(left_2,OUTPUT); | |
pinMode(right_1,OUTPUT); | |
pinMode(right_2,OUTPUT); | |
pinMode(speed_left,OUTPUT); | |
pinMode(speed_right,OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop(){ | |
int incoming_byte = 0; | |
char incoming_char; | |
if(Serial.available() > 0){ | |
incoming_byte = Serial.read(); | |
incoming_char = char(incoming_byte); | |
if(incoming_char == 'w'){ | |
forward(); | |
delay(2000); | |
halt(); | |
} | |
else if(incoming_char == 's'){ | |
backward(); | |
delay(2000); | |
halt(); | |
} | |
else if(incoming_char == 'a'){ | |
turn_left(); | |
delay(2000); | |
halt(); | |
} | |
else if(incoming_char == 'd'){ | |
turn_right(); | |
delay(2000); | |
halt(); | |
} | |
} | |
} | |
void halt(){ | |
wheel_halt(right_2,right_1); | |
wheel_halt(left_2,left_1); | |
} | |
void forward(){ | |
wheel_forward(right_1,right_2,speed_right); | |
wheel_forward(left_1,left_2,speed_left); | |
} | |
void turn_left(){ | |
wheel_forward(right_1,right_2,speed_right); | |
wheel_forward(left_2,left_1,speed_left); | |
} | |
void turn_right(){ | |
wheel_forward(right_2,right_1,speed_right); | |
wheel_forward(left_1,left_2,speed_left); | |
} | |
void backward(){ | |
wheel_forward(right_2,right_1,speed_right); | |
wheel_forward(left_2,left_1,speed_left); | |
} | |
void wheel_forward(int pin_1,int pin_2,int spd){ | |
analogWrite(spd,100); | |
digitalWrite(pin_1,HIGH); | |
digitalWrite(pin_2,LOW); | |
} | |
void wheel_backward(int pin_1,int pin_2){ | |
digitalWrite(pin_1,LOW); | |
digitalWrite(pin_2,HIGH); | |
} | |
void wheel_halt(int pin_1,int pin_2){ | |
digitalWrite(pin_1,LOW); | |
digitalWrite(pin_2,LOW); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Define pin number | |
int left_1 = 9; | |
int left_2 = 8; | |
int right_1 = 13; | |
int right_2 = 12; | |
int speed_left = 10; | |
int speed_right = 11; | |
void setup(){ | |
pinMode(left_1,OUTPUT); | |
pinMode(left_2,OUTPUT); | |
pinMode(right_1,OUTPUT); | |
pinMode(right_2,OUTPUT); | |
pinMode(speed_left,OUTPUT); | |
pinMode(speed_right,OUTPUT); | |
} | |
void loop(){ | |
analogWrite(speed_left,100); | |
digitalWrite(left_1,HIGH); | |
digitalWrite(left_2,LOW); | |
digitalWrite(left_1,LOW); | |
delay(1000); | |
analogWrite(speed_right,100); | |
digitalWrite(right_1,HIGH); | |
digitalWrite(right_2,LOW); | |
digitalWrite(right_1,LOW); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment