public
Created

HSKL Demo Bot Test Code

  • Download Gist
client.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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()
new_softserial_relay
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#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);
}
}
robot_code_function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
//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);
}
robot_control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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);
}
robot_test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.