Canada
Asked — Edited

4-In-1 Sensor Questions

I am attempting to get a 4-in-1 sensor to work as I need it to and not have much success - I need a hand. I am trying to get the 4- in-1 sensor to drive 2 servos to keep something level/plumb. I have the servos set to go up or down depending on the sensor readings. The problems I'm having are;

  1. It will kind of work for a short time but will only drive one servo at a time even if both should move,
  2. The Y axis stops working after a few minutes,
  3. Then everything stops working,
  4. For some reason the 'Sleep' command above the 'loop' is run each time and anything else put there -except the first line. I am running the sensor on a EZBv4/2. My code is a modified version of one from a JD project. Here's my code;

ControlCommand("MPU9150", Init)

sleep(1000)

:loop
#Added these because $AccelX and $AccelY were not recognized later in the script for some reason.
$XAccel = $AccelX
$YAccel = $AccelY

if ("x" =0)

print(1)  

sleep(2000)
  
goto(loop)
  
endif 
  
ControlCommand("MPU9150", RunOnce)

print("x: " + $XAccel)
print("y: " + $YAccel)

if ($XAccel > 1000)
ServoUp(D1,10)
Servospeed(D1,2)
 
ELSEif ($XAccel  < -1000)
ServoDown(D3,10)
Servospeed(D3,2)
Endif

if ($YAccel > 1000)
ServoUp(D3,10)
Servospeed(D3,2) 
  
ELSEif ($YAccel < -1000)
ServoDown(D3,10)
Servospeed(D3,2) 
Sleep(500)

endif  
sleep(1500)

goto(loop)

Has anyone played with these sensors much and got them working? I know I have a lot to learn about them so your assistance will be appreciated.
Thanks


ARC Pro

Upgrade to ARC Pro

Take control of your robot's destiny by subscribing to Synthiam ARC Pro, and watch it evolve into a versatile and responsive machine.

#1  

Sounds as though maybe your servo’s position is not being updated, or too much load on the servo?

Are you using digital or analogue servos?

I know the ezb will work with both, but digital would be much better, hope you find the problem soon. : )

#2  

Hi @bhouston,

My guess is the way you state your servo speeds? From the Script help section:


Once the ServoSpeed() has been initialized the first time, specify the ServoSpeed() before specifying the Servo() position.

Your stating your servo speeds "after" you set your position.

PRO
USA
#3  

@bhouston,

What you have in mind ?
Are you trying to detect motion or track the orientation ?

Your code reads the IMU every 1.5 seconds. A lot of things can happen in 1500 milliseconds.

#4  

@Newagetomy, I'm using EZ Robot HD servos @Dave, I'll clean that up, thanks. @ptp, I am trying to detect motion. The robot starts out level, If the robot is leaned to the left or the right or to the front or back, that will be detected. Then the servos will move the axis effected back to level. I realize that 1500 milliseconds is a long time- I'm just trying to get it to work, then I'll shorten the time.

#5  

I've been working on this again today. If I run this code, it works great;


Servospeed(D1,2)
 
ControlCommand("MPU9150", Init)

sleep(500)

:loop
#Added these because $AccelX and $AccelY were not recognized later in the script for some reason.
$XAccel = $AccelX


if ("x" =0)

print(1)  

sleep(2000)
  
goto(loop)
  
endif 
  
ControlCommand("MPU9150", RunOnce)

print("x: " + $XAccel)
#print("y: " + $YAccel)

if ($XAccel > 1000)
ServoUp(D1,15)
 
ELSEif ($XAccel < -1000)
ServoDown(D1,15)
 
endif

sleep(100)
Goto(loop)

But I need 2 servos to run in the code. If I run this code;


Servospeed(D1,2)
Servospeed(D3,2) 
ControlCommand("MPU9150", Init)

sleep(500)

:loop
#Added these because $AccelX and $AccelY were not recognized later in the script for some reason.
$XAccel = $AccelX
$YAccel = $AccelY

if ("x" =0)

print(1)  

sleep(2000)
  
goto(loop)
  
endif 
  
ControlCommand("MPU9150", RunOnce)

print("x: " + $XAccel)
print("y: " + $YAccel)

if ($XAccel > 1000)
ServoUp(D1,15)

 
ELSEif ($XAccel < -1000)
ServoDown(D1,15)

 
endif
if ($YAccel > 1000)
ServoUp(D3,15)

 
ELSEif ($YAccel < -1000)
ServoDown(D3,15)

 
endif


sleep(100)
Goto(loop)

It does not move either of the 2 servos and the code runs lines of code outside of the 'loop'. This is what that looks like;


2: Servospeed(D3,2)
3: ControlCommand("MPU9150", Init)
5: sleep(500)
7: :loop
9: $XAccel = $AccelX
10: $YAccel = $AccelY
12: if ("x" =0)
20: endif
22: ControlCommand("MPU9150", RunOnce)
24: print("x: " + $XAccel)
> x: -4876
25: print("y: " + $YAccel)
> y: 1543
27: if ($XAccel > 1000)
31: ELSEif ($XAccel < -1000)
32: ServoDown(D1,15)
35: endif
36: if ($YAccel > 1000)
37: ServoUp(D3,15)
40: ELSEif ($YAccel < -1000)
47: sleep(100)
48: Goto(loop)

I still looking for some help before I give up on these sensors. Thanks

PRO
USA
#7  

Bob,

Try the code below and let us know the output:


ServoSpeed(D1,2)
ServoSpeed(D3,2)
ControlCommand("MPU9150", Init)
Sleep(500)

$movex_counter = 0
$movey_counter = 0
$debug_counter = 0

:loop
        $AccelX=0
        $AccelY=0
	ControlCommand("MPU9150", RunOnce)

	if ($AccelX>1000)
		ServoUp(D1,15)
		$movex_counter=$movex_counter+1
		Print("AccelX: " + $AccelX)
		Print("D1: " + GetServo(D1))
	elseif ($AccelX<-1000)
		ServoDown(D1,15)
		$movex_counter=$movex_counter+1
		Print("AccelX: " + $AccelX)
		Print("D1: " + GetServo(D1))
	endif

	if ($AccelY>1000)
		ServoUp(D3,15)
		$movey_counter=$movey_counter+1
		Print("AccelY: " + $AccelY)
		Print("D3: " + GetServo(D3))
	elseif ($AccelY<-1000)
		ServoDown(D3,15)
		$movey_counter=$movey_counter+1
		Print("AccelY: " + $AccelY)
		Print("D3: " + GetServo(D3))
	endif

	$debug_counter=$debug_counter+1

	#print debug msg every 2 seconds
	if ($debug_counter>=20)
		$debug_counter=0
		Print("movex_counter: " + $movex_counter)
		Print("movey_counter: " + $movey_counter)
	endif
	
	Sleep(100)
GoTo(loop)

#8  

@DJ, I'll try that. @ptp, Here's a snipet of what I get;


1: ServoSpeed(D1,2)
2: ServoSpeed(D3,2)
3: ControlCommand("MPU9150", Init)
4: Sleep(500)
6: $movex_counter = 0
7: $movey_counter = 0
8: $debug_counter = 0
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7170
24: Print("D1: " + GetServo(D1))
> D1: -225
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1573
31: Print("D3: " + GetServo(D3))
> D3: 150
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7131
24: Print("D1: " + GetServo(D1))
> D1: -240
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1587
31: Print("D3: " + GetServo(D3))
> D3: 165
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7208
24: Print("D1: " + GetServo(D1))
> D1: -255
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1541
31: Print("D3: " + GetServo(D3))
> D3: 180
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7162
24: Print("D1: " + GetServo(D1))
> D1: -270
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1575
31: Print("D3: " + GetServo(D3))
> D3: 195
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7142
24: Print("D1: " + GetServo(D1))
> D1: -285
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1567
31: Print("D3: " + GetServo(D3))
> D3: 210
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7136
24: Print("D1: " + GetServo(D1))
> D1: -300
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1581
31: Print("D3: " + GetServo(D3))
> D3: 225
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7135
24: Print("D1: " + GetServo(D1))
> D1: -315
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1582
31: Print("D3: " + GetServo(D3))
> D3: 240
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7144
24: Print("D1: " + GetServo(D1))
> D1: -330
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1577
31: Print("D3: " + GetServo(D3))
> D3: 255
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7154
24: Print("D1: " + GetServo(D1))
> D1: -345
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1575
31: Print("D3: " + GetServo(D3))
> D3: 270
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7149
24: Print("D1: " + GetServo(D1))
> D1: -360
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1560
31: Print("D3: " + GetServo(D3))
> D3: 285
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7168
24: Print("D1: " + GetServo(D1))
> D1: -375
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1565
31: Print("D3: " + GetServo(D3))
> D3: 300
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)
10: :loop
11: $AccelX=0
12: $AccelY=0
13: ControlCommand("MPU9150", RunOnce)
15: if ($AccelX>500)
20: elseif ($AccelX<-500)
21: ServoDown(D1,15)
22: $movex_counter=$movex_counter+1
23: Print("AccelX: " + $AccelX)
> AccelX: -7134
24: Print("D1: " + GetServo(D1))
> D1: -390
25: endif
27: if ($AccelY>500)
28: ServoUp(D3,15)
29: $movey_counter=$movey_counter+1
30: Print("AccelY: " + $AccelY)
> AccelY: 1564
31: Print("D3: " + GetServo(D3))
> D3: 315
32: elseif ($AccelY<-500)
39: $debug_counter=$debug_counter+1
42: if ($debug_counter>=20)
46: endif
48: Sleep(100)
49: GoTo(loop)

I worked for a short time then quit working