Hold and Rotations for RobotMesh

#1

We are using RobotMesh, Python for V5. We set the controller up using Controller Express and are now trying to modify the code to to meet our needs. Two things we want it to do are first hold the “Lift” in a position when a button is not pressed. Second, we want it to move a “flipper” motor three revolutions when we press the button. Please let us know if you can help:

Thanks!!

while True:
	leftfrontdrive_power = 0
	rightfrontdrive_power = 0
	leftbackdrive_power = 0
	rightbackdrive_power = 0
	lift_power = 0
	Flipper_power = 0
	puncher_power = 0

	# buttonL1: Forward
	if con.buttonL1.pressing():
		lift_power = 50
	
	# buttonL2: Reverse
	if con.buttonL2.pressing():
		lift_power = -50

	# buttonR2: Reverse
	if con.buttonR2.pressing():
		Flipper_power = -50
	
	# buttonB: Reverse
	if con.buttonB.pressing():
		puncher_power = -100
	
	leftfrontdrive.spin(vex.DirectionType.FWD, leftfrontdrive_power)
	rightfrontdrive.spin(vex.DirectionType.FWD, rightfrontdrive_power)
	leftbackdrive.spin(vex.DirectionType.FWD, leftbackdrive_power)
	rightbackdrive.spin(vex.DirectionType.FWD, rightbackdrive_power)
	lift.spin(vex.DirectionType.FWD, lift_power)
	Flipper.spin(vex.DirectionType.FWD, Flipper_power)
0 Likes

#2

The only change to the code here to make the lift motor hold is to set the default braking mode to “hold” somewhere before this while loop:


lift.set_stopping(vex.BrakeType.HOLD)

. When a motor is told to spin at 0 velocity, it will use its default braking mode.

As for going a fixed number of revolutions, that depends on whether it is to a fixed position or a fixed distance. If you are going to a fixed position, a startRotateTo like this example would work. For a fixed distance, you would need to keep track of the last state of the button, then use startRotateFor if the button was pressed and it had not previously been pressed, along these lines:

lastPressed = false
while True:
  if buttonIsPressed() && !lastPressed:
    motor.startRotateFor(blahblahblah)
  lastPressed = buttonIsPressed()
0 Likes

#3

Thanks Rick- we continue to get two errors. First, we get an “invalid port handle” when downloading. Second, the program dumps out after 1 second. Below is the entire code.

import vex

#region config
brain            = vex.Brain()
rightffrontdrive = vex.Motor(vex.Ports.PORT1, vex.GearSetting.RATIO18_1, True)
leftfrontdrive   = vex.Motor(vex.Ports.PORT2, vex.GearSetting.RATIO18_1, False)
rightbackdrive   = vex.Motor(vex.Ports.PORT3, vex.GearSetting.RATIO18_1, True)
leftbackdrive    = vex.Motor(vex.Ports.PORT4, vex.GearSetting.RATIO18_1, False)
lift             = vex.Motor(vex.Ports.PORT5, vex.GearSetting.RATIO18_1, False)
puncher          = vex.Motor(vex.Ports.PORT6, vex.GearSetting.RATIO36_1, False)
flipper          = vex.Motor(vex.Ports.PORT7, vex.GearSetting.RATIO18_1, False)
con              = vex.Controller(vex.ControllerType.PRIMARY)
#endregion config

con.set_deadband(5)
lift.set_stopping(vex.BrakeType.BRAKE)

#region actions
while True:
	leftfrontdrive_power = 0
	rightfrontdrive_power = 0
	leftbackdrive_power = 0
	rightbackdrive_power = 0
	lift_power = 0
	Flipper_power = 0
	puncher_power = 0
	
	# axis3: Arcade Drive
	move = con.axis3.position()
	steer = con.axis4.position()
	if move != 0 or steer != 0:
		leftfrontdrive_power = move + steer
	if move != 0 or steer != 0:
		rightfrontdrive_power = move - steer
	
	
	# axis3: Arcade Drive
	move = con.axis3.position()
	steer = con.axis4.position()
	if move != 0 or steer != 0:
		leftbackdrive_power = move + steer
	if move != 0 or steer != 0:
		rightbackdrive_power = move - steer
	
	
	# buttonL1: Forward
	if con.buttonL1.pressing():
		lift_power = 50
		
	
	# buttonL2: Reverse
	if con.buttonL2.pressing():
		lift_power = -50
	
	# buttonR1: Forward
	if con.buttonR1.pressing():
		Flipper_power = 50
	
	# buttonR2: Reverse
	if con.buttonR2.pressing():
		Flipper_power = -50
	
	# buttonB: Reverse
	if con.buttonB.pressing():
		puncher_power = -100
	
	leftfrontdrive.spin(vex.DirectionType.FWD, leftfrontdrive_power)
	rightfrontdrive.spin(vex.DirectionType.FWD, rightfrontdrive_power)
	leftbackdrive.spin(vex.DirectionType.FWD, leftbackdrive_power)
	rightbackdrive.spin(vex.DirectionType.FWD, rightbackdrive_power)
	lift.spin(vex.DirectionType.FWD, lift_power)
	Flipper.spin(vex.DirectionType.FWD, Flipper_power)
	puncher.spin(vex.DirectionType.FWD, puncher_power)
0 Likes

#4

“Invalid Port Handle” usually means something else is monopolizing the robot’s communications port. This could be VCS, another tab of Robot Mesh Studio, etc. Make sure VCS is closed and no other RMS stuff is trying to talk to the robot before downloading.

As for “dumps out after 1 second”, I would need the actual error message, but I suspect it’s where you capitalize


Flipper

on the second-to-last line when it is declared as


flipper

in the config region. It looks like someone changed its name without changing all of the references to it.

(And for future reference, if you highlight your code and wrap it in code tags with the </> button at the top right, it will preserve your code’s formatting. Which is important for Python.)

0 Likes