Editing the Code in Basic Micro Studio

Hey guys :slight_smile:

Im editing the code for my hexapod to include some new functions and changing some of the buttons around, but I am not quite sure where to start. Ill describe what I would like my robot to do, and link some of the code I think needs to change. If anyone could give me a hand, or point me to another topic where this is covered (I did a search, but couldnt find anything relevant) I would be very appreciative.

So, the next big coding problem I have is… coding the Left analog stick to pan and tilt the turret-torso.

Currently, the Left Analog Stick code looks like this:

[code]Steering = DualShock(5) - 128 ; Left Stick Horizontal

StepFlag = (GaitSpeedTmp - 2) max 3
if HeightAdjust or LockLegs then
	Height = LastHeight + (((DualShock(6) - 128) / 5) - LastHeight) / StepFlag; Left Stick Vertical
endif

Height = (Height Max HeightLimit) min - HeightLimit

;H3
if Steering or (Height <> LastHeight) or YSpeed or XSpeed or LockLegs then
MovesDelay = 8
endif

if MovesDelay > 0 then
	LastHeight = Height
	DCoord = SQR(XSpeed * XSpeed + YSpeed * YSpeed)
	TmpCos = XSpeed * 127 / DCoord
	gosub ACos
	DAngle = TmpAngle
	if YSpeed > 0 then
		DAngle = 256 - DAngle
	endif[/code]

I want to change this code so that instead of rotating left and right with the Left Stick Horizontal, I want it to be rotated left when pushing L1 and right when pushing R1.

Currently the code for L1 and R1 is this:

if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then ;R1 Button test Height = 0 ; average BH, low LUS, max speed LegUpShift = 20 GaitSpeed = 3 elseif (DualShock(2).bit2 = 0) and LastButton(1).bit2 ;L1 Button test Height = 5 ; tile floor LegUpShift = 30 GaitSpeed = 3

The new function for Left Stick Horizontal, I want to turn Servo X left when pushing left, and right when pushing right. The new function for Left Stick Vertical, I want to turn Servo Y left when pushing up, and right when pushing down. What I need to do as well, however, is make sure the servos dont turn too far and break, so there would be need to be a maximum limit for the servos.

Ill add the full code at the bottom of this post, if you need to see the constants as well. (on second thoughts, its way too big - I will add it as an attachment instead)

Ill be putting the panning servo on Pin 31, as detailed in this code:

; Deck Pin DeckP Con "3" ;Deck Panning servo : pin 31 DeckP2 Con "1"

and the Tilt servo on Pin 28, as detailed in this code:

; Additional Tilt servo for 180° Deck DeckTilt Con "2" DeckTilt2 Con "8"

Ill show the example of the max limits that I found, i suspect ill need to copy this for panning and tilting limits - I would like to start off with the maximum limits of the servos, and then cut down the maximum limits after experimentation with the actual robot.

[code];Deck
Deck_PulseMin con 600
Deck_PulseMax con 2400

;Little Gripper
LGripLR_PulseMin con 750
LGripLR_PulseMax con 2250
LGripOC_PulseMin con 750
LGripOC_PulseMax con 2250

;Legs
HipH_AngleMin con 21 ;30°
HipH_AngleMax con 107 ;150°
HipH_PulseMin con 910
HipH_PulseMax con 2090

HipV_AngleMin con 25 ;35°
HipV_AngleMax con 103 ;145°
HipV_PulseMin con 960
HipV_PulseMax con 2040

Knee_AngleMin con 36 ;50°
Knee_AngleMax con 107 ;150°
Knee_PulseMin con 1107
Knee_PulseMax con 2090[/code]

Anyway, I hope you guys can help me understand this, as I am struggling a little trying to figure it out myself, and dont want to accidentally “break” the code by putting the wrong stuff in.

The servos I currently have have a range of 180 degrees.
zeus-toshisedit.rar (6.91 KB)

As for the programming changes, It should not be too difficult, but it is probably something that you would need to tinker with and refine, which may take awhile to get it to where you want. There are so many options on how the code runs. I don’t have time now to try to make your complete set of changes, but will try to give some hints on what you might try. The format of the SSC-32 commands are in the manual: lynxmotion.com/images/html/build136.htm

It basically says, move this servo (#) from it’s current location, to a new location§ (pulse width) and get there in the time (T). All of the ASCII commands like this must be terminated by a CR (13). The command also allows you to specify multiple servos/positions in the same command.

Using L1/R1 buttons to turn. You can do. Often you would keep a variable in this case: Steering
You could test for R1 being down and probably do something like:

if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then   ;R1 Button test
    if Steering < 127
        Steering = Steering + 1
    endif
endif

Likewise for L1. (Note may have them reversed, that is maybe R1 should subtract… ) Again lots of options here. Should you only change the value by 1? Do you only want it change once when you push the button, like it is coded above or do you want it to keep turning when the button is down. If the later you would remove the LastButton(1).bit3 test. But then it may turn too quickly so then maybe you would need to add a test for delta time. That is only increment/decrement if the time since my last increment/decrement exceeded some time threshold… (You will probably not need this)

Then for moving your servo, you could choose to do it the same as steering is done now where the value to move to is directly set to where that joystick is at that time. The values could be in some special number space or could simply be kept in servo pulse widths. For example if you knew that you wanted the servo to move through the range 1000-2000 centered at 1500 you could do something like:
DeckPos=1000 + ((2000-1000)*(DualShock(5))/255 ; Left Stick Horizontal

You could output this value directly each time, you could/should choose to limit how fast it rotates by using the speed or time parameters for the SSC-32 command. With the above approach this would have the effect that when you release the stick the servo would jump back to the center position, which may or may not be what you want. You may instead want the Deck to stay where you move it with moving the stick one direction would rotate one direction and moving the stick the other direction would rotate the other… In which case you may do something like:

if DualShock(5) > (128+cTravelDeadZone) then
    DeckPos = DeckPos + (DualShock(5)-128)
    if DeckPos > DECKPOSMAX then
        DeckPos = DECKPOSMAX
    endif
elseif... (handle the other)

Again options here. Maybe you need to factor how much you add/subtract. That is Maybe adding/subtracting up to 128 per iteration is too much and you should factor it, by dividing it by 2 or 3 or 4…

Hope that helps. Good Luck
Kurt

You are my Emperor of code Kurte!

bows deeply

I really appreciate the link, I was looking for something like that but couldnt locate it :S Ill have a good read of your suggestions and try and nut them out :slight_smile:

Thanks heaps :slight_smile:

So ive been having fun playing with the code so far, this is my current version im about to test:

If LockLegs then NoPresets if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then ;R1 Button rotate right if Steering < 127 Steering = Steering + 1 endif elseif (DualShock(2).bit2 = 0) and LastButton(1).bit2 ;L1 Button rotate left if Steering > 127 Steering = Steering - 1 endif elseif (DualShock(2).bit1 = 0) and LastButton(1).bit1 ;R2 Button test Height = 0 ; average BH, high LUS, max speed - high legs LegUpShift = 70 GaitSpeed = 3 elseif (DualShock(2).bit0 = 0) and LastButton(1).bit0 ;L2 Button test Height = 0 ; average BH, average LUS, max speed - fastest speed! LegUpShift = 45 GaitSpeed = 3 else goto NoPresets endif

Ill let you know if it is successful :slight_smile:

EDIT: Not so much, “Error: FILE C:\DOCUMENTS AND SETTINGS\TOSHI\DESKTOP\ZEUS-TOSHISEDITV2.BAS(LINE 395) : [TOKEN :] : Expected THEN” line in question: if Steering < 127

I tried adding then in front of this line, but that didnt fix it either.

EDIT2:

So, Ive carried on and started experimenting with the pan and tilt of the deck and turret, but first I have to get rid of the automated pan and tilt function in the code. I have deleted this code here:

[code]if Steering or (Height <> LastHeight) or YSpeed or XSpeed or LockLegs then
MovesDelay = 8
endif

if MovesDelay > 0 then
	LastHeight = Height
	DCoord = SQR(XSpeed * XSpeed + YSpeed * YSpeed)
	TmpCos = XSpeed * 127 / DCoord
	gosub ACos
	DAngle = TmpAngle
	if YSpeed > 0 then
		DAngle = 256 - DAngle
	endif

	DCoord = DCoord max (128 - ABS(Height) - ABS(TibiaAngle * 2))

	; 180° Deck Code
	if (Dangle < 64 or Dangle > 191) and (DCoord > 0) then
		DeckTilt_Pulse = 1680 - Dcoord
	else
		DeckTilt_Pulse = 1380 + Dcoord
	endif
	Deck_Pulse =  ((Deck_PulseMax - Deck_PulseMin) * ((DAngle + 64 ) & $7F)) / 127 + Deck_PulseMin

	; 360° Deck Code
	;Deck_Pulse = ((Deck_PulseMax - Deck_PulseMin) * DAngle) / 255 + Deck_PulseMin[/code]

Which, I believe, is all the Pan code (deck turning) - im not sure if there is a seperate part for the tilt code somewhere else which i will also need to get rid of.

EDIT3:

Well im still figuring stuff out, but I believe I want the code looking something like this: (ive decided the left joystick will control translation movement, and the right joystick will control pan and tilt of the turret)
The pan servo is number 31, range 1000-2000 and midpoint 1500

if DualShock(3) < 120
   then "#31P1100S200,13"
if DualShock(3) > 136
   then "#31P1900S200,13"

The problem I have here is that this means that the servo will continue moving to Position 1100 until it gets further instructions, yes? What do I have to do to make it move in small increments, based on how long I hold the dualshock joystick for? (so, it slowly moves left while I hold the joystick, then stays at its position when I let go)

I would assume the same goes for the tilt servo on pin 28:

if DualShock(4) <120
   then "#28P1100S200,13"
if DualShock(4) >136
   then "#28P1900S200,13"

May I recommend you try simpler code examples before trying to dive into this code? There is a syntax manual built into the Studio program. There are examples of how “if then” statements are formatted. There are several different acceptable formats.

You need to have the “then” commands on the same line as the if statement. You also don’t have endifs. For example:

if DualShock(4) <120 then "#28P1100S200,13" if DualShock(4) >136 then "#28P1900S200,13"This will not compile.

if DualShock(4) <120 then "#28P1100S200,13" endif if DualShock(4) >136 then "#28P1900S200,13" endifBut this… still won’t.

Since it’s not setting anything to anything else or executing a command, it still won’t compile. You’ll be a bit closer to something that will compile, however.

Try something like this instead, replacing SSC32PIN and SSCBAUD with relevant values.

if DualShock(4) <120 then serout SSC32PIN,SSCBAUD,"#28P1100S200",13] endif if DualShock(4) >136 then serout SSC32PIN,SSCBAUD,"#28P1900S200",13] endif

Haha, im confused - not about the code, its seeming a lot clearer now.

Are James “Dont call me Jim” Frye and Jim Frye the same person?

Anyway, thanks for the replies, I know im jumping in the deep end with this level of coding, but im a fast learner and with a few hints I should be able to get a hang of it pretty quickly. Ive checked what you said and found the Synxtax manual, which is very interesting.

Thanks for clarifying that part of the code, It was my bad that I forgot you had to state the baudrate and the SSC32 pin, I had done it with an earlier piece of code and missed it when writing this part.

Ill continue working on writing it, and let you know how i get on.

Thanks!

Nope. We’re completely different people! :smiley:

Good that you’re beginning to understand. Just let us know if you need any more help.