timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Scratch servo control

Sat Feb 20, 2016 5:10 am

I've been experimenting with servo control from Scratch. WiringPi isn't a suitable way to do it (at least, not its current incarnation) but ServoBlaster appears to be a good bet.

I've added "servoXpositionY" and "servoXpercentageY" commands and it seems to work perfectly well. What isn't so nice is the way one has to configure it, start it, stop it, and cope with the way it grabs gpio pins possibly clashing with the rest of your scripts. The default config takes over 8 pins, for example, and trying to do anything else with them from within Scratch can lead to assorted problems.

I'd be interested to hear of any ideas people have on how to deal with this - or even if I should worry about it. If it all works out ok it should make the next release.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch servo control

Sat Feb 20, 2016 12:34 pm

Great work, Tim !! I'm afraid you're well above my skill level, so I'm not sure what you mean by the servo taking over 8 pins - how, why, huh !? So, nothing useful to add, just thought I'd pop up to show my appreciation, especially as I've been giving you a hard time about servos and Scratch.

Thanks.

User avatar
mikronauts
Posts: 2726
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Scratch servo control

Sat Feb 20, 2016 6:08 pm

Tim,
Joan's pigpio works very well with servos and pwm, I use it for my Pi Droid Alpha product.

http://abyz.co.uk/rpi/pigpio/

http://www.mikronauts.com/raspberry-pi/pi-droid-alpha/
timrowledge wrote:I've been experimenting with servo control from Scratch. WiringPi isn't a suitable way to do it (at least, not its current incarnation) but ServoBlaster appears to be a good bet.

I've added "servoXpositionY" and "servoXpercentageY" commands and it seems to work perfectly well. What isn't so nice is the way one has to configure it, start it, stop it, and cope with the way it grabs gpio pins possibly clashing with the rest of your scripts. The default config takes over 8 pins, for example, and trying to do anything else with them from within Scratch can lead to assorted problems.

I'd be interested to hear of any ideas people have on how to deal with this - or even if I should worry about it. If it all works out ok it should make the next release.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch servo control

Sat Feb 27, 2016 1:30 am

mikronauts wrote:Tim,
Joan's pigpio works very well with servos and pwm, I use it for my Pi Droid Alpha product.
Yup, that looks very interesting. Thanks for pointing me to it!
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

Re: Scratch servo control

Sat Feb 27, 2016 10:11 am

It would be nice if you could use the gpiozero library, which has a pigpio extension
That would possibly make a smoother transition moving from scratch to python.
gpiozero

Chris

timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch servo control

Fri Mar 04, 2016 6:11 pm

As far as I can tell gpiozero is completely written in Python which makes it completely unusable by anyone else. Not such a good idea really.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

Re: Scratch servo control

Sun Mar 06, 2016 8:12 am

Thanks for checking it out.

Chris

timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch servo control

Tue Mar 08, 2016 6:13 pm

Just as an update - pigpen is working out very well for servo control. What, 'pigpen'? Damn You Autocorrect! That should, obviously, be 'pigpio'
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
mikronauts
Posts: 2726
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Scratch servo control

Tue Mar 08, 2016 6:19 pm

Please shoot me an email (or PM) once you release it... I'd like to see how easy it would be for my nephew's to program a PiDroidAlpha+RaspberryPi based robot.

http://www.mikronauts.com/raspberry-pi/pi-droid-alpha/
timrowledge wrote:Just as an update - pigpen is working out very well for servo control. What, 'pigpen'? Damn You Autocorrect! That should, obviously, be 'pigpio'
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch servo control

Tue Mar 08, 2016 6:38 pm

External programs can fill the gap till a perfect solution in scratch gpioserver is available.

scratchClient supports servo control with 'build in' DMA support, using a pi2- modification of RPIO.PWM. This lib is included in distribution.
Or use an arduino nano as an adapter board, featuring servo control, io and adc:
http://heppg.de/ikg/wordpress/?p=532

Regards,
Gerhard

anstey
Posts: 1
Joined: Sat Mar 12, 2016 5:06 pm

Re: Scratch servo control

Sat Mar 12, 2016 5:13 pm

I am some way behind the progress made by others on this thread. I am trying to control a servo using scratch running on a raspberry pi. I can see how to control the pwm using the GPIO server. Is there a way to control the duty cycle, or do i need some additional code to support Scratch to o this? Is the current GPIO server on Scratch capable of controlling a servo directly?

thanks

john

timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch servo control

Sun Mar 13, 2016 1:23 am

No, you cannot currently drive servos from the built in gpio server. Next release, maybe, depending on how much time I get to spend on it.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

ikke4
Posts: 27
Joined: Thu Jan 02, 2014 4:18 pm

Re: Scratch servo control

Wed Sep 21, 2016 3:14 pm

Hi,

I want to get some students (12 years) to work with scratch and a single servo. What is the status of this doing it with a micro servo directly connected to the gpio ports.

grt

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch servo control

Wed Sep 21, 2016 5:40 pm

ikke4 wrote:Hi,

I want to get some students (12 years) to work with scratch and a single servo. What is the status of this doing it with a micro servo directly connected to the gpio ports.

grt
NuScratch (the 'built-in' Scratch) does now support servos, but in my experience it seems that the travel is limited to around 60 degrees.

Have you tried ScratchGPIO: http://simplesi.net/scratchgpio/scratch ... rypi-gpio/

This has had servo support for ages now, and work well.

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch servo control

Wed Sep 21, 2016 7:19 pm

Hello,
the servo angle is a device property, most servo have 60 degree. 90 degree or 180 degree or even 360 degree servo are on market.
The control signal are always the same (1ms to 2ms pulse, 50Hz).
Regards,
Gerhard

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch servo control

Wed Sep 21, 2016 8:42 pm

ghp wrote:Hello,
the servo angle is a device property, most servo have 60 degree. 90 degree or 180 degree or even 360 degree servo are on market.
The control signal are always the same (1ms to 2ms pulse, 50Hz).
Regards,
Gerhard
My micro servos, which have a >180 degrees of travel using ScratchGPIO, only have a 50-ish degree of travel under NuScratch :?

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch servo control

Thu Sep 22, 2016 6:04 pm

Hello,
was curious to see what happens. With "servo17%0", I get 1.5 ms pulse width. With "servo17%100", there are 2ms pulses. So I checked -100 with "servo17%-100" and got (drum fill) 1ms.
In scratchClient, I use ranges from 0..100 for outputs. Here in scratch gpioserver, servo use -100 to 100.
Regards,
Gerhard

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch servo control

Thu Sep 22, 2016 7:02 pm

ghp wrote:Hello,
was curious to see what happens. With "servo17%0", I get 1.5 ms pulse width. With "servo17%100", there are 2ms pulses. So I checked -100 with "servo17%-100" and got (drum fill) 1ms.
In scratchClient, I use ranges from 0..100 for outputs. Here in scratch gpioserver, servo use -100 to 100.
Regards,
Gerhard
Ok, so how many degrees of rotation does that give you from -100 to 100 ?

BTW, I'm using cheap 9g MIcro-servos.

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch servo control

Thu Sep 22, 2016 7:34 pm

Hello,
I just connected my good old oscilloscope and checked the signal. When you had only half the rotation expected with values[0..100] or [1.5..2ms], then you will get full rotation angle with values [-100..100] or [1..2ms].
I heard that some servo allow for some 'overdrive'. Pulses smaller than 1ms or larger 2ms could extend the angle range, just to the control electronics' limits. You could use a 0 to 100% pwm signal with 50Hz and check values lower than 5% or larger 10%. Could damage a servo, possibly.
You could work around limited angle by using a different lever. Which rises the question of 'torque', but this could be beyond the scope of a scratch forum ?
Regards,
Gerhard

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Scratch servo control

Thu Sep 22, 2016 7:40 pm

The 9g servos I have accept pulses in the range roughly 600µs to 2400µs to turn through their just over 180 degrees.

timrowledge
Posts: 1286
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch servo control

Tue Sep 27, 2016 12:02 am

Apologies for the delayed response; busy-busy!

I've been flying radio controlled model planes for... 43 years now, gulp... a while, and have always understood the proper signal to range from 1000 to 2000 microseconds with the centre at 1500. If anyone can find me a good authoritative reference explaining why this range should be expanded I'll be happy to revise the code.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Scratch servo control

Tue Sep 27, 2016 8:00 am

That depends on what you think is authoritative.

There are plenty of on-line mentions of ranges between 500µs and 2500µs.

E.g. http://www.robotshop.com/forum/servo-range-t8519

I think the Arduino libraries also allow 500-2500 limits.

The pigpio limits are 500-2500. I don't remember why I chose those limits but there would have been a justifiable reason at the time.

Later on I'll try some 9g servos and see what they accept.

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Scratch servo control

Tue Sep 27, 2016 10:10 am

Just tried a 9g servo.

It is happy with values between 500 and 2500 µs. For those limits it turns about 190 degrees.

Video at http://abyz.co.uk/videos/servo.mp4 (my browser complains that the video is corrupt, you may need to download to view, e.g. wget http://abyz.co.uk/videos/servo.mp4).

Code: Select all

#!/usr/bin/env python

import time
import pigpio
import i2c_lcd

SERVO=4

SERVO_MIN=500
SERVO_MAX=2500
SERVO_INC=100

SERVO_LOW=1400
SERVO_HIGH=1600
servo_low=SERVO_LOW
servo_high=SERVO_HIGH

pi = pigpio.pi()
if not pi.connected:
   exit(0)

lcd = i2c_lcd.lcd(pi, width=20)

count = 1

try:
   while True:

      lcd.put_line(0, "Servo range test")
      lcd.put_line(1, "Low={}".format(servo_low))
      lcd.put_line(2, "High={}".format(servo_high))
      lcd.put_line(3, time.asctime())

      for i in range(3):
         pi.set_servo_pulsewidth(SERVO, servo_low)
         time.sleep(1)
         pi.set_servo_pulsewidth(SERVO, servo_high)
         time.sleep(1)

      servo_low -= SERVO_INC
      servo_high += SERVO_INC

      if servo_low < SERVO_MIN:
          servo_low = SERVO_LOW

      if servo_high > SERVO_MAX:
          servo_high = SERVO_HIGH

except KeyboardInterrupt:
   pass

print("exiting")

lcd.close()
pi.stop()

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch servo control

Tue Sep 27, 2016 6:42 pm

timrowledge wrote:Apologies for the delayed response; busy-busy!

I've been flying radio controlled model planes for... 43 years now, gulp... a while, and have always understood the proper signal to range from 1000 to 2000 microseconds with the centre at 1500. If anyone can find me a good authoritative reference explaining why this range should be expanded I'll be happy to revise the code.
Deja vu...

viewtopic.php?f=77&t=148071

The point is, respectfully, we're not flying model planes. We're teaching 8-year-olds to wave a flag, and 10-year-olds to build a robot arm, using cheap 9g micro-servos.

ghp
Posts: 1418
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch servo control

Thu Sep 29, 2016 4:28 pm

Hello Forris,
I think that it is not a question of what is done with the servo, but what 'the standard' is. When I started to use servo, I looked in the net and found almost always the pulse width [1..2 ms].
scratch is by its nature a general purpose system. To implement all possible deviations for special devices or some vendors would make the commands quite complicated. Especially for kids, the target audience of scratch.

For scratchClient, the framework for hardware connections I maintain, I have started to implement optional parameter for the servo adapters which allow to set millisecond.min and millisecond.max in the configuration files. This will allow operation of servo outside the specs, but also the limitation of sweep range because connected hardware could need it. The config files used are prepared as part of the hardware setup by the teacher, so in school hours kids cannot collect frustration points by misspelling commands or alike. Will be completed the next weeks and the uploaded to the website.

Regards,
Gerhard

Return to “Scratch”