The control of servomotor through MSN is basically successful. Be sure to use the root user. In addition, the charset of msnp is UTF-8
Condition: phidgets hardware (servo interface board, servo motor, or Arduino)
Servo Motor Control End, can be PC or single board machine (with USB host)
Msnp (Python Implementation of MSN)
Phidgets driver and pythonphidgets (Python Implementation of phidgets)
Phidgetsmsncontoller. py (your program)
As follows:
The basic code is as follows:
# Simple control protocol:
# Command: ctype, para1, para2
# Get board attach status: ctype ex: 0 (get servo board attach status)
# Get motor engage status: ctype, index ex: 1, 0 (get servo motor index 0 engage status)
# Get position status: ctype, index ex: 2, 0 (get servo motor index 0 postion status)
# Set position status: ctype, index, POS ex: 3, 0,100 (servo motor index 0 move to position 100)
# Status: stype, para1, para2
# Board attach/detach status: stype, Val ex: 0, 0, 0/1 (servo board detached/attached)
# Motor engage/disengage status: stype, index, Val ex: 1, 0, 0/1 (servo motor index 0 disengage/engage)
# Position status: stype, index, POS ex: 2, 0,100 (servo motor index 0 moved to position 100)
# Basic imports
From ctypes import *
Import sys
From time import sleep
# Phidget specific imports
From phidgets. phidgetexception import *
From phidgets. Events. Events import *
From phidgets. devices. Servo import servo
# MSN imports
Import msnp
Class msnchatlistener (msnp. chatcallbacks ):
Def message_received (self, passport_id, display_name, text, charset ):
Print '% s: % s' % (passport_id, text)
Self. Chat. send_message (text, charset)
Self. sb. processcmd (text)
Class msnlistener (msnp. sessioncallbacks ):
Def _ init _ (Self ):
Self. SB = servoboard (Self)
Def chat_started (self, chat ):
Callbacks = msnchatlistener ()
Chat. Callbacks = callbacks
Callbacks. Chat = chat
Callbacks. SB = self. sb
Self. sb. Chat = chat
Self. sb. onchat = true
Def state_changed (self, State ):
If State = msnp. States. Online:
Print 'you are now online .'
Self. Online = true
Self. sb. open ()
Self. sb. Attach ()
Elif state = msnp. States. Offline:
Print 'you are now offline .'
Self. Online = false
Self. sb. Close ()
Class msncontroller ():
Def beginmsncontrol (Self ):
Self. msnlistener = msnlistener ()
MSN = msnp. Session (self. msnlistener)
MSN. login ('xxx @ hotmail.com ', 'xxx ')
MSN. sync_friend_list ()
While true:
MSN. Process (Chats = true)
Sleep (1)
######################################## ################
Class servoboard:
Def _ init _ (self, listener ):
# Create an servo object
Self. Servo = servo ()
Self. Listener = listener
Self. charset = 'utf-8'
Self. onchat = false
Def reportstatus (self, type, para1, para2 ):
Try:
TEXT = STR (type) + "," + STR (para1) + "," + STR (para2)
If (self. onchat ):
Self. Chat. send_message (text, self. charset)
Except t:
Print "Report status failed ."
Def processcmd (self, text ):
Try:
Cmd = text. Split (',')
If cmd [0] = '0 ':
If (self. Servo. isattached ()):
Self. reportstatus (0, 0, 1)
Else:
Self. reportstatus (0, 0, 0)
Elif cmd [0] = '1 ':
If (self. Servo. getengaged (INT (CMD [1]):
Self. reportstatus (1, INT (CMD [1]), 1)
Else:
Self. reportstatus (1, INT (CMD [1]), 0)
Elif cmd [0] = '2 ':
Self. reportstatus (2, INT (CMD [1]), self. Servo. getposition (INT (CMD [1])
Elif cmd [0] = '3 ':
Self. Servo. setposition (INT (CMD [1]), INT (CMD [2])
Else:
Pass
Except t:
Print "process command failed ."
# Information display function
Def displaydeviceinfo (Self ):
Print "| ------------ | ---------------------------------- | -------------- | ------------ |"
Print "|-attached-|-type-|-Serial No.-|-version-|"
Print "| ------------ | ---------------------------------- | -------------- | ------------ |"
Print "|-% 8 S-|-% 30 s-|-% 10d-|-% 8d-|" % (self. servo. isattached (), self. servo. getdevicetype (), self. servo. getserialnum (), self. servo. getdeviceversion ())
Print "| ------------ | ---------------------------------- | -------------- | ------------ |"
Print "Number of Motors: % I" % (self. Servo. getmotorcount ())
# Event handler callback Functions
Def servoattached (self, e ):
Attached = E. Device
Print "servo % I attached! "% (Attached. getserialnum ())
Self. reportstatus (0, 0, 1)
Def servodetached (self, e ):
Detached = E. Device
Print "servo % I detached! "% (Detached. getserialnum ())
Self. reportstatus (0, 0, 0)
Def servoerror (self, e ):
Print "phidget error % I: % s" % (E. ecode, E. description)
Def servopositionchanged (self, e ):
Print "Motor % I Current position: % F" % (E. index, E. position)
Self. reportstatus (2, E. index, E. position)
Def open (Self ):
# Main program code
Try:
Self. Servo. setonattachhandler (self. servoattached)
Self. Servo. setondetachhandler (self. servodetached)
Self. Servo. setonerrorhandler (self. servoerror)
Self. Servo. setonpositionchangehandler (self. servopositionchanged)
Except t phidgetexception, E:
Print "phidget exception % I: % s" % (E. Code, E. Message)
Print "exiting ...."
Exit (1)
Print "Opening phidget object ...."
Try:
Self. Servo. openphidget ()
Except t phidgetexception, E:
Print "phidget exception % I: % s" % (E. Code, E. Message)
Print "exiting ...."
Exit (1)
Def attach (Self ):
Print "waiting for attach ...."
Try:
Self. Servo. waitforattach (10000)
Except t phidgetexception, E:
Print "phidget exception % I: % s" % (E. Code, E. Message)
Try:
Self. Servo. closephidget ()
Except t phidgetexception, E:
Print "phidget exception % I: % s" % (E. Code, E. Message)
Print "exiting ...."
Exit (1)
Print "exiting ...."
Exit (1)
Else:
Self. displaydeviceinfo ()
Def close (Self ):
Print "closing ..."
Try:
Self. Servo. closephidget ()
Except t phidgetexception, E:
Print "phidget exception % I: % s" % (E. Code, E. Message)
Print "exiting ...."
Exit (1)
Print "done ."
Exit (0)
######################################## ################
If _ name _ = "_ main __":
MC = msncontroller ()
MC. beginmsncontrol ()
Problem to be resolved:
Unable to get the status of the motor connection (getengaged error,-is it caused by the driver version or outdated hardware? Changed getmotorstatus and setmotorstatus to getengaged and setengaged to reflect changes in the C API
)
Report to PC when an error occurs