Lucas Teske revisó este gist 10 years ago. Ir a la revisión
2 files changed, 196 insertions
axispipe.py(archivo creado)
| @@ -0,0 +1,53 @@ | |||
| 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(archivo creado)
| @@ -0,0 +1,143 @@ | |||
| 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 | + | } | |
Siguiente
Anterior