Hi,
Thanks to you all for your support!
Ok, here is the complete code. This code works perfectly. But if you set the RCch01 -04 to P12 - P15, the BodyPosY bug starts:
[code];Project Lynxmotion Phoenix
;Description: “Phoenix” style bot controlled by a wii remote
;Software version: V1.1
;Date: 25-06-2008
;Programmer: Jeroen Janssen (aka Xan)
;
;Hardware setup: ABB2 with ATOM 28 Pro, SSC32, BlueSmirf (See further for connections)
;
;NEW IN V1.1
; - 6 steps Ripple Gait
; - Lynxmotion PS2 controller support
;
;KNOWN BUGS:
; - Synchronisation PS2 controller (for more information see: http://www.lynxmotion.net/viewtopic.php?t=3979&postdays=0&postorder=asc&start=0)
; - Maximum height of the body. If BodyY > 128 it will overflow and gets a negative value which causes the hex to drop down with the legs in the air.
;
;TODO:
; - Include Deadzone on the controls
; - Replace sin, cos, atan with a byte table to improve calculating speed
; - Replace All Floats with sword * 1000 to improve calculating speed and free some memory
; - Implement 12 steps Gait
; - Run some synchronisation tests
; - Include balance calculations
;
;--------------------------------------------------------------------
;[CONSTANDS]
TRUE con 1
FALSE con 0
BUTTON_DOWN con 0
BUTTON_UP con 1
;--------------------------------------------------------------------
;[SERIAL CONNECTIONS]
SSC_OUT con P11 ;Output pin for (SSC32 RX) on BotBoard (Yellow)
SSC_IN con P10 ;Input pin for (SSC32 TX) on BotBoard (Blue)
SSC_BAUTE con i38400 ;SSC32 Baute rate
;--------------------------------------------------------------------
;[RC Controller]
RCch01 con P0
RCch02 con P1
RCch03 con P2
RCch04 con P3
RCpwmMAX con 1850
RCpwmMIN con 1150
;--------------------------------------------------------------------
;[PIN NUMBERS]
RFCoxaPin con P8 ;Front Right leg Hip Horizontal
RFFemurPin con P9 ;Front Right leg Hip Vertical
RFTibiaPin con P10 ;Front Right leg Knee
RMCoxaPin con P4 ;Middle Right leg Hip Horizontal
RMFemurPin con P5 ;Middle Right leg Hip Vertical
RMTibiaPin con P6 ;Middle Right leg Knee
RRCoxaPin con P0 ;Rear Right leg Hip Horizontal
RRFemurPin con P1 ;Rear Right leg Hip Vertical
RRTibiaPin con P2 ;Rear Right leg Knee
LFCoxaPin con P24 ;Front Left leg Hip Horizontal
LFFemurPin con P25 ;Front Left leg Hip Vertical
LFTibiaPin con P26 ;Front Left leg Knee
LMCoxaPin con P20 ;Middle Left leg Hip Horizontal
LMFemurPin con P21 ;Middle Left leg Hip Vertical
LMTibiaPin con P22 ;Middle Left leg Knee
LRCoxaPin con P16 ;Rear Left leg Hip Horizontal
LRFemurPin con P17 ;Rear Left leg Hip Vertical
LRTibiaPin con P18 ;Rear Left leg Knee
;--------------------------------------------------------------------
;[SERVO Offsets]
RFCoxaOffset con 733 ;Front Right leg Hip Horizontal ;650+83
RFFemurOffset con 617 ;Front Right leg Hip Vertical ;650-33
RFTibiaOffset con 644 ;Front Right leg Knee ;650-6
RMCoxaOffset con 623 ;Middle Right leg Hip Horizontal ;650-27
RMFemurOffset con 629 ;Middle Right leg Hip Vertical ;650-21
RMTibiaOffset con 696 ;Middle Right leg Knee ;650+46
RRCoxaOffset con 649 ;Rear Right leg Hip Horizontal ;650-1
RRFemurOffset con 627 ;Rear Right leg Hip Vertical ;650-23
RRTibiaOffset con 619 ;Rear Right leg Knee ;650-31
LFCoxaOffset con 699 ;Front Left leg Hip Horizontal ;650+49
LFFemurOffset con 665 ;Front Left leg Hip Vertical ;650+15
LFTibiaOffset con 655 ;Front Left leg Knee ;650+5
LMCoxaOffset con 639 ;Middle Left leg Hip Horizontal ;650-11
LMFemurOffset con 599 ;Middle Left leg Hip Vertical ;650-51
LMTibiaOffset con 634 ;Middle Left leg Knee ;650-16
LRCoxaOffset con 593 ;Rear Left leg Hip Horizontal ;650-57
LRFemurOffset con 613 ;Rear Left leg Hip Vertical ;650-37
LRTibiaOffset con 614 ;Rear Left leg Knee ;650-36
;--------------------------------------------------------------------
;[MIN/MAX ANGLES]
RFCoxa_MIN con -90 ;Mechanical limits of the Right Front Leg
RFCoxa_MAX con 80
RFFemur_MIN con -92
RFFemur_MAX con 92
RFTibia_MIN con -104
RFTibia_MAX con 74
RMCoxa_MIN con -58 ;Mechanical limits of the Right Middle Leg
RMCoxa_MAX con 96
RMFemur_MIN con -92
RMFemur_MAX con 95
RMTibia_MIN con -105
RMTibia_MAX con 72
RRCoxa_MIN con -100 ;Mechanical limits of the Right Rear Leg
RRCoxa_MAX con 52
RRFemur_MIN con -96
RRFemur_MAX con 95
RRTibia_MIN con -96
RRTibia_MAX con 82
LFCoxa_MIN con -80 ;Mechanical limits of the Left Front Leg
LFCoxa_MAX con 95
LFFemur_MIN con -97
LFFemur_MAX con 96
LFTibia_MIN con -81
LFTibia_MAX con 104
LMCoxa_MIN con -95 ;Mechanical limits of the Left Middle Leg
LMCoxa_MAX con 58
LMFemur_MIN con -94
LMFemur_MAX con 96
LMTibia_MIN con -81
LMTibia_MAX con 102
LRCoxa_MIN con -100 ;Mechanical limits of the Left Rear Leg
LRCoxa_MAX con 50
LRFemur_MIN con -96
LRFemur_MAX con 94
LRTibia_MIN con -82
LRTibia_MAX con 100
;--------------------------------------------------------------------
;[BODY DIMENSIONS]
CoxaLength con 29 ;Length of the Coxa [mm]
FemurLength con 76 ;Length of the Femur [mm]
TibiaLength con 106 ;Lenght of the Tibia [mm]
RFOffsetX con -43 ;Distance X from center of the body to the Right Front coxa
RFOffsetZ con -82 ;Distance Z from center of the body to the Right Front coxa
RMOffsetX con -63 ;Distance X from center of the body to the Right Middle coxa
RMOffsetZ con 0 ;Distance Z from center of the body to the Right Middle coxa
RROffsetX con -43 ;Distance X from center of the body to the Right Rear coxa
RROffsetZ con 82 ;Distance Z from center of the body to the Right Rear coxa
LFOffsetX con 43 ;Distance X from center of the body to the Left Front coxa
LFOffsetZ con -82 ;Distance Z from center of the body to the Left Front coxa
LMOffsetX con 63 ;Distance X from center of the body to the Left Middle coxa
LMOffsetZ con 0 ;Distance Z from center of the body to the Left Middle coxa
LROffsetX con 43 ;Distance X from center of the body to the Left Rear coxa
LROffsetZ con 82 ;Distance Z from center of the body to the Left Rear coxa
;--------------------------------------------------------------------
;Start height const
StartYPos con 0
;Trip inhibit
TripInhibit con 3
;--------------------------------------------------------------------
;[ANGLES]
RFCoxaAngle var sword ;Actual Angle of the Right Front Leg
RFFemurAngle var sword
RFTibiaAngle var sword
RMCoxaAngle var sword ;Actual Angle of the Right Middle Leg
RMFemurAngle var sword
RMTibiaAngle var sword
RRCoxaAngle var sword ;Actual Angle of the Right Rear Leg
RRFemurAngle var sword
RRTibiaAngle var sword
LFCoxaAngle var sword ;Actual Angle of the Left Front Leg
LFFemurAngle var sword
LFTibiaAngle var sword
LMCoxaAngle var sword ;Actual Angle of the Left Middle Leg
LMFemurAngle var sword
LMTibiaAngle var sword
LRCoxaAngle var sword ;Actual Angle of the Left Rear Leg
LRFemurAngle var sword
LRTibiaAngle var sword
;--------------------------------------------------------------------
;[POSITIONS]
RFPosX var sword ;Actual Position of the Right Front Leg
RFPosY var sword
RFPosZ var sword
RMPosX var sword ;Actual Position of the Right Middle Leg
RMPosY var sword
RMPosZ var sword
RRPosX var sword ;Actual Position of the Right Rear Leg
RRPosY var sword
RRPosZ var sword
LFPosX var sword ;Actual Position of the Left Front Leg
LFPosY var sword
LFPosZ var sword
LMPosX var sword ;Actual Position of the Left Middle Leg
LMPosY var sword
LMPosZ var sword
LRPosX var sword ;Actual Position of the Left Rear Leg
LRPosY var sword
LRPosZ var sword
;--------------------------------------------------------------------
;[INPUTS]
butA var bit
butB var bit
butC var bit
prev_butA var bit
prev_butB var bit
prev_butC var bit
;--------------------------------------------------------------------
;[OUTPUTS]
LedA var bit ;Red
LedB var bit ;Green
LedC var bit ;Orange
;--------------------------------------------------------------------
;[VARIABLES]
Index var byte ;Index used for freeing the servos
SSCDone var byte ;Char to check if SSC is done
;GetSinCos
AngleDeg var float ;Input Angle in degrees
ABSAngleDeg var float ;Absolute value of the Angle in Degrees
AngleRad var float ;Angle in Radian
sinA var float ;Output Sinus of the given Angle
cosA var float ;Output Cosinus of the given Angle
sinB var float ;Output Sinus of the given Angle
cosB var float ;Output Cosinus of the given Angle
sinG var float ;Output Sinus of the given Angle
cosG var float ;Output Cosinus of the given Angle
sinINT var word
cosINT var word
;GetBoogTan
BoogTanX var sword ;Input X
BoogTanY var sword ;Input Y
BoogTan var float ;Output BOOGTAN2(X/Y)
;Body position
BodyPosX var sword ;Global Input for the position of the body
BodyPosY var sword
BodyPosZ var sword
;Body Inverse Kinematics
BodyRotX var sword ;Global Input pitch of the body
BodyRotY var sword ;Global Input rotation of the body
BodyRotZ var sword ;Global Input roll of the body
PosX var sword ;Input position of the feet X
PosZ var sword ;Input position of the feet Z
PosY var sword ;Input position of the feet Y
RotationY var sbyte ;Input for rotation of a single feet for the gait
BodyOffsetX var sbyte ;Input Offset betweeen the body and Coxa X
BodyOffsetZ var sbyte ;Input Offset betweeen the body and Coxa Z
PitchY var sword ;PitchY offset, needs to be added to the BodyPosY
RollY var sword ;RollY offset, needs to be added to the BodyPosY
TotalX var sword ;Total X distance between the center of the body and the feet
TotalZ var sword ;Total Z distance between the center of the body and the feet
DistCenterBodyFeet var float ;Total distance between the center of the body and the feet
AngleCenterBodyFeetX var float ;Angle between the center of the body and the feet
BodyIKPosX var sword ;Output Position X of feet with Rotation
BodyIKPosY var sword ;Output Position Y of feet with Rotation
BodyIKPosZ var sword ;Output Position Z of feet with Rotation
;Leg Inverse Kinematics
IKFeetPosX var sword ;Input position of the Feet X
IKFeetPosY var sword ;Input position of the Feet Y
IKFeetPosZ var sword ;Input Position of the Feet Z
IKFeetPosXZ var sword ;Length between the coxa and feet
IKSW var float ;Length between shoulder and wrist
IKA1 var float ;Angle between SW line and the ground in rad
IKA2 var float ;?
IKSolution var bit ;Output true if the solution is possible
IKSolutionWarning var bit ;Output true if the solution is NEARLY possible
IKSolutionError var bit ;Output true if the solution is NOT possible
IKFemurAngle var sword ;Output Angle of Femur in degrees
IKTibiaAngle var sword ;Output Angle of Tibia in degrees
IKCoxaAngle var sword ;Output Angle of Coxa in degrees
;--------------------------------------------------------------------
;[GP player]
GPByte0 var Byte
GPByte1 var Byte
GPByte2 var Byte
GPByte3 var Byte
GPModeON var bit
GPIsPlaying var bit
;--------------------------------------------------------------------
;RC variable
pwmInput01 var word
pwmInput02 var word
pwmInput03 var word
pwmInput04 var word
RCtemp var sbyte
;[TEMP]
DebugOn var bit
;gait
LegLiftHeight var byte ;Current Travel height
TravelLengthX var sbyte ;Current Travel length X
TravelLengthZ var sbyte ;Current Travel length Z
TravelRotationY var sbyte ;Current Travel Rotation Y
GaitSpeed var nib ;Speed (Time) multiply factor
GaitStep var nib ;Global Input Gait step
GaitLegNr var nib ;Input Number of the leg
TravelMulti var sbyte ;Multiplier for the length of the step
Lastleg var bit ;True when it’s the last leg in one gait step
TLDivFactor var nib ;Travellength divide by factor depends of number of travelspteps in gait
NmbrOfStepInGait var nib ;Number of steps in one gait cycle
GaitMethod var bit ;Toogle between 6 step tripod and 9 step quadripple, so far just a bit
RFGaitLegNr var nib ;Gait number for leg in upper end position
RFGaitPosX var sbyte ;Relative position corresponding to the Gait
RFGaitPosY var sbyte
RFGaitPosZ var sbyte
RFGaitRotY var sbyte ;Relative rotation corresponding to the Gait
RMGaitLegNr var nib
RMGaitPosX var sbyte
RMGaitPosY var sbyte
RMGaitPosZ var sbyte
RMGaitRotY var sbyte
RRGaitLegNr var nib
RRGaitPosX var sbyte
RRGaitPosY var sbyte
RRGaitPosZ var sbyte
RRGaitRotY var sbyte
LFGaitLegNr var nib
LFGaitPosX var sbyte
LFGaitPosY var sbyte
LFGaitPosZ var sbyte
LFGaitRotY var sbyte
LMGaitLegNr var nib
LMGaitPosX var sbyte
LMGaitPosY var sbyte
LMGaitPosZ var sbyte
LMGaitRotY var sbyte
LRGaitLegNr var nib
LRGaitPosX var sbyte
LRGaitPosY var sbyte
LRGaitPosZ var sbyte
LRGaitRotY var sbyte
GaitPosX var sbyte ;In-/Output Pos X of feet
GaitPosY var sbyte ;In-/Output Pos Y of feet
GaitPosZ var sbyte ;In-/Output Pos Z of feet
GaitRotY var sbyte ;In-/Output Rotation Y of feet
;--------------------------------------------------------------------
;[INIT]
;Turning off all the leds
LedA = 0
LedB = 0
LedC = 0
'Feet Positions
RFPosX = 53 ;Start positions of the Right Front leg
RFPosY = StartYPos
RFPosZ = -91
RMPosX = 105 ;Start positions of the Right Middle leg
RMPosY = StartYPos
RMPosZ = 0
RRPosX = 53 ;Start positions of the Right Rear leg
RRPosY = StartYPos
RRPosZ = 91
LFPosX = 53 ;Start positions of the Left Front leg
LFPosY = StartYPos
LFPosZ = -91
LMPosX = 105 ;Start positions of the Left Middle leg
LMPosY = StartYPos
LMPosZ = 0
LRPosX = 53 ;Start positions of the Left Rear leg
LRPosY = StartYPos
LRPosZ = 91
;Body Positions
BodyPosX = 0
BodyPosY = 0
BodyPosZ = 0
;Body Rotations
BodyRotX = 0
BodyRotY = 0
BodyRotZ = 0
;Gait
LegLiftHeight = 50
GaitStep = 1
GaitSpeed = 5
;Initate with a 9 step quadripple gait
'GaitMethod = TRUE
TLDivFactor = 6
NmbrOfStepInGait = 9
LRGaitLegNr = 1
RFGaitLegNr = 2
LMGaitLegNr = 4
RRGaitLegNr = 5
LFGaitLegNr = 7
RMGaitLegNr = 8
;Initate 6 step tripod gait
'TLDivFactor = 3
'NmbrOfStepInGait = 6
'LRGaitLegNr = 4
'RFGaitLegNr = 1
'LMGaitLegNr = 1
'RRGaitLegNr = 1
'LFGaitLegNr = 4
'RMGaitLegNr = 4
;GP mode
GPModeON = FALSE
GPIsPlaying = FALSE
;--------------------------------------------------------------------
;[MAIN]
main:
gosub ReadButtons
gosub WriteLeds
gosub RCInput
if GPModeON=FALSE then
;Calculate Gait positions
LastLeg = FALSE
gosub Gait [LRGaitLegNr, LRGaitPosX, LRGaitPosY, LRGaitPosZ, LRGaitRotY]
LRGaitPosX = GaitPosX
LRGaitPosY = GaitPosY
LRGaitPosZ = GaitPosZ
LRGaitRotY = GaitRotY
gosub Gait [RFGaitLegNr, RFGaitPosX, RFGaitPosY, RFGaitPosZ, RFGaitRotY]
RFGaitPosX = GaitPosX
RFGaitPosY = GaitPosY
RFGaitPosZ = GaitPosZ
RFGaitRotY = GaitRotY
gosub Gait [LMGaitLegNr, LMGaitPosX, LMGaitPosY, LMGaitPosZ, LMGaitRotY]
LMGaitPosX = GaitPosX
LMGaitPosY = GaitPosY
LMGaitPosZ = GaitPosZ
LMGaitRotY = GaitRotY
gosub Gait [RRGaitLegNr, RRGaitPosX, RRGaitPosY, RRGaitPosZ, RRGaitRotY]
RRGaitPosX = GaitPosX
RRGaitPosY = GaitPosY
RRGaitPosZ = GaitPosZ
RRGaitRotY = GaitRotY
gosub Gait [LFGaitLegNr, LFGaitPosX, LFGaitPosY, LFGaitPosZ, LFGaitRotY]
LFGaitPosX = GaitPosX
LFGaitPosY = GaitPosY
LFGaitPosZ = GaitPosZ
LFGaitRotY = GaitRotY
LastLeg = TRUE
gosub Gait [RMGaitLegNr, RMGaitPosX, RMGaitPosY, RMGaitPosZ, RMGaitRotY]
RMGaitPosX = GaitPosX
RMGaitPosY = GaitPosY
RMGaitPosZ = GaitPosZ
RMGaitRotY = GaitRotY
'DebugOn = TRUE
;Right Front leg
gosub BodyIK -RFPosX+BodyPosX+RFGaitPosX, RFPosZ+BodyPosZ+RFGaitPosZ,RFPosY+BodyPosY+RFGaitPosY, RFOffsetX, RFOffsetZ, RFGaitRotY]
gosub LegIK [RFPosX-BodyPosX+BodyIKPosX-RFGaitPosX, RFPosY+BodyPosY-BodyIKPosY+RFGaitPosY, RFPosZ+BodyPosZ-BodyIKPosZ+RFGaitPosZ]
RFCoxaAngle = IKCoxaAngle + 60 ;60 degree for the basic setup for the front leg
RFFemurAngle = IKFemurAngle
RFTibiaAngle = IKTibiaAngle
'DebugOn = False
;Right Middle leg
gosub BodyIK -RMPosX+BodyPosX+RMGaitPosX, RMPosZ+BodyPosZ+RMGaitPosZ,RMPosY+BodyPosY+RMGaitPosY, RMOffsetX, RMOffsetZ, RMGaitRotY]
gosub LegIK [RMPosX-BodyPosX+BodyIKPosX-RMGaitPosX, RMPosY+BodyPosY-BodyIKPosY+RMGaitPosY, RMPosZ+BodyPosZ-BodyIKPosZ+RMGaitPosZ]
RMCoxaAngle = IKCoxaAngle
RMFemurAngle = IKFemurAngle
RMTibiaAngle = IKTibiaAngle
;Right Rear leg
gosub BodyIK -RRPosX+BodyPosX+RRGaitPosX, RRPosZ+BodyPosZ+RRGaitPosZ,RRPosY+BodyPosY+RRGaitPosY, RROffsetX, RROffsetZ, RRGaitRotY]
gosub LegIK [RRPosX-BodyPosX+BodyIKPosX-RRGaitPosX, RRPosY+BodyPosY-BodyIKPosY+RRGaitPosY, RRPosZ+BodyPosZ-BodyIKPosZ+RRGaitPosZ]
RRCoxaAngle = IKCoxaAngle - 60 ;60 degree for the basic setup for the front leg
RRFemurAngle = IKFemurAngle
RRTibiaAngle = IKTibiaAngle
;Left Front leg
gosub BodyIK [LFPosX-BodyPosX+LFGaitPosX, LFPosZ+BodyPosZ+LFGaitPosZ,LFPosY+BodyPosY+LFGaitPosY, LFOffsetX, LFOffsetZ, LFGaitRotY]
gosub LegIK [LFPosX+BodyPosX-BodyIKPosX+LFGaitPosX, LFPosY+BodyPosY-BodyIKPosY+LFGaitPosY, LFPosZ+BodyPosZ-BodyIKPosZ+LFGaitPosZ]
LFCoxaAngle = IKCoxaAngle + 60 ;60 degree for the basic setup for the front leg
LFFemurAngle = IKFemurAngle
LFTibiaAngle = IKTibiaAngle
;Left Middle leg
gosub BodyIK [LMPosX-BodyPosX+LMGaitPosX, LMPosZ+BodyPosZ+LMGaitPosZ,LMPosY+BodyPosY+LMGaitPosY, LMOffsetX, LMOffsetZ, LMGaitRotY]
gosub LegIK [LMPosX+BodyPosX-BodyIKPosX+LMGaitPosX, LMPosY+BodyPosY-BodyIKPosY+LMGaitPosY, LMPosZ+BodyPosZ-BodyIKPosZ+LMGaitPosZ]
LMCoxaAngle = IKCoxaAngle
LMFemurAngle = IKFemurAngle
LMTibiaAngle = IKTibiaAngle
;Left Rear leg
gosub BodyIK [LRPosX-BodyPosX+LRGaitPosX, LRPosZ+BodyPosZ+LRGaitPosZ,LRPosY+BodyPosY+LRGaitPosY, LROffsetX, LROffsetZ, LRGaitRotY]
gosub LegIK [LRPosX+BodyPosX-BodyIKPosX+LRGaitPosX, LRPosY+BodyPosY-BodyIKPosY+LRGaitPosY, LRPosZ+BodyPosZ-BodyIKPosZ+LRGaitPosZ]
LRCoxaAngle = IKCoxaAngle - 60 ;60 degree for the basic setup for the front leg
LRFemurAngle = IKFemurAngle
LRTibiaAngle = IKTibiaAngle
gosub CheckAngles
gosub ServoDriver
LedC = IKSolutionWarning
LedA = IKSolutionError
else
gosub GPPlayer
endif
goto main
;--------------------------------------------------------------------
;[ReadButtons] Reading input buttons from the ABB
ReadButtons:
input P4
input P5
input P6
prev_butA = butA
prev_butB = butB
prev_butC = butC
butA = IN4
butB = IN5
butC = IN6
return
;--------------------------------------------------------------------
;[WriteLEDs] Updates the state of the leds
WriteLEDs:
if ledA = 1 then
low p4
endif
if ledB = 1 then
low p5
endif
if ledC = 1 then
low p6
endif
return
;--------------------------------------------------------------------
;Read RC pulses and converts them to travellengths
;
RCInput:
pulsin RCch01,0, pwmInput01
pwmInput01 = (pwmInput01 min RCpwmMIN) max RCpwmMAX
BodyRotZ = (pwmInput01 -1500)/15
'TravelLengthX = (ZpwmInput -1500)/4
pulsin RCch03,0, pwmInput03
pwmInput03 = (pwmInput03 min RCpwmMIN) max RCpwmMAX
BodyPosY = (pwmInput03 -1150)/4
pulsin RCch02,0, pwmInput02
pwmInput02 = (pwmInput02 min RCpwmMIN) max RCpwmMAX
BodyRotX = -(pwmInput02 -1500)/15
pulsin RCch04,0, pwmInput04
pwmInput04 = (pwmInput04 min RCpwmMIN) max RCpwmMAX
TravelRotationY = (pwmInput04 -1500)/14
'serout s_out, i9600, "BodyPosY=", sdec BodyPosY," TLZ=", sdec TravelLengthZ," TLX=", sdec TravelLengthX," TRY=", sdec TravelRotationY, 13]
'serout s_out, i9600, "BodyPosY=", sdec BodyPosY," BodyRotZ=", sdec BodyRotZ," BodyRotX=", sdec BodyRotX," TRY=", sdec TravelRotationY, 13]
'serout s_out, i9600, "CH01=", sdec pwmInput01," CH02=", sdec pwmInput02," CH03=", sdec pwmInput03," CH04=", sdec pwmInput04, 13]
return
;--------------------------------------------------------------------
;[GAIT]
Gait [GaitLegNr, GaitPosX, GaitPosY, GaitPosZ, GaitRotY]
;Up start position:
IF (GaitStep=GaitLegNr+1) & ((ABS(TravelLengthX)>TripInhibit) | (ABS(TravelLengthZ)>TripInhibit) | (ABS(GaitPosX)>TripInhibit) | (ABS(GaitPosZ)>TripInhibit) | (ABS(GaitRotY)>TripInhibit)) THEN
GaitPosX = TravelLengthX/2
GaitPosY = -LegLiftHeight
GaitPosZ = TravelLengthZ/2
GaitRotY = TravelRotationY/2
ELSE
;Up end position:
IF (GaitStep=GaitLegNr) & ((ABS(TravelLengthX)>TripInhibit) | (ABS(TravelLengthZ)>TripInhibit) | (ABS(GaitPosX)>TripInhibit) | (ABS(GaitPosZ)>TripInhibit) | (ABS(GaitRotY)>TripInhibit)) THEN
GaitPosX = -TravelLengthX/2
GaitPosY = -LegLiftHeight
GaitPosZ = -TravelLengthZ/2
GaitRotY = -TravelRotationY/2
ELSE
;Down start position:
IF (GaitStep=GaitLegNr+2 | GaitStep=GaitLegNr-7) & GaitPosY<0 THEN
GaitPosX = TravelLengthX/2
GaitPosY = 0
GaitPosZ = TravelLengthZ/2
GaitRotY = TravelRotationY/2
;just walk:
ELSE
GaitPosX = GaitPosX - (TravelLengthX/TLDivFactor)
GaitPosY = 0
GaitPosZ = GaitPosZ - (TravelLengthZ/TLDivFactor)
GaitRotY = GaitRotY - (TravelRotationY/TLDivFactor)
ENDIF
ENDIF
ENDIF
;serout S_OUT, i9600, “GaitStep=”, sdec GaitStep, " LegNr=", sdec GaitLegNr, " TLZ=", sdec TravelLengthZ," Z=", sdec GaitPosZ, " TLX=", sdec TravelLengthX, " X=", sdec GaitPosX, " Y=", sdec GaitPosY,13]
;Advance to the next step
IF LastLeg THEN ;the last leg in this step (RM leg)
GaitStep = GaitStep+1
IF GaitStep>NmbrOfStepInGait THEN
GaitStep = 1
ENDIF
ENDIF
return
;--------------------------------------------------------------------
;[GETSINCOS] Get the sinus and cosinus from the angle +/- multiple circles
;AngleDeg - Input Angle in degrees
;sinA - Output Sinus of AngleDeg
;cosA - Output Cosinus of AngleDeg
GetSinCos [AngleDeg]
;Get the absolute value of AngleDeg
IF AngleDeg < 0.0 THEN
ABSAngleDeg = AngleDeg *-1.0
ELSE
ABSAngleDeg = AngleDeg
ENDIF
;Shift rotation to a full circle of 360 deg -> AngleDeg // 360
IF AngleDeg < 0.0 THEN ;Negative values
AngleDeg = 360.0-(ABSAngleDeg-TOFLOAT(360*(TOINT(ABSAngleDeg/360.0))))
ELSE ;Positive values
AngleDeg = ABSAngleDeg-TOFLOAT(360*(TOINT(ABSAngleDeg/360.0)))
ENDIF
IF AngleDeg < 180.0 THEN ;Angle between 0 and 180
;Subtract 90 to shift range
AngleDeg = AngleDeg -90.0
;Convert degree to radials
AngleRad = (AngleDeg*3.141592)/180.0
sinA = FCOS(AngleRad) ;Sin o to 180 deg = cos(Angle Rad - 90deg)
cosA = -FSIN(AngleRad) ;Cos 0 to 180 deg = -sin(Angle Rad - 90deg)
ELSE ;Angle between 180 and 360
;Subtract 270 to shift range
AngleDeg = AngleDeg -270.0
;Convert degree to radials
AngleRad = (AngleDeg*3.141592)/180.0
sinA = -FCOS(AngleRad) ;Sin 180 to 360 deg = -cos(Angle Rad - 270deg)
cosA = FSIN(AngleRad) ;Cos 180 to 360 deg = sin(Angle Rad - 270deg)
ENDIF
return
;--------------------------------------------------------------------
;[BOOGTAN2] Gets the Inverse Tangus from X/Y with the where Y can be zero or negative
;BoogTanX - Input X
;BoogTanY - Input Y
;BoogTan - Output BOOGTAN2(X/Y)
GetBoogTan [BoogTanX, BoogTanY]
IF(BoogTanX = 0) THEN ; X=0 -> 0 or PI
IF(BoogTanY >= 0) THEN
BoogTan = 0.0
ELSE
BoogTan = 3.141592
ENDIF
ELSE
IF(BoogTanY = 0) THEN ; Y=0 -> +/- Pi/2
IF(BoogTanX > 0) THEN
BoogTan = 3.141592 / 2.0
ELSE
BoogTan = -3.141592 / 2.0
ENDIF
ELSE
IF(BoogTanY > 0) THEN ;BOOGTAN(X/Y)
BoogTan = FATAN(TOFLOAT(BoogTanX) / TOFLOAT(BoogTanY))
ELSE
IF(BoogTanX > 0) THEN ;BOOGTAN(X/Y) + PI
BoogTan = FATAN(TOFLOAT(BoogTanX) / TOFLOAT(BoogTanY)) + 3.141592
ELSE ;BOOGTAN(X/Y) - PI
BoogTan = FATAN(TOFLOAT(BoogTanX) / TOFLOAT(BoogTanY)) - 3.141592
ENDIF
ENDIF
ENDIF
ENDIF
return
;--------------------------------------------------------------------
;[BODY INVERSE KINEMATICS]
;BodyRotX - Global Input pitch of the body
;BodyRotY - Global Input rotation of the body
;BodyRotZ - Global Input roll of the body
;RotationY - Input Rotation for the gait
;PosX - Input position of the feet X
;PosZ - Input position of the feet Z
;BodyOffsetX - Input Offset betweeen the body and Coxa X
;BodyOffsetZ - Input Offset betweeen the body and Coxa Z
;RollY - RollY offset, needs to be added to the BodyPosY
;PitchY - PitchY offset, needs to be added to the BodyPosY
;BodyIKPosX - Output Position X of feet with Rotation
;BodyIKPosY - Output Position Y of feet with Rotation
;BodyIKPosZ - Output Position Z of feet with Rotation
BodyIK [PosX, PosZ, PosY, BodyOffsetX, BodyOffsetZ, RotationY]
;Calculating totals from center of the body to the feet
TotalZ = BodyOffsetZ+PosZ
TotalX = BodyOffsetX+PosX
;PosY are equal to a “TotalY”
;Successive global rotation matrix:
;Math shorts for rotation: Alfa (A) = Xrotate, Beta (B) = Zrotate, Gamma (G) = Yrotate
;Sinus Alfa = sinA, cosinus Alfa = cosA. and so on…
;First calculate sinus and cosinus for each rotation:
gosub GetSinCos [TOFLOAT(BodyRotX)]
sinG = sinA
cosG = cosA
gosub GetSinCos [TOFLOAT(BodyRotZ)]
sinB = sinA
cosB = cosA
gosub GetSinCos [TOFLOAT(BodyRotY+RotationY)]
;Calcualtion of rotation matrix:
BodyIKPosX = TotalX-TOINT(TOFLOAT(TotalX)cosAcosB - TOFLOAT(TotalZ)cosBsinA + TOFLOAT(PosY)sinB)
BodyIKPosZ = TotalZ-TOINT(TOFLOAT(TotalX)cosGsinA + TOFLOAT(TotalX)cosAsinBsinG +TOFLOAT(TotalZ)cosAcosG-TOFLOAT(TotalZ)sinAsinBsinG-TOFLOAT(PosY)cosBsinG)
BodyIKPosY = PosY - TOINT(TOFLOAT(TotalX)sinAsinG - TOFLOAT(TotalX)cosAcosGsinB + TOFLOAT(TotalZ)cosAsinG + TOFLOAT(TotalZ)cosGsinA*sinB + TOFLOAT(PosY)cosBcosG)
return
;--------------------------------------------------------------------
;[LEG INVERSE KINEMATICS] Calculates the angles of the tibia and femur for the given position of the feet
;IKFeetPosX - Input position of the Feet X
;IKFeetPosY - Input position of the Feet Y
;IKFeetPosZ - Input Position of the Feet Z
;IKSolution - Output true if the solution is possible
;IKSolutionWarning - Output true if the solution is NEARLY possible
;IKSolutionError - Output true if the solution is NOT possible
;IKFemurAngle - Output Angle of Femur in degrees
;IKTibiaAngle - Output Angle of Tibia in degrees
;IKCoxaAngle - Output Angle of Coxa in degrees
LegIK [IKFeetPosX, IKFeetPosY, IKFeetPosZ]
;Reset all Solution options
IKSolution = FALSE
IKSolutionWarning = FALSE
IKSolutionError = FALSE
;Length between the Coxa and Feet
IKFeetPosXZ = TOINT(FSQRT(TOFLOAT((IKFeetPosX*IKFeetPosX)+(IKFeetPosZ*IKFeetPosZ))))
;IKSW - Length between shoulder and wrist
IKSW = FSQRT(TOFLOAT(((IKFeetPosXZ-CoxaLength)*(IKFeetPosXZ-CoxaLength))+(IKFeetPosY*IKFeetPosY)))
;IKA1 - Angle between SW line and the ground in rad
gosub GetBoogTan [IKFeetPosXZ-CoxaLength, IKFeetPosY]
IKA1 = BoogTan
;IKA2 - ?
IKA2 = FACOS((TOFLOAT((FemurLength*FemurLength) - (TibiaLength*TibiaLength)) + (IKSW*IKSW)) / (TOFLOAT(2*Femurlength) * IKSW))
;IKFemurAngle
IKFemurAngle = (TOINT(((IKA1 + IKA2) * 180.0) / 3.141592)*-1)+90
;IKTibiaAngle
IKTibiaAngle = (90-TOINT(((FACOS((TOFLOAT((FemurLength*FemurLength) + (TibiaLength*TibiaLength)) - (IKSW*IKSW)) / TOFLOAT(2*Femurlength*TibiaLength)))*180.0) / 3.141592)) * -1
;IKCoxaAngle
gosub GetBoogTan [IKFeetPosZ, IKFeetPosX]
IKCoxaAngle = TOINT((BoogTan*180.0) / 3.141592)
;Set the Solution quality
IF(IKSW < TOFLOAT(FemurLength+TibiaLength-30)) THEN
IKSolution = TRUE
ELSE
IF(IKSW < TOFLOAT(FemurLength+TibiaLength)) THEN
IKSolutionWarning = TRUE
ELSE
IKSolutionError = TRUE
ENDIF
ENDIF
return
;--------------------------------------------------------------------
;[CHECK ANGLES] Checks the mechanical limits of the servos
CheckAngles:
RFCoxaAngle = (RFCoxaAngle min RFCoxa_MIN) max RFCoxa_MAX
RFFemurAngle = (RFFemurAngle min RFFemur_MIN) max RFFemur_MAX
RFTibiaAngle = (RFTibiaAngle min RFTibia_MIN) max RFTibia_MAX
RMCoxaAngle = (RMCoxaAngle min RMCoxa_MIN) max RMCoxa_MAX
RMFemurAngle = (RMFemurAngle min RMFemur_MIN) max RMFemur_MAX
RMTibiaAngle = (RMTibiaAngle min RMTibia_MIN) max RMTibia_MAX
RRCoxaAngle = (RRCoxaAngle min RRCoxa_MIN) max RRCoxa_MAX
RRFemurAngle = (RRFemurAngle min RRFemur_MIN) max RRFemur_MAX
RRTibiaAngle = (RRTibiaAngle min RRTibia_MIN) max RRTibia_MAX
LFCoxaAngle = (LFCoxaAngle min LFCoxa_MIN) max LFCoxa_MAX
LFFemurAngle = (LFFemurAngle min LFFemur_MIN) max LFFemur_MAX
LFTibiaAngle = (LFTibiaAngle min LFTibia_MIN) max LFTibia_MAX
LMCoxaAngle = (LMCoxaAngle min LMCoxa_MIN) max LMCoxa_MAX
LMFemurAngle = (LMFemurAngle min LMFemur_MIN) max LMFemur_MAX
LMTibiaAngle = (LMTibiaAngle min LMTibia_MIN) max LMTibia_MAX
LRCoxaAngle = (LRCoxaAngle min LRCoxa_MIN) max LRCoxa_MAX
LRFemurAngle = (LRFemurAngle min LRFemur_MIN) max LRFemur_MAX
LRTibiaAngle = (LRTibiaAngle min LRTibia_MIN) max LRTibia_MAX
return
;--------------------------------------------------------------------
;[SERVO DRIVER] Updates the positions of the servos
ServoDriver:
;Wait for previous commands to be completed
SSCWait:
serout SSC_OUT,SSC_BAUTE,“Q”,13]
serin SSC_IN, SSC_BAUTE,[SSCDone]
IF SSCDone <> “.” THEN
goto SSCWait
ENDIF
serout SSC_OUT,SSC_BAUTE,"#",dec RFCoxaPin,“P”,dec TOINT(TOFLOAT(-RFCoxaAngle +90)/0.10588238)+RFCoxaOffset, |
“#”,dec RFFemurPin,“P”,dec TOINT(TOFLOAT(-RFFemurAngle+90)/0.10588238)+RFFemurOffset,|
“#”,dec RFTibiaPin,“P”,dec TOINT(TOFLOAT(-RFTibiaAngle+90)/0.10588238)+RFTibiaOffset,|
“#”,dec RMCoxaPin,“P”,dec TOINT(TOFLOAT(-RMCoxaAngle +90)/0.10588238)+RMCoxaOffset,|
“#”,dec RMFemurPin,“P”,dec TOINT(TOFLOAT(-RMFemurAngle+90)/0.10588238)+RMFemurOffset,|
“#”,dec RMTibiaPin,“P”,dec TOINT(TOFLOAT(-RMTibiaAngle+90)/0.10588238)+RMTibiaOffset,|
“#”,dec RRCoxaPin,“P”,dec TOINT(TOFLOAT(-RRCoxaAngle +90)/0.10588238)+RRCoxaOffset,|
“#”,dec RRFemurPin,“P”,dec TOINT(TOFLOAT(-RRFemurAngle+90)/0.10588238)+RRFemurOffset,|
“#”,dec RRTibiaPin,“P”,dec TOINT(TOFLOAT(-RRTibiaAngle+90)/0.10588238)+RRTibiaOffset,|
“#”,dec LFCoxaPin,“P”,dec TOINT(TOFLOAT(LFCoxaAngle +90)/0.10588238)+LFCoxaOffset,|
“#”,dec LFFemurPin,“P”,dec TOINT(TOFLOAT(LFFemurAngle+90)/0.10588238)+LFFemurOffset,|
“#”,dec LFTibiaPin ,“P”,dec TOINT(TOFLOAT(LFTibiaAngle+90)/0.10588238)+LFTibiaOffset,|
“#”,dec LMCoxaPin,“P”,dec TOINT(TOFLOAT(LMCoxaAngle +90)/0.10588238)+LMCoxaOffset,|
“#”,dec LMFemurPin,“P”,dec TOINT(TOFLOAT(LMFemurAngle+90)/0.10588238)+LMFemurOffset,|
“#”,dec LMTibiaPin,“P”,dec TOINT(TOFLOAT(LMTibiaAngle+90)/0.10588238)+LMTibiaOffset,|
“#”,dec LRCoxaPin,“P”,dec TOINT(TOFLOAT(LRCoxaAngle +90)/0.10588238)+LRCoxaOffset,|
“#”,dec LRFemurPin,“P”,dec TOINT(TOFLOAT(LRFemurAngle+90)/0.10588238)+LRFemurOffset,|
“#”,dec LRTibiaPin,“P”,dec TOINT(TOFLOAT(LRTibiaAngle+90)/0.10588238)+LRTibiaOffset,|
“T”,dec 40*GaitSpeed,13]
;debug:
;serout S_OUT, i9600, "RFCoxa = ",sdec RFCoxaAngle, " RMcoxa= “, sdec RMCoxaAngle,” pwm: “,dec TOINT(TOFLOAT(-RMCoxaAngle +90)/0.10588238)+650+RMCoxaOffset,” RRCoxa = ",sdec RRCoxaAngle,13]
;serout S_OUT, i9600, “LFCoxa = “,sdec LFCoxaAngle, " LMcoxa= “, sdec LMCoxaAngle,” pwm: " ,dec TOINT(TOFLOAT(LMCoxaAngle +90)/0.10588238)+650+LMCoxaOffset,” LRCoxa = “,sdec LRCoxaAngle,13]
return
;--------------------------------------------------------------------
;[FREE SERVOS] Frees all the servos
FreeServos
for Index = 0 to 31
serout SSC_OUT,SSC_BAUTE,”#”,DEC Index,“P0”]
next
serout SSC_OUT,SSC_BAUTE,[13]
return
;--------------------------------------------------------------------
;GP player
GPPlayer
if GPIsPlaying = FALSE then
serout SSC_OUT,SSC_BAUTE,“PL0 SQ0 SM100 IX0 PA0 ONCE”,13]
GPIsPlaying = TRUE
else
serout SSC_OUT,SSC_BAUTE,“QPL0”,13]
serin SSC_IN, SSC_BAUTE,[GPByte0,GPByte1,GPByte2,GPByte3]
;debug:
serout S_OUT, i9600, "SQPlayed: “,dec GPByte0,” IX: “, dec GPByte1,” -> “,dec GPByte2,” Remaining time: ",dec GPByte3, 13]
IF GPByte0 = 255 THEN
GPIsPlaying = FALSE
GPModeON = FALSE
endif
endif
return
[/code]