###前提・実現したいこと
現在、PS3のコントローラーをBluetoothでつなぎラジコンにしてしまおうと考えています。
そこでこちらのプログラムを参考にしてGPIOを制御し動かそうと考えました。
(Rapiroにシリアル通信をするプログラム)
[http://www.kiluck.co.jp/rapiro/rapiro_ps3.py
](http://www.kiluck.co.jp/rapiro/rapiro_ps3.py)
Python
1import sys 2import serial 3import os 4import time 5import datetime 6file = open('/dev/input/js0','r') 7data = [] 8com = serial.Serial('/dev/ttyAMA0', 57600, timeout = 10) 9LLR = 20 #Left analog stick Left - Right 10LUD = 120 #Left analog stick UP - DOWN 11RLR = 100 #Right analog stick Left - Right 12RUD = 0 #Right analog stick UP - DOWN 13 14mode = "wait" 15before_time = 0 16now_time = 0 17 18while 1: 19 for character in file.read(1): 20 data += ['%02X' % ord(character)] 21 if len(data) == 8: 22 if data[6] == '01': 23 mode = "button" 24 if data[4] == '01': #pressed button 25 if data[7] == '00': # SELECT 26 sys.stdout.write('You pressed the SELECT button\n') 27 28 29 elif data[7] == '01': #L3 30 sys.stdout.write('You pressed the L3 button\n') 31 32 33 elif data[7] == '02': #R3 34 sys.stdout.write('You pressed the R3 button\n') 35 36 37 elif data[7] == '03': #START 38 sys.stdout.write('You pressed the START button\n') 39 40 41 elif data[7] == '04': #UP 42 sys.stdout.write('You pressed the UP button\n') 43 com.write("#M1") 44 45 elif data[7] == '05': #RIGHT 46 sys.stdout.write('You pressed the RIGHT button\n') 47 com.write("#M3") 48 49 elif data[7] == '06': #DOWN 50 sys.stdout.write('You pressed the DOWN button\n') 51 com.write("#M2") 52 53 elif data[7] == '07': #LEFT 54 sys.stdout.write('You pressed the LEFT button\n') 55 com.write("#M4") 56 57 elif data[7] == '08': #L2 58 sys.stdout.write('You pressed the L2 button\n') 59 time.sleep(0.11) 60 com.write("#PS07A040T001") 61 62 elif data[7] == '09': #R2 63 sys.stdout.write('You pressed the R2 button\n') 64 time.sleep(0.11) 65 com.write("#PS04A080T001") 66 67 elif data[7] == '0A': #L1 68 sys.stdout.write('You pressed the L1 button\n') 69 time.sleep(0.11) 70 com.write("#PS07A080T001") 71 72 elif data[7] == '0B': #R1 73 sys.stdout.write('You pressed the R1 button\n') 74 time.sleep(0.11) 75 com.write("#PS04A040T001") 76 77 elif data[7] == '0C': #TRIANGLE 78 sys.stdout.write('You pressed the TRIANGLE button\n') 79 com.write("#M5") 80 81 elif data[7] == '0D': #CIRCLE 82 sys.stdout.write('You pressed the CIRCLE button\n') 83 com.write("#M6") 84 85 elif data[7] == '0E': #CROSS 86 sys.stdout.write('You pressed the CROSS button\n') 87 com.write("#M7") 88 89 elif data[7] == '0F': #SQUARE 90 sys.stdout.write('You pressed the SQUARE button\n') 91 com.write("#M8") 92 93 94 elif data[4] == '00': #released button 95 96 if data[7] == '00': # SELECT 97 sys.stdout.write('You released the SELECT button\n') 98 elif data[7] == '01': #L3 99 sys.stdout.write('You released the L3 button\n') 100 elif data[7] == '02': #R3 101 sys.stdout.write('You released the R3 button\n') 102 elif data[7] == '03': #START 103 sys.stdout.write('You released the START button\n') 104 elif data[7] == '04': #UP 105 com.write("#M0") 106 sys.stdout.write('You released the UP button\n') 107 elif data[7] == '05': #RIGHT 108 com.write("#M0") 109 sys.stdout.write('You released the RIGHT button\n') 110 elif data[7] == '06': #DOWN 111 com.write("#M0") 112 sys.stdout.write('You released the DOWN button\n') 113 elif data[7] == '07': #LEFT 114 com.write("#M0") 115 sys.stdout.write('You released the LEFT button\n') 116 elif data[7] == '08': #L2 117 time.sleep(0.11) 118 com.write("#PS07A060T001") 119 sys.stdout.write('You released the L2 button\n') 120 elif data[7] == '09': #R2 121 time.sleep(0.11) 122 com.write("#PS04A060T001") 123 sys.stdout.write('You released the R2 button\n') 124 elif data[7] == '0A': #L1 125 time.sleep(0.11) 126 com.write("#PS07A060T001") 127 sys.stdout.write('You released the L1 button\n') 128 elif data[7] == '0B': #R1 129 time.sleep(0.11) 130 com.write("#PS04A060T001") 131 sys.stdout.write('You released the R1 button\n') 132 elif data[7] == '0C': #TRIANGLE 133 com.write("#M0") 134 sys.stdout.write('You released the TRIANGLE button\n') 135 elif data[7] == '0D': #CIRCLE 136 com.write("#M0") 137 sys.stdout.write('You released the CIRCLE button\n') 138 elif data[7] == '0E': #CROSS 139 com.write("#M0") 140 sys.stdout.write('You released the CROSS button\n') 141 elif data[7] == '0F': #SQUARE 142 com.write("#M0") 143 sys.stdout.write('You released the SQUARE button\n') 144 elif data[6] == '02': 145 146 now = datetime.datetime.now() 147 now_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000 148 a_data = int(data[5],16) 149 if a_data >= 0 and a_data < 128: 150 a_data = a_data + 128 151 elif a_data >= 128 and a_data < 256: 152 a_data = a_data - 128 153 154 155 if data[7] == '00': #Left stick L-R PS06 156 a_data = (int(a_data/2.13) - 120) * -1 157 a_data =int(( a_data - 60) * 1.66 + 20) 158 if a_data <= 20: 159 a_data = 20 160 LLR = a_data 161 joy = True 162 mode = "analog" 163 164 elif data[7] == '01': #Left stick U-D PS05 165 a_data = int(a_data/2.13) 166 a_data = a_data * 2 167 if a_data >= 120: 168 a_data = 120 169 LUD = a_data 170 joy = True 171 mode = "analog" 172 173 elif data[7] == '02': #Right stick L-R PS03 174 a_data = (int(a_data/2.13) - 120) * -1 175 a_data = int(a_data * 1.66) 176 if a_data >= 100: 177 a_data = 100 178 RLR = a_data 179 joy = True 180 mode = "analog" 181 182 elif data[7] == '03': #Right stick U-D PS02 183 a_data = (int(a_data/2.13) - 120) * -1 184 a_data = (a_data - 60 ) * 2 185 if a_data <= 0: 186 a_data = 0 187 RUD = a_data 188 joy = True 189 mode = "analog" 190 191 else: 192 joy = False 193 dif = now_time - before_time 194 if dif > 110 and joy == True: 195 com.write("#PS02A" + str(RUD).zfill(3) + "S03A" + str(RLR).zfill(3) + "S05A" + str(LUD).zfill(3) + "S06A" + str(LLR).zfill(3) + "T001\r\n") 196 197 com.write("ms : " + str(dif) + "\r\n") 198 now = datetime.datetime.now() 199 before_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000 200 sys.stdout.flush() 201 data = [] 202 now = datetime.datetime.now() 203 j_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000 204 dif = j_time - before_time 205 if dif > 200 and mode == "analog": 206 com.write("#PS02A" + str(RUD).zfill(3) + "S03A" + str(RLR).zfill(3) + "S05A" + str(LUD).zfill(3) + "S06A" + str(LLR).zfill(3) + "T001\r\n") 207 now = datetime.datetime.now() 208 before_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000 209sys.stdout.flush() 210data = []
こちらを参考にして書いてみようと考えました。
###発生している問題・エラーメッセージ
こちらからjs0の取得方法を真似して
Python2.7
1import sys 2import os 3import time 4import datetime 5file = open('/dev/input/js0','r') 6data = [] 7LLR = 20 #Left analog stick Left - Right 8LUD = 120 #Left analog stick UP - DOWN 9RLR = 100 #Right analog stick Left - Right 10RUD = 0 #Right analog stick UP - DOWN 11 12mode = "wait" 13before_time = 0 14now_time = 0 15 16while 1: 17 for character in file.read(1): 18 data += ['%02X' % ord(character)] 19 if len(data) == 8: 20 if data[6] == '01': 21 mode = "button" 22 if data[4] == '01': #pressed button 23 elif data[7] == '0D': #CIRCLE 24 print "Press Circle" 25
まではある程度理解できたのですが、
**elif data[6] == '02':**以降の処理が理解できません。
当方初心者につき稚拙な問題ではありますがよろしくお願いします。
###試したこと
python2.7
1elif data[6] == '02': 2 3 now = datetime.datetime.now() 4 now_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000 5 a_data = int(data[5],16) 6 if a_data >= 0 and a_data < 128: 7 a_data = a_data + 128 8 elif a_data >= 128 and a_data < 256: 9 a_data = a_data - 128 10 11 12 if data[7] == '00': #Left stick L-R PS06 13 a_data = (int(a_data/2.13) - 120) * -1 14 a_data =int(( a_data - 60) * 1.66 + 20) 15 if a_data <= 20: 16 a_data = 20 17 LLR = a_data 18 joy = True 19 mode = "analog" 20 print a_data
を先ほどの最後に追加しa_dataの処理後の値を見ようとするも何度見なおしてもインデントエラーになります。
###補足情報(言語/FW/ツール等のバージョンなど)
Python2.7 RaspberryPiB+
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/21 11:42