# Marty Learns To Swing – Part 1

Marty is a wonderful walking robot created by Sandy Enoch and brought to many educators, parents and, most importantly, kids around the world by the team at Robotical. I’ve had a Marty since the first crowdfund but he hasn’t been up to much recently so I decided to cajole him into helping me experiment with some simple physics – the physics of the playground swing.

We all know what a playground swing looks like and we also know that a person riding a swing either sits (or stands) on the seat and holds/pulls on to the ropes (or chains) that suspend the seat. A specific set of movements (and timing of those movements) are needed to keep the swing moving and most kids learn pretty quickly what to do after just a few goes. But the physics behind a swing is quite interesting and the tactics required to keep a swing going are fun to discover.

## A Simple Swing

There are a lot of different levels on which the physics of a playground swing can be investigated and understood. To get started let’s look at the simplest of these: the motion of a swing as a pendulum.

In keeping with most clock pendulums (yes this is the correct plural of pendulum according to the Oxford English Dictionary) a swing needs to be set in motion and this is generally done with a push (or a kick-off). Once moving the swing will go backwards and forwards and it will do this at a constant rate. So if we time the swing from a fixed point (say the highest point the seat reaches on one side of its swing) to the same point on the next swing cycle then this is the period of the swing (which we measure in seconds).

An interesting property of a pendulum (or swing) is that the time the swing takes to complete one cycle does not depend on how high you swing (within reasonable limits – it certainly would be different if you went right over the top!). It also doesn’t depend on how heavy the person sitting on the swing is.

In fact the only thing that changes the period of a swing is the length of the ropes holding it up (assuming you stay on the same planet that is).

The following animation of a swing shows the period of swing for different string lengths. Try pressing the Longer String and Shorter String buttons to see how long a pendulum of a particular length takes to swing. It is based on a Wired Article and there is more background physics in that article if you are interested.

## Making a Swing for Marty

When you get on a swing you normally sit but standing on the seat also works. Sitting leaves your lower legs free to pump backwards and forwards and your arms can also pull on the ropes to allow you to lean back as a method to increase the size of the swing. To keep the motion working when standing you need to crouch down and stand up alternately.

Unfortunately though, Marty wasn’t designed to sit as you can see in the photo below and his knees bend sideways making the standing-up pumping action impossible for him. Marty also lacks the ability to pull on ropes as he doesn’t have any elbow joints (and so pulling in a specific direction isn’t possible – he can only pull around the circle centered on his shoulder).

So the best way to get Marty swinging simply is to do what gymnasts do when they swing on rings. In other words we’re going to give Marty a couple of strings to hang onto while he does his swinging.

To get this to work I actually had to disconnect the servo motor cables for Marty’s arm motors. This is because Marty’s motors attempt to keep his joints in a fixed position and when he is hanging by his arms they are being moved slightly by the shifting of his weight. The motors try to counteract this movement if they are connected but they aren’t strong enough to move Marty on their own. So Marty sensibly turns off the power to all the motors to avoid over-stressing himself. Disconnecting the motor cables for the arm motors spares him this unnecessary trouble.

You also need to make sure (after you have disconnected the arm servo motor cables) that Marty has his head screwed on well. The arms are held in place by the top (and face) part of Marty’s head and this is secured to the rest of Marty with two screws (one is shown in the image below and the other is on the opposite side of Marty’s head).

To get Marty swinging, tie two pieces of string to Marty’s hands (I actually removed the hands and tied to the ends of his arms as it was easier and I don’t think he looks too unhappy like this). Then tie the other ends of the strings around a horizontal beam that can be used to suspend Marty so his legs don’t touch the floor (or table). I used strings of about 40 cm total length each and once I’d tied them in place Marty’s head was around 24 cm below the bar that he’s hanging from.

You might need to use your ingenuity to find something suitable for Marty to swing on. Remember that as he swings backwards and forwards his weight will be pulling on the support so it needs to be pretty rigid and not slide around.  I ended up deciding to build Marty a swing all of his own (see the photo below). It wasn’t really necessary but if you want to build one too the design files and instructions are on GitHub.

## Timing Marty’s Swing

To try out Marty’s new swing give him a little push in the way you would a person in a playground. Marty might wobble a little bit to start off with but then he should settle into a smooth movement backwards and forwards. Practice a little getting him to swing smoothly and then see if you can give him an extra little push every few swings to help him keep going.

In order to better understand the swinging motion we need to start measuring how Marty swings. and the first thing we can do is to time how long it takes Marty to swing back and forth.  We can time his swing in a few different ways (maybe you can think of some others too?):

1. With a timer, such as the timer function on a smartphone or tablet. This allows us to measure the period of his swing and see if it matches with the simple pendulum simulation above.
2. Taking a video of Marty swinging at a known frame-rate (the number of still images per second that the video is made up from) and counting the frames between him reaching the high point of his motion on successive swings.
3. Using the motion sensor that is built-into Marty’s head and creating a program in Scratch or Python to get information about how Marty is moving.

We’ll start off with the first method. Get Marty swinging smoothly by giving him a push and waiting a few seconds. Then watch for Marty reaching the highest point of his swing and start a timer when he gets there. It is sometimes easiest to wait for the moment when Marty just starts to move back downwards. If your timer has a lap function then you can press the lap button each time Marty reaches the high-point of his swing on the same side that you started from.

Each lap you record like this should take approximately the same time and this is the period of the swing. If you don’t have a lap function or want to get a more accurate value than you can get in one go then you could time ten swings and then divide the total time by ten.

If you have used a similar length of strings to me then the period of Marty’s swing will be approximately 1.2 seconds. If it is much shorter than this then you might need to lengthen the strings a bit – and possibly attach them to something a bit higher off the desk – so that the swing period is over 1 second. The reasons for this should become apparent as we continue our experiments.

When you compare the time you measure to the time on the pendulum animation the length of the string that you need to consider isn’t just the length of the string attached to Marty’s arms. In the animation the red ball is considered to be a “point mass” and this makes things simpler. But Marty isn’t a point-mass – he’s more of a “big chunk” – so the length of the string you will need to consider isn’t going to be from the beam to his head or even to his toes but somewhere in-between – a place called his “centre of mass”. Marty’s centre of mass can be found by lying Marty flat on his back and trying to balance him on a ruler or similar edge. I found that my Marty’s centre of mass is just below his head.

## Detecting Marty’s Swing

We’re now going to go for the third option in the list above: “Using the motion sensor built into Marty’s head”.

Firstly we need to find Marty’s IP address and this can be done with the Marty App. If you scan for Marty then his IP address should appear. If there are several Martys around you will need to make sure you get the right one.

Standing really works best if you can move your body up and down without having to move your feet. Marty isn’t designed to do this either as his feet move apart when his knees bend, as the slightly surprised Marty on the right is demonstrating.

I wrote a short program in Python to connect to Marty, gather the information from his motion sensors and the display a blue rectangle which moves based on sensor data Marty reports.

Here’s a video of Marty and his motion sensor data (called accelerometer data in the video):

The code to show the rectangle based on Marty’s motion sensor data is in the GitHub repository.

What we have learned here is that we can detect Marty’s motion on a computer but that there is a slight delay before the information reaches the PC (or at least before the PC is able to draw its rectangle). We won’t bother to measure the delay yet but it would be possible to do this using the second method I mentioned above (video recording the motion and counting frames).

## Getting in the Swing of It

As we know the rate at which Marty swings (once he’s given a push) is determined by the length of the strings that Marty is hanging from (in reality it is the distance from the top of the string to Marty’s centre of mass – which I found to be just below his head). What we would really like to do now is to detect the period of Marty’s swing and track his motion so that we can work out when we would like to give him another push to keep his swing going.

If you were pushing a friend on a playground swing you would probably wait until the swing reached its highest point and was just starting to move back down again. Then, assuming you are at the right side to push, you would start pushing and stop a while before the swing reached the middle of its movement. The same thing applies if you are sitting on a swing and working to keep the swing going yourself. You will probably lean backwards and kick your legs at the highest point (when you are moving backwards). Then you will straighten up when you reach the opposite side – and repeat on each swing to keep the motion going to overcome the friction (the drag of the air and the friction in the rope movement) that constantly acts to slow you down.

To get a better idea of what is actually going on as Marty swings I changed the program to record the values from the motion sensor (accelerometer X axis) into a file. The program is called RecordSwing.py and is in the folder “Step 2 Record Swing”. Since this program is very short I’ve reproduced it here too.

The best way to use this program is to get it ready (you can run it in any terminal program with the command “python RecordSwing.py” or in IDLE or similar) but just before you start the program, give Marty a reasonably big push and then wait for a few seconds until his swing has become fairly smooth and then run. The program records data for 20 seconds into a text file called martySwing.txt

Make sure the program has finished and then you can open the martySwing.txt file in Google Sheets or Microsoft Excel. You should see a vertical column of numbers and the best way to look at what these numbers mean is to plot them on a graph or chart. In Google Sheets all you have to do is left-click on the cell at the top of the column of numbers and select Insert Chart from the menu. You should see something like this appear (in Microsoft Excel left-click on the cell at the top of the column and select Insert then find the little icon that represents Insert Line or Area Chart):

So now we should think a little bit about what we are seeing.

The information represents the motion of Marty as time passes – so the X axis (left-to-right) on the chart represents time – and the full set of data represents 20 seconds. Each swing that Marty makes results in values being collected that include the point where he passes through the lowest point of the swing and the points where he is swinging the highest at each end of his motion. To begin our understanding of what the Y axis values mean we can start by noticing that it looks a bit like a very steep roller-coaster. If we start at the beginning of the blue-line on the left side of the chart and follow the path we see that it is like going up over a hill and then down into a valley and then back up over another hill.

The top of each hill represents the high-point of Marty’s swing on one side and the bottom of each valley represents the high-point of Marty’s swing on the other side. The points where the line crosses the X axis (the slightly thicker black line) represent the low-points of Marty’s swing.

By counting the hills we can count the total number of swings that Marty does in the 20 seconds we were recording his motion. In this case there are 17 hills. So the period of Marty’s swing is 20 divided by 17 which is around 1.2 seconds per swing.

Going back to the little animation at the beginning of this post and setting the string length to 0.3m and then 0.4m we can see that the period for each of these pendulums is 1.1s and 1.3s. Our swing period is 1.2s and that indicates that our Marty-pendulum has an effective length of between 0.3m and 0.4m. This makes sense because my strings are around 0.25m long and the Marty’s centre of mass is just below his head – which is around 0.1m from the end of the string (so a total of around 0.35m).

## Why Does a Pendulum Stop Swinging

You might also have noticed at this point that Marty doesn’t continue to swing forever. His swings get smaller and smaller over time and he eventually stops altogether. We can see this too from the chart of Marty’s swing – each cycle is a bit smaller than the previous one. The time Marty takes to complete each swing cycle doesn’t change, but the speed that he’s going at the lowest point is reduced and the height he reaches at each of the high-points is also reduced on each cycle.

This is because of the drag of the air and friction in the strings holding Marty up.  When anything moves through the air it is having to overcome a force that is trying to resist its motion. This is called drag and, ultimately, it results in all of the energy in Marty’s swing being lost (to heat and possibly sound). If we were to remove all of the air in the room – obviously not an ideal situation for us – then Marty would continue to swing for a lot longer. But unfortunately that’s not really a practical experiment to do.

## Acceleration due to Gravity

Now it is time to think a little about what is actually making Marty swing in this way.

To investigate this have a go at giving Marty a push and then try to push him again at different places in his swing.  What you might find is that it is possible to force Marty to change direction or move faster in the direction he is going by giving him a push. If you wait for the right time and then push Marty at the top of his swing he will go slightly further on the next swing. But if you push against him while he is still swinging towards your hand you can make him slow down.

Whatever we do though, when we then leave Marty alone he goes back to swinging at the same rate as before and he changes direction at the top of each swing and starts moving back the other way. Since we know that we can make this kind of motion happen by forcing Marty’s movement ourselves (for instance giving him a push at the top of each swing) we might reason that something else is forcing Marty to act the way he does when we leave him alone on the swing.

The force that is acting on Marty to make him keep changing direction is called gravity. Gravity is the force that keeps us all on the earth and keeps the planets moving around the Sun. It is the same force that causes a ball thrown in the air to come back to earth. When he is swinging Marty is continually rising into the air and then being pulled back towards the earth by gravity.

As Marty passes through the lowest point he is moving his fastest but the string stops him from just heading off in a straight line and pulls him into an upward curve. Gravity is always working to pull downwards and so Marty slows down (just like a ball does when thrown in the air) and eventually stops (at the top of his swing). He then starts to move downwards once more. Marty can’t fall straight downwards because of the string so he falls in a curve, gaining speed, until he goes through the lowest point again and starts to climb once more.

The force of gravity always pulls down towards the earth but the Marty’s speed at the lowest point is sideways (horizontal). We also notice that at the top of his swing Marty is not moving at all. So how does the force of gravity – which is only downwards (vertical) – create movement at the lowest point which is horizontal.

The answer to this is that gravity pulls Marty downwards and the strings resists this pull. Since the strings aren’t vertical the result is partly that the strings pulls upwards (to resist gravity) but they also pull partly sideways. This sideways pull results in sideways acceleration of Marty towards the centre of the movement and that’s what makes Marty accelerate from being stopped at the top of his swing to moving fast in a horizontal direction at the bottom.

## So what does the Motion Sensor Actually Measure?

Now that we have a sense of what is really going on to make Marty swing I can explain that the motion sensor that we are using to monitor Marty’s motion is coming from an accelerometer. The values we are seeing are measurements of acceleration horizontally (in the direction that Marty’s is swinging at the bottom of his motion).

The fact that Marty is accelerating horizontally in a cyclical way shouldn’t be a surprise anymore since we know that the string pulls partly sideways when Marty is at the top of his swing. It probably also isn’t a surprise that this is the point where the sideways pull (acceleration) is at its maximum. From this point, as Marty gathers speed (i.e. accelerates) toward the centre of his motion, Marty continues to be pulled (accelerated) sideways (but less strongly) by the string and then, just as he goes through the centre point (where he is going fastest), the horizontal pull of the string switches direction and starts to slow him down (accelerate him negatively) towards a stop at the other high-point of his motion. Then the cycle starts again but in the opposite direction.

## A Program to Time Marty’s Swing

People have a pretty amazing capability to look at something moving in a cyclical way and sense the rhythmic motion. You may have noticed this if you have pushed a friend on a real swing – after just a few swings you can work out when is the best time to give the swing another push without really thinking about it.

But for a computer program to detect motion from a series of numbers being recorded one at a time is actually quite difficult. There are some advanced mathematical techniques that can be used but I’ve decided to use a more common-sense approach.

The program times the period of the swing in a similar way to doing it manually and is shown below. It looks a little more complicated than before and it is the first time we’ve used a class in our Python programs but it is really quite simple and is described below.

Also note that this program uses recorded data as we did before, but I’ve used a slightly different program to record Marty’s swing – this program is called RecordSwingWithTime.py (in the same folder as before):

• it records a longer period of time – 60 seconds
• it records only values which have changed (because Marty’s software returns values that repeat if you request it) and you may have noticed in the Google Sheets / Excel data that there were repeated values
• it records the time when the measurement was taken
We’ll skip the description of the class for now and jump to line 34 which creates the MartySwingPeriodTracker object and passes it a value of 1.2 which is our estimate of Marty’s swing period. The reason we pass an estimate will be explained in a moment.

Lines 37-39 read in the recordings we made with the program that records Marty’s swing and time. Make sure to run the RecordSwingWithTime.py program to capture these measurements.

Lines 41-47 work through each line in the file and extract the time and accelerometer X value.

Line 48 uses the martySwingPeriodTracker object we created above and sends it the time and accelerometer value.

Now let’s look at the MartySwingPeriodTracker class. A class is a programming mechanism that allows us to gather together data and program code that relates to a specific activity. In this case tracking Marty’s swing period.

__init__(self) on lines 2-8 is a function that just sets up some variables for us.

newData(self, timeSecs, xAcc) on line 10 introduces the function we use to tell our MartySwingPeriodTracker class that there is a new value for x acceleration.

line 12 (which is part of the newData function) checks if the newly received accelerometer reading is bigger than zero and also checks if the previous accelerometer reading was less than or equal to zero. This is basically checking whether the line that we traced earlier that looked like a roller-coaster has just crossed the X axis and I call these “crossings” in the explanation below.

We should recall that when Marty is heading through his time of minimum acceleration he is at the lowest point in his motion. We are also only checking for this crossing in one-direction – from negative acceleration to positive acceleration. It doesn’t actually matter which direction this is in reality, just that we only count crossings in one direction, otherwise we would be timing half of the total period for one full cycle.

line 13 just checks if this is the first time we’ve found a crossing. If not we remember the time this happened as the start time for our measurements – in lines 20-23

lines 15-17 are a check to see if we have recently passed through the lowest point. This is to try to avoid the effects of “wobble” on our measurements. If Marty is wobbling in his swing then the accelerometer data will record the wobble as well. And this means the values may go up and down a little bit on top of the bigger values for the swinging itself. We avoid being fooled by this by making sure that we ignore any repetition of going through the lowest point if we recorded one within half of our expected swing period. Since the wobble values will be small compared to the swing values this should work OK. This is also why we needed to pass in an estimate of the swing period – so we would know if we saw numbers that were way too small to be correct.

lines 18-19 remember the number of times we’ve gone through the lowest point of the swing and also the last time we did so.

finally – going back to the bottom of the program – line 51 prints out the period we’ve calculated making use of lines 28-31 which just divide the time between the first and last crossings by the number of crossings we recorded.

When I run this program I see:

How does this compare with what you get?

## Resources

That’s it for this blog post. Remember the programs and CAD drawings are on GitHub here.

If you want to build the swing here is a PDF file with some instructions: Building a Marty Swing

The programs should work on any computer that can run Python but I’ve only tried them so far on Windows. If you have any problems please let me know in the comments.

Marty Learns To Swing – Part 1

1. Geoffrey Bullough

Perpetual Marty
If Marty is standing on a swing ( the long pendulum ) and has separate strings for his hands ( the short pendulum ) will Marty be able to shift his mass from one pendulum to the other and so have a more effective push at the end of each swing ?
If the push is greater will Marty keep swinging ( using more energy ) until the batteries are empty ?

1. Post
Author
rob

Hi Geoff

I’m thinking of making Marty kick his legs – although there isn’t much range of movement in this action – to shorten and lengthen the “Marty pendulum” at the appropriate moments. Ideally shortening the pendulum at the centre point of his swing and lengthening again at the extremes – or a close approximation to this. The problems still to overcome are: (a) filtering the signal from the accelerometer well enough to be able to detect his swing (kicks and wobbles pollute the signal significantly) and (b) trying to coordinate kicking – currently my attempts create more wobble than swing as I’m not sure if I can get Marty to swing both legs at the same time. There are probably some other challenges I haven’t recognised yet too 🙂

Best

Rob

1. Geoff

I remember teenagers swinging almost to the top of large swings in the park without anyone to push start the swing . the frame was similar the your swing , as a child it seemed very big and had 2 side chains and a wooden seat .
the teenagers stood on the seat / plank and grasped the chains as high as possible , moving the mass between the hands and the feet and using the feet to keep the wooden seat available when swinging from the hands

2. Geoff Bullough

Could you make a smartphone swing?

As you seem to enjoy Reprogramming, could you make a a smartphone swing ?
A smartphone has inputs from movement sensors and a camera to detect its maximum swing position and power outputs from the sound , USB and vibrate.
Also , when edge on, quite low air resistance.
NASA’s PhoneSat program made a satellite, and a smartphone with I/O has been proposed for the Internet of Things.

1. Post
Author
rob

Hi Geoff

The only “actuators” I can think a Smartphone has are speakers and vibration-motor. I’m not sure they would be sufficient to keep the device swinging even if side-on.

Rob

1. Geoff

I was thinking that the smartphone, or Marty,could provide the intelligence and the USB output could supply just enough energy to keep It swinging , until the batteries are flat, rather than slowing down unless pushed .
I thought the speaker output could provide the signal for a release of battery energy and an external actuator throwing just enough mass in the opposite direction to the push required to keep everything moving at the pendulum resonant frequently.
The mass returning to the reset position at the opposite end of the pendulum stroke. like a rocket motor with a reset.

This site uses Akismet to reduce spam. Learn how your comment data is processed.