Sunday, August 29, 2010

Windows to PIC Interface - (Hexapod Part 6)

The full source code can be found here.

Servo Control
I've now 'closed the loop' for communications with the PIC. Each servo has a pair of scroll bars. One showing the desired position, the other showing the actual. There are two reason why a servo may not be in the desired position.

The first is that the servo's calibration may not allow it to move to that position. If the user selects position 255 and the maximum position the servo can move to is 250 the 'Actual' position will show this. Thats the easy one. The more difficult scenario is that the servo is 'stuck' against something I.E. during a leg movement the leg has come up against an obstacle.

Each servos current will be monitored. If the current exceeds a preset limit (probably around 100ma) the servo will be considered stuck. The PIC will then back the servo off until the current becomes acceptable (probably around 50ma). Neither of these scenarios are implemented in the firmware yet but the UI has allowed for it.

The windows UI has a timer, currently set to 1 second. Each tick of the timer a single byte packet (0x02) is sent to the PIC. The PIC responds with a position packet, which consists of 4 bytes. The first byte is 'A' (0x41). This indicates the following 3 bytes will be the position of the three servos attached to this PIC. The UI receives these bytes and displays them on the scroll bar associated with the each servo. This is for the 'Actual' position.

When the user moves the 'Desired' position scroll bar the UI sends a packet to the PIC which consists of 3 bytes. The first byte (0x01) indicates this is a positioning packet. The second byte indicates which servo should be positioned and the third byte indicates the position it should be moved to.

Request: 02            - UI to PIC, Send Positions
Answer: 41 7F A0 A0    - PIC to UI, Positions are 0x7F, 0xA0 and 0xA0
Request:01 00 17       - UI to PIC, Set servo 0 to position 0x17
02                     - UI to PIC, Send Positions
Answer: 41 17 A0 A0    - PIC to UI, Positions are 0x17, 0xA0 and 0xA0
Request: 01 00 11      - UI to PIC, Set servo 0 to position 0x11
02                     - UI to PIC, Send Positions
Answer: 41 11 A0 A0    - PIC to UI, Positions are 0x11, 0xA0 and 0xA0

Under some circumstances (yet to be determined) the PIC stops responding. It is still holding the servo in position so it hasn't 'locked up' but it seems to have got out of whack somewhere. That'll be the next challenge.

After thats resolved I'll be looking at hooking the current measuring shunts up to the A/D converter and sensing overload situations.

NOTE: Big lesson learned while trying to get the RS232 interface going (specifically the PIC receiving data) don't use hyperterminal. Not sure what it was doing but after trying everything I could think of in the PIC firmware I tried using a different terminal tool and everything started working. So, if your considering using hyperterm, don't, just don't.

No comments:

Post a Comment