axispipe.py
· 1.6 KiB · Python
Bruto
import serial
import struct
import os
import sys
import time
class AxisPipe:
def __init__(self, port):
self.port = port
self.serial = serial.Serial(port, 115200, timeout=0.01)
self.axis = [ 0,0,0,0 ]
self.aux = [ 0,0 ]
def UpdateAxis(self, axis, value):
if self.axis[axis] != value & 0xFFFF:
print "Updating Axis %s with %s" %(axis,value)
print "HUE(%s,%s)" % (value&0xFF,(value/256) & 0xFF)
self.axis[axis] = value & 0xFFFF
self.serial.write(struct.pack("BBB",axis,value&0xFF,(value/256)&0xFF))
def UpdateAux(self, aux, value):
if self.aux[aux] != value & 0xFFFF:
print "Updating Aux %s with %s" %(aux,value)
self.aux[aux] = value & 0xFFFF
self.serial.write(struct.pack("BBB",aux+7,value&0xFF,value/256))
def SetLed(self, val):
self.serial.write(struct.pack("BBB",4,val&0xFF,0))
def ReadData(self):
data = self.serial.readline()
if len(data) > 1:
print "Response: %s" %data.replace("\n","")
def TurnOn(self):
self.serial.write("\x05\x00\x00")
self.UpdateAxis(0,1500);
self.UpdateAxis(1,1500);
self.UpdateAxis(2,1500);
self.UpdateAxis(3,1500);
self.UpdateAux(0,1500);
self.UpdateAux(1,1500);
def TurnOff(self):
self.serial.write("\x06\x00\x00")
def KeepAlive(self):
self.serial.write("\x09\x00\x00")
def Close(self):
self.TurnOff()
self.serial.close()
| 1 | import serial |
| 2 | import struct |
| 3 | import os |
| 4 | import sys |
| 5 | import time |
| 6 | |
| 7 | class AxisPipe: |
| 8 | def __init__(self, port): |
| 9 | self.port = port |
| 10 | self.serial = serial.Serial(port, 115200, timeout=0.01) |
| 11 | self.axis = [ 0,0,0,0 ] |
| 12 | self.aux = [ 0,0 ] |
| 13 | |
| 14 | def UpdateAxis(self, axis, value): |
| 15 | if self.axis[axis] != value & 0xFFFF: |
| 16 | print "Updating Axis %s with %s" %(axis,value) |
| 17 | print "HUE(%s,%s)" % (value&0xFF,(value/256) & 0xFF) |
| 18 | self.axis[axis] = value & 0xFFFF |
| 19 | self.serial.write(struct.pack("BBB",axis,value&0xFF,(value/256)&0xFF)) |
| 20 | |
| 21 | def UpdateAux(self, aux, value): |
| 22 | if self.aux[aux] != value & 0xFFFF: |
| 23 | print "Updating Aux %s with %s" %(aux,value) |
| 24 | self.aux[aux] = value & 0xFFFF |
| 25 | self.serial.write(struct.pack("BBB",aux+7,value&0xFF,value/256)) |
| 26 | |
| 27 | def SetLed(self, val): |
| 28 | self.serial.write(struct.pack("BBB",4,val&0xFF,0)) |
| 29 | |
| 30 | def ReadData(self): |
| 31 | data = self.serial.readline() |
| 32 | if len(data) > 1: |
| 33 | print "Response: %s" %data.replace("\n","") |
| 34 | |
| 35 | def TurnOn(self): |
| 36 | self.serial.write("\x05\x00\x00") |
| 37 | self.UpdateAxis(0,1500); |
| 38 | self.UpdateAxis(1,1500); |
| 39 | self.UpdateAxis(2,1500); |
| 40 | self.UpdateAxis(3,1500); |
| 41 | |
| 42 | self.UpdateAux(0,1500); |
| 43 | self.UpdateAux(1,1500); |
| 44 | |
| 45 | def TurnOff(self): |
| 46 | self.serial.write("\x06\x00\x00") |
| 47 | |
| 48 | def KeepAlive(self): |
| 49 | self.serial.write("\x09\x00\x00") |
| 50 | |
| 51 | def Close(self): |
| 52 | self.TurnOff() |
| 53 | self.serial.close() |
axispipe_328.ino
· 3.6 KiB · Arduino
Bruto
/*
_ __ _____ ____ ____ ___ ____ _____
/ \ \ \/ /_ _/ ___|| _ \_ _| _ \| ____|
/ _ \ \ / | |\___ \| |_) | || |_) | _|
/ ___ \ / \ | | ___) | __/| || __/| |___
/_/ \_\/_/\_\___|____/|_| |___|_| |_____|
This is an Serial Axis Pipe developed for ATMEGA328
You basicly has 6 Servo outputs that are configurable through the serialport.
You will ALWAYS send 3 bytes through serial, and them are:
CMD DATA0 DATA1
CMD List:
0x00 => Enable Channel 0 Servo with DATA0 + DATA1 * 256
0x01 => Enable Channel 1 Servo with DATA0 + DATA1 * 256
0x02 => Enable Channel 2 Servo with DATA0 + DATA1 * 256
0x03 => Enable Channel 3 Servo with DATA0 + DATA1 * 256
0x04 => Do Nothing on Atmega328, used for led blinking
0x05 => Turn On the controller . The default state is turned off. This simulates controller Turn On Button
0x06 => Turn Off the controller. The default state is turned off. This simulates controller Turn Off Button
0x07 => Enable Aux Channel 0 Servo with DATA0 + DATA1 * 256
0x08 => Enable Aux Channel 1 Servo with DATA0 + DATA1 * 256
0x09 => Keep-Alive. You must send this at least more than 60 times per second. This is the safest and simplier way I found to make wireless fail-proof.
This is an prototype that is used with axispipe.py needs improvement.
Have fun!
*/
#include <Servo.h>
Servo ch[6];
unsigned char buff[3];
unsigned short count;
unsigned short kcount = 0;
void TurnOn() {
ch[0].attach(3);
ch[1].attach(5);
ch[2].attach(6);
ch[3].attach(9);
ch[4].attach(8);
ch[5].attach(10);
digitalWrite(13, HIGH);
Serial.println("Controller ON");
}
void TurnOff() {
/*
for(int i=0;i<6;i++)
ch[i].detach();
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(13, LOW);
Serial.println("Controller OFF");
*/
}
void KeepAlive() {
TCCR2B = 0x00;
TCNT2 = 0x00;
TIFR2 = 0x00;
TIMSK2 = 0x01;
TCCR2A = 0x00;
TCCR2B |= (1<< CS12) | (1<< CS10);
kcount = 0;
}
ISR(TIMER2_OVF_vect) {
//We didnt receive the signal yet. So we must shutdown the output
kcount++;
if(kcount == 255)
TurnOff();
TCCR2B = 0x00;
TCNT2 = 0x00;
TIFR2 = 0x00;
TIMSK2 = 0x01;
TCCR2A = 0x00;
TCCR2B |= (1<< CS12) | (1<< CS10);
}
void setup() {
Serial.begin(115200);
Serial.println("Inititializing controllers");
pinMode(13, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
count = 0;
Serial.println("AxisPipe Started!");
KeepAlive();
}
void loop() {
short val;
if(count == 3) {
count = 0;
val = buff[1] + buff[2] * 256;
Serial.println(val);
switch(buff[0]) {
case 0:
case 1:
case 2:
case 3:
ch[buff[0]].writeMicroseconds(val);
break;
case 4: break; // Nothing on 328
case 5: TurnOn(); break;
case 6: TurnOff(); break;
case 7:
case 8: // Aux Outputs
ch[buff[0]-3].writeMicroseconds(val);
break;
case 9: break;
default:
Serial.println("Unknown CMD");
}
KeepAlive();
}
if (Serial.available() > 0) {
buff[count] = Serial.read();
count++;
}
}
| 1 | /* |
| 2 | _ __ _____ ____ ____ ___ ____ _____ |
| 3 | / \ \ \/ /_ _/ ___|| _ \_ _| _ \| ____| |
| 4 | / _ \ \ / | |\___ \| |_) | || |_) | _| |
| 5 | / ___ \ / \ | | ___) | __/| || __/| |___ |
| 6 | /_/ \_\/_/\_\___|____/|_| |___|_| |_____| |
| 7 | |
| 8 | This is an Serial Axis Pipe developed for ATMEGA328 |
| 9 | |
| 10 | You basicly has 6 Servo outputs that are configurable through the serialport. |
| 11 | |
| 12 | You will ALWAYS send 3 bytes through serial, and them are: |
| 13 | |
| 14 | CMD DATA0 DATA1 |
| 15 | |
| 16 | CMD List: |
| 17 | |
| 18 | 0x00 => Enable Channel 0 Servo with DATA0 + DATA1 * 256 |
| 19 | 0x01 => Enable Channel 1 Servo with DATA0 + DATA1 * 256 |
| 20 | 0x02 => Enable Channel 2 Servo with DATA0 + DATA1 * 256 |
| 21 | 0x03 => Enable Channel 3 Servo with DATA0 + DATA1 * 256 |
| 22 | 0x04 => Do Nothing on Atmega328, used for led blinking |
| 23 | 0x05 => Turn On the controller . The default state is turned off. This simulates controller Turn On Button |
| 24 | 0x06 => Turn Off the controller. The default state is turned off. This simulates controller Turn Off Button |
| 25 | 0x07 => Enable Aux Channel 0 Servo with DATA0 + DATA1 * 256 |
| 26 | 0x08 => Enable Aux Channel 1 Servo with DATA0 + DATA1 * 256 |
| 27 | 0x09 => Keep-Alive. You must send this at least more than 60 times per second. This is the safest and simplier way I found to make wireless fail-proof. |
| 28 | |
| 29 | This is an prototype that is used with axispipe.py needs improvement. |
| 30 | |
| 31 | Have fun! |
| 32 | */ |
| 33 | #include <Servo.h> |
| 34 | |
| 35 | Servo ch[6]; |
| 36 | |
| 37 | unsigned char buff[3]; |
| 38 | unsigned short count; |
| 39 | unsigned short kcount = 0; |
| 40 | |
| 41 | void TurnOn() { |
| 42 | ch[0].attach(3); |
| 43 | ch[1].attach(5); |
| 44 | ch[2].attach(6); |
| 45 | ch[3].attach(9); |
| 46 | ch[4].attach(8); |
| 47 | ch[5].attach(10); |
| 48 | digitalWrite(13, HIGH); |
| 49 | Serial.println("Controller ON"); |
| 50 | } |
| 51 | |
| 52 | void TurnOff() { |
| 53 | /* |
| 54 | for(int i=0;i<6;i++) |
| 55 | ch[i].detach(); |
| 56 | digitalWrite(3, LOW); |
| 57 | digitalWrite(5, LOW); |
| 58 | digitalWrite(6, LOW); |
| 59 | digitalWrite(8, LOW); |
| 60 | digitalWrite(9, LOW); |
| 61 | digitalWrite(10, LOW); |
| 62 | digitalWrite(13, LOW); |
| 63 | Serial.println("Controller OFF"); |
| 64 | */ |
| 65 | } |
| 66 | |
| 67 | void KeepAlive() { |
| 68 | TCCR2B = 0x00; |
| 69 | TCNT2 = 0x00; |
| 70 | TIFR2 = 0x00; |
| 71 | TIMSK2 = 0x01; |
| 72 | TCCR2A = 0x00; |
| 73 | TCCR2B |= (1<< CS12) | (1<< CS10); |
| 74 | kcount = 0; |
| 75 | } |
| 76 | |
| 77 | ISR(TIMER2_OVF_vect) { |
| 78 | //We didnt receive the signal yet. So we must shutdown the output |
| 79 | kcount++; |
| 80 | if(kcount == 255) |
| 81 | TurnOff(); |
| 82 | TCCR2B = 0x00; |
| 83 | TCNT2 = 0x00; |
| 84 | TIFR2 = 0x00; |
| 85 | TIMSK2 = 0x01; |
| 86 | TCCR2A = 0x00; |
| 87 | TCCR2B |= (1<< CS12) | (1<< CS10); |
| 88 | } |
| 89 | |
| 90 | void setup() { |
| 91 | Serial.begin(115200); |
| 92 | Serial.println("Inititializing controllers"); |
| 93 | pinMode(13, OUTPUT); |
| 94 | pinMode(3, OUTPUT); |
| 95 | pinMode(5, OUTPUT); |
| 96 | pinMode(6, OUTPUT); |
| 97 | pinMode(8, OUTPUT); |
| 98 | pinMode(9, OUTPUT); |
| 99 | pinMode(10, OUTPUT); |
| 100 | |
| 101 | digitalWrite(3, LOW); |
| 102 | digitalWrite(5, LOW); |
| 103 | digitalWrite(6, LOW); |
| 104 | digitalWrite(8, LOW); |
| 105 | digitalWrite(9, LOW); |
| 106 | digitalWrite(10, LOW); |
| 107 | |
| 108 | count = 0; |
| 109 | Serial.println("AxisPipe Started!"); |
| 110 | KeepAlive(); |
| 111 | } |
| 112 | |
| 113 | void loop() { |
| 114 | short val; |
| 115 | if(count == 3) { |
| 116 | count = 0; |
| 117 | val = buff[1] + buff[2] * 256; |
| 118 | Serial.println(val); |
| 119 | switch(buff[0]) { |
| 120 | case 0: |
| 121 | case 1: |
| 122 | case 2: |
| 123 | case 3: |
| 124 | ch[buff[0]].writeMicroseconds(val); |
| 125 | break; |
| 126 | case 4: break; // Nothing on 328 |
| 127 | case 5: TurnOn(); break; |
| 128 | case 6: TurnOff(); break; |
| 129 | case 7: |
| 130 | case 8: // Aux Outputs |
| 131 | ch[buff[0]-3].writeMicroseconds(val); |
| 132 | break; |
| 133 | case 9: break; |
| 134 | default: |
| 135 | Serial.println("Unknown CMD"); |
| 136 | } |
| 137 | KeepAlive(); |
| 138 | } |
| 139 | if (Serial.available() > 0) { |
| 140 | buff[count] = Serial.read(); |
| 141 | count++; |
| 142 | } |
| 143 | } |