def index(): if request.method == 'POST': if request.form["direction"] == "forward": car.forward() elif request.form["direction"] == "backward": car.backward() else: car.stop() return render_template('./UI.html')
def car_control(keyboard, car): """Control the movement of the car using keyboard. Args: keyboard: Keyboard to handle key events. car: Car object to control the movement. """ # A key flag indicating which key has been pressed. # This key flag will be used to classify the images. global key_flag # Start handle the key enents in this infinite loop. while True: for event in keyboard.event.get(): # Capture key press events. if event.type == keyboard.KEYDOWN: # Get the key value. key_input = keyboard.key.get_pressed() # When you press the up arrow key, control the car to move forward. if key_input[keyboard.K_UP]: # Sets the PWM value to the car. car.move_forward(4) key_flag = 'f' # Update the key flag. print('Move forward') # When you press the left arrow key, control the car to turn left. elif key_input[keyboard.K_LEFT]: # Sets the PWM value to the car. car.rotate_left(4) key_flag = 'l' # Update the key flag. print('Ture left') # When you press the right arrow key, control the car to turn right. elif key_input[keyboard.K_RIGHT]: # Sets the PWM value to the car. car.rotate_right(4) key_flag = 'r' # Update the key flag. print('Ture right') # Capture key up events. # After releasing the key, control the car to stop moving. elif event.type == keyboard.KEYUP: # Stop the car. car.stop() key_flag = 's' # Update the key flag. print('Stop') # Quit key events loop. elif event.type == keyboard.QUIT: raise SystemExit
def driving(qRead): global state global running while running: s = state if s == DRIVING: #start the car if it is stopped if car.stopped: car.start() try: #read the steering angle from the remote control and update the car angle = qRead.get(True, 0.2) car.steeringAngle = angle car.update() except queue.Empty as e: print('Timeout on get angle from queue') elif s == AI: #start the car if it is stopped if car.stopped: car.start() #use the neural network to compute the steering angle from the current video frame angle = car_ai.compute_steering_angle(cap) print("angle " + str(angle)) car.steeringAngle = angle car.update() elif s == LD: #start the car if it is stopped if car.stopped: car.start() #use the lane algorithm to compute the steering angle from the current video frame angle = car_ai.computeSteeringAngleWithHough(cap) print("angle " + str(angle)) car.steeringAngle = angle car.update() else: if not car.stopped: #stop the car print('STOP CAR') car.stop()
try: port = 5555 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(("", port)) print "waiting on port:", port data = bytearray(10) while True: s.recvfrom_into(data) direction = data[0] angle = struct.unpack_from('<d', data, 1)[0] if direction == 0: car.stop() elif direction == 1: car.forward() elif direction == 2: car.backward() car.stear(angle) except KeyboardInterrupt: pass except: raise finally: car.cleanup()
def SetInstruction_processing(bot, update): global car_user global Failure_time global photos global car_user text = str(update.message.text) car.update_user(update.message.from_user.id, update.message.from_user.username, update.message.from_user.last_name, update.message.from_user.first_name) if '/save_photos' == text[0:12]: if photos != []: car.new_car_data(photos, 1) photos = [] update.message.reply_text("OK") else: update.message.reply_text("沒資料") elif '/start_car' == text[0:10]: dd = car.blacklist(update.message.from_user.id) if dd == -1: car.Start(bot_id, server_host, username, password, from_port) dd = car.blacklist(update.message.from_user.id) if(dd): x = bot.send_message( update.message.chat.id, "你現在無法發車,你濫用機器人遭封鎖.\n%s解除封鎖\n如有疑問請尋找管理員 @abab5601" % (dd)) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, "黑名單要求開車") car.check_message(x.chat.id, x.message_id, 7) bot.deleteMessage(update.message.chat.id, update.message.message_id) return True if not car.start_car(update.message.chat.id, update.message.from_user.id): bot.send_message(user_id, '發車錯誤\n頻道 : %s 使用者 : %s' % ( update.message.chat.id, update.message.from_user.id)) d = update.message.reply_text('無法發車--請重試\n多次無發發車請通知管理員 @abab5601') car.check_message(update.message.chat.id, update.message.message_id, 7) car.check_message(d.chat.id, d.message_id, 7) return False id_ = update.message.from_user.name car_user = [update.message.chat.id, update.message.from_user.id, time.time()+Failure_time] message = bot.send_message(update.message.chat.id, '%s 你可以了開車\n我可以幫你收車\n開車時間至%s' % (id_, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+Failure_time)))) car.check_message(message.chat.id, message.message_id, 7) message = bot.send_message( update.message.chat.id, '#發車記錄點!(開始)%s' % (update.message.from_user.name)) car.new_car(update.message.chat.id, message.message_id) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '開始開車') elif '/car' == text[0:4]: car.new_car(update.message.chat.id, update.message.message_id) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '發送資源\n%s' % (update.message.text)) message = bot.send_message( update.message.chat.id, '倒數%s秒後刪除' % (car_time)) car.check_message(message.chat.id, message.message_id, 7) elif '/stop_car' == text[0:9]: ren = car.stop_car(update.message.chat.id, update.message.from_user.id) if ren == -1: car.Start( bot_id=bot_id, host=server_host, user=username, password=password, database=from_port ) ren = car.stop_car(update.message.chat.id, update.message.from_user.id) if(ren == -1): bot.send_message(user_id, '發生錯誤:\n頻道 : %s 使用者 : %s' % ( update.message.chat.id, update.message.from_user.id)) update.message.reply_text('無法停止發車--請重試\n多次無發發車請通知管理員') return False if ren and ren != -1: car_user = [update.message.chat.id, update.message.from_user.id, time.time()] message = bot.send_message( update.message.chat.id, '停止\n資源時間到會自動刪除') car.check_message(message.chat.id, message.message_id, 10) message = bot.send_message( update.message.chat.id, '#發車記錄點!(結束)%s' % (update.message.from_user.name)) car.new_car(update.message.chat.id, message.message_id) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '停止發車') elif ren != -1: message = bot.send_message(update.message.chat.id, '你沒有正在開車') car.check_message(message.chat.id, message.message_id, 5) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '停止發車無效') elif '/status_car' == text[0:11]: id_ = update.message.from_user.name if caruser(update.message.chat.id, update.message.from_user.id): message = bot.send_message( update.message.chat.id, '%s 現在開車狀態 : 開車中' % (id_)) car.check_message(message.chat.id, message.message_id, 5) else: message = bot.send_message( update.message.chat.id, '%s 現在開車狀態 : 休息中' % (id_)) car.check_message(message.chat.id, message.message_id, 5) elif '/黑名單' == text[0:4]: admin = False car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, "要求封鎖黑名單") if(update.message.reply_to_message): blacklist_id = car.messageLookFor( update.message.chat.id, update.message.reply_to_message.message_id) if(blacklist_id == -2): a = bot.send_message(update.message.chat.id, "操作太快請稍後重試\n如多次錯誤請通知管理員") car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖錯誤 : 找不到訊息ID") car.check_message(a.chat.id, a.message_id, 7) car.check_message(update.message.chat.id, update.message.message_id, 7) return False else: a = bot.send_message(update.message.chat.id, "指令使用錯誤\n請回復要黑名單的訊息") car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖錯誤 : 指令格式錯誤") car.check_message(a.chat.id, a.message_id, 7) car.check_message(update.message.chat.id, update.message.message_id, 7) return False if(blacklist_id == bot.id): bot.send_message(update.message.chat.id, "此訊息由機器人主動發出(非匿名發車),無法封鎖") a = bot.deleteMessage(update.message.chat.id, update.message.message_id) car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖錯誤 : 機器人訊息") car.check_message(a.chat.id, a.message_id, 7) car.check_message(update.message.chat.id, update.message.message_id, 7) return False for x in bot.get_chat_administrators(-1001278835291): if(x.user.id == blacklist_id): a = bot.send_message(update.message.chat.id, "錯誤 : 無發封鎖管理員\n請聯絡 @abab5601") car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖錯誤 : 無法封鎖管理員 %s" % (blacklist_id)) bot.deleteMessage(update.message.chat.id, update.message.message_id) car.check_message(a.chat.id, a.message_id, 7) return False elif(x.user.id == update.message.from_user.id): admin = True The_reason, DD, Day = text[4:].replace(" ", "").partition(':') try: f = int(Day) except ValueError: f = 0 if(DD != ':' and (f < -1)): a = bot.send_message(update.message.chat.id, "指令發送錯誤\n格式 : \n/黑名單 封鎖原因 : 天數 \n如日期為永久則輸入-1\n取消黑名單天數輸入0") car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖錯誤 : 指令格式錯誤") bot.deleteMessage(update.message.chat.id, update.message.message_id) car.check_message(update.message.chat.id, update.message.message_id, 7) car.check_message(a.chat.id, a.message_id, 7) return False da = bot.get_chat_member(update.message.chat.id, blacklist_id) if(not admin): c = bot.send_message(update.message.chat.id, "你不是管理員無法封鎖\n以幫你向管理員檢舉") car.log(update.message.chat.id, c.from_user.id, c.message_id, "封鎖錯誤 : 不是管理員 想封鎖對象 , 回復") d = bot.send_message(-1001215609662, "%s檢舉%s請確認\n群組名稱 : %s\n原因 : %s" % (update.message.from_user.name, da.user.name, update.message.chat.title, The_reason)) car.log(-1001215609662, d.from_user.id, d.message_id, "封鎖錯誤 : 不是管理員 想封鎖對象 : %s" % (blacklist_id)) bot.deleteMessage(update.message.chat.id, update.message.message_id) car.check_message(c.chat.id, c.message_id, 7) return False if(not car.blacklist_status(blacklist_id, True, f)): a = bot.send_message(update.message.chat.id, "錯誤 : \n系統錯誤") car.log(update.message.chat.id, a.from_user.id, a.message_id, "封鎖失敗 %s" % (blacklist_id)) car.check_message(a.chat.id, a.message_id, 7) else: car_user[3] = time.time()-1 car.point_Reset(blacklist_id) car.stop_car(1, update.message.from_user.id) a = update.message.reply_text("已成功封鎖") car.log(update.message.chat.id, a.from_user.id, a.message_id, "新增黑名單成員 : %s 因%s被%s封鎖%s天" % (blacklist_id, The_reason, update.message.from_user.name, Day)) car.check_message(a.chat.id, a.message_id, 7) car.check_message(update.message.chat.id, update.message.message_id, 7) for user in get_subscriber(0): message = bot.send_message( user, "%s因為%s被封鎖%s天" % (da.user.name, The_reason, Day)) car.log(user, update.message.from_user.id, message.message_id, '發送%s因為%s被封鎖%s天訊息' % (da.user.name, The_reason, Day)) bot.send_message(-1001215609662, "%s因為%s被封鎖%s天" % (da.user.name, The_reason, Day)) return True elif '/GetID' == text[0:6]: bot.send_message(update.message.chat.id, '頻道 ID : '+str( update.message.chat.id)+'\n使用者 ID : '+str(update.message.from_user.id)) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '取得ID') bot.deleteMessage(update.message.chat.id, update.message.message_id) elif '/time_message' == text[0:13]: bot.deleteMessage(update.message.chat.id, update.message.message_id) car.start_car(update.message.chat, update.message.chat) message = bot.send_message(update.message.chat, '群組開始進入限時對話\n限時對話將於%s結束或是手動關閉\n限時對話 : 每則訊息會在發出後的%s秒後刪除' % (str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+Failure_time))), str(car_time))) car.check_message(message.chat.id, message.message_id, car_time) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '開啟限時對話') elif '/stop_time_message' == text[0:18]: bot.deleteMessage(update.message.chat.id, update.message.message_id) if car.stop_car(update.message.chat.id, update.message.from_user.id): message = bot.send_message( update.message.chat, '已恢復正常對話模式\n限時對話模式中的訊息會在倒數計時到後刪除') car.check_message(message.chat.id, message.message_id, 5) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '結束限時對話') else: message = bot.send_message(update.message.chat, '頻道未開啟限時對話') car.check_message(message.chat.id, message.message_id, 5) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '結束限時對話失敗') elif '/status_time_message' == text[0:20]: if timemessage(update.message.chat.id): message = bot.send_message(update.message.chat.id, '對話狀態 : 限時對話') car.check_message(message.chat.id, message.message_id, 5) else: message = bot.send_message(update.message.chat.id, '對話狀態 : 正常狀態') car.check_message(message.chat.id, message.message_id, 5) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, "查詢頻道狀態") elif '/status_point' == text[0:13]: re = bot.get_chat_member(Backup, update.message.from_user.id) if(re.status == 'member' or re.status == 'restricted' or re.status == 'administrator'): car.vip_group_members(update.message.from_user.id, 1) else: car.vip_group_members(update.message.from_user.id, 0) ls = car.point_vip_get(update.message.from_user.id) if(update.message.chat.id < 0): message = bot.send_message(update.message.chat.id, "請在私人聊天室查詢") car.check_message(message.chat.id, message.message_id, 2) elif(ls[1] == 1): if ls[0] > 0: bot.send_message(update.message.chat.id, "你已在群組內\n 今日差 %s 分浮出水面 \n 每日所需最低積分 : %s" % ( ls[0], death_message)) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '使用者查詢vip資格 : 已在群內 -- 今日積分未達成 差 %s 分' % (ls[0])) else: ls[0] -= death_message ls[0] *= -1 bot.send_message(update.message.chat.id, "你已在群組內\n 今日積分已達 %s 分 \n 每日所需最低積分 : %s\n已達成今日所需!" % ( ls[0], death_message)) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '使用者查詢vip資格 : 已在群內 -- 今日積分已達成 %s 分' % (ls[0])) elif ls[0] <= 0: id_ = update.message.from_user.id bot.send_message(update.message.chat.id, "你好歡迎進入群組\n群組規則 : 只要積分未達%s分連續%s天將會被強制剔除\n請在今日24點前進入\n連結 : %s" % ( death_message, death_day, str(bot.export_chat_invite_link(Backup)))) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, "申請進入群組(完成)") try: bot.unban_chat_member(Backup, id_) except: pass else: bot.send_message(update.message.chat.id, "你在%s分即可進入備份群" % (ls[0])) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, "無法進入vip群 缺少積分 : %s" % (ls[0])) elif '/start' == text[0:6]: car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '查詢機器人指令') message = bot.send_message(update.message.chat.id, '%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s' % ( '機器人指令列表 :', '/start : 查看所有指令', '/start_car : 開始發大量資源 \n-->發完指令後你個人發的訊息在特定時間內都會算做資源', #'/car : 發網址資源(指令只能發在訊息的第一行,同一則訊息才算)', '/status_car : 查詢目前開車狀態', '/stop_car : 與 /start_car 關聯\n-->發完資源後回復正常對話模式\n', #'/time_message : 在此模式下期間,所有訊息會被刪除', #'/status_time_message : 查詢目前對話模式', #'/stop_time_message : 與 /time_message 搭配\n-->用於提早結束限時對話模式\n', '新備份群機制說明 :\n因備份群公開後經常被舉報群組掛掉\n因此現在改積分制管理', '\n累計積分方法:\n在群內發車(2分)或聊天一則訊息(1分)-指令除外\n使用機器人匿名發車一台車2分(匿名開車教學往下看)\n積分會在每日24奌扣%s\n\n進群發法:\n使用查詢活躍度指令當積分足夠會得到群網址' % (death_message), '\n備份群規範 : \n進群須達積分%s分才可進入\n進入後如%s天以上未達%s分會被強制剔除!\n(如被剔除重進方法: 與進群方法相同)(積分上限 : 1000)' % ( death_message, death_day, death_message), '/status_point :查詢活躍度', '與機器人的對話將在 60 秒後刪除\n機器人開發者 : @abab5601', '目前機器人剛開發完成有BUG或其他問題情直接聯繫開發者', # 點名 : 自動通知群內溺水的人起來呼吸(說警告比較貼切)' '新增功能 : \n匿名發車 : 再羅咖以外的聊天室發 /start_car 即可開始發車\n' )) car.check_message(message.chat.id, message.message_id, 60) elif '/reboot' == text[0:7]: if((update.message.chat.id == user_id) or (update.message.chat.id == -1001297029945)): bot.send_message(update.message.chat.id, 'ok') car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '重新啟動 - %s' % (bot_id)) if os.name == 'posix': t = threading.Thread(target=reboot) t.start() car.stop() else: bot.send_message(update.message.chat.id, '重啟失敗:目前使用系統 = win system') else: bot.send_message(update.message.chat.id, '權限不足') car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '呼叫重新啟動') else: car.check_message(update.message.chat.id, update.message.message_id, 5) message = bot.send_message( update.message.chat.id, '無效指令\n輸入 /start 查看全指令') car.check_message(message.chat.id, message.message_id, 5) car.log(update.message.chat.id, update.message.from_user.id, update.message.message_id, '無效指令 : %s' % (update.message.text)) bot.deleteMessage(update.message.chat.id, update.message.message_id)
from flask import Flask,render_template,request app = Flask(__name__) @app.route("/",methods=['GET','POST']) def index(): if request.method == 'POST': if request.form["direction"] == "forward": car.forward() elif request.form["direction"] == "backward": car.backward() else: car.stop() return render_template('./UI.html') if __name__ == "__main__": app.run(host='0.0.0.0',port=8888) car.stop() car.terminate() GPIO.cleanup()
def back(on): print "back: %s" % on if on: car.back() else: car.stop()
def stop(): print "stop" car.stop()
# Clear command once issued cmd = "" # Move onto next character i += 1 ## ## Main starting piont of server ## running = True try: while running: print "Waiting ..." # Wait to accept a connection from client control app conn, addr = s.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) # Start listening to new socket on seperate thread start_new_thread(clientthread, (conn, )) finally: print "Exiting" try: car.stop() # Stop the car if we get here in error except: pass s.close() time.sleep(0.1) # wait a little for threads to finish car.cleanup()
def forward(on): print "forward: %s" % on if on: car.forward() else: car.stop()
cmd = "" # Move onto next character i += 1 ## ## Main starting piont of server ## running = True try: while running: print "Waiting ..." # Wait to accept a connection from client control app conn, addr = s.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) # Start listening to new socket on seperate thread start_new_thread(clientthread ,(conn,)) finally: print "Exiting" try: car.stop() # Stop the car if we get here in error except: pass s.close() time.sleep(0.1) # wait a little for threads to finish car.cleanup()
def stop_controller(): stop() return 'OK'