Presentation is loading. Please wait.

Presentation is loading. Please wait.

4 - 270° Speedometer Display 4 - 270° Speedometer Display The speedometer on a car is traditionally a circular dial using around 270° of the arc to indicate.

Similar presentations


Presentation on theme: "4 - 270° Speedometer Display 4 - 270° Speedometer Display The speedometer on a car is traditionally a circular dial using around 270° of the arc to indicate."— Presentation transcript:

1 4 - 270° Speedometer Display 4 - 270° Speedometer Display The speedometer on a car is traditionally a circular dial using around 270° of the arc to indicate the speed. The reason it looks like this is that it was, and generally still is, a mechanical device. A magnet on the end of a rotating cable, driven from the gearbox, acts on the pointer and causes it to turn around a dial against the restoring force of a spring. In Bloodhound the speed is measured digitally, because the car’s software needs to ‘know’ the speed as a digital number. This number could just be displayed to the driver, but a better way might be to reproduce a traditional analogue speedometer from which the driver can gauge the speed of the car from a quick glance. The pictures above illustrate the 270° display, in exaggerated form, against the background of a Bloodhound cockpit. For simplicity it is scaled in % rather than mph. The left image shows 27% of full speed and the right image shows full speed at 100%. The fader output (0% to 100%) is deemed to be the speed of the car. This module is primarily about writing the software to simulate a conventional speedometer. In order to give the software a ‘speed’ to work with, that we can easily vary, we are taking the output from the slider / fader on a PicoBoard and assuming that it is the speed of the car. P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

2 Program Outline Wait until slider has moved. Clear the screen. Progressively draw an arc, starting from 0° until a point is reached that represents the current slider position. Initialise variables and screen. Wait until the slider has moved. Scratch is what is known as an interpreted language, and for every Scratch command a significant amount of underlying code needs to be run. This, as will be seen, takes time, and the delay can be visible on the screen. It is therefore better to only change the display when the slider has moved to a new position. What constitutes a ‘new position’ is a moot point. In the code below it is deemed to be + or – 5%. Note It may be necessary to run Scratch in ‘Turbo’ mode to get a reasonable display. 2 P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

3 Clearing the Screen The method used to clear the screen actually switches the sprite’s costume to one that represents zero speed; in this example a simple blank disk is used. It is obviously possible to create something more speedometer-like, perhaps with numbers around the edge, but the paint facility in Scratch is not very sophisticated. The best way would probably be to use another drawing tool and import it as a picture. The following commands will be useful: Drawing the Arc If we were indicating the speed by a straight line, it would be easy. It would just be a case of moving the sprite to the start position, say (X=0, Y=0), putting the pen down and then moving to a position that indicates the speed. For example, if the speed was 50 you might move the sprite to (X=0, Y=50). However we need a curve, so we need to move the sprite in small increments, calculating X and Y each time. The first requirement is to scale the slider value, which is from 0 to 100, to an angle that corresponds to the length of the arc to be displayed. We then need to create the display. This is achieved with 3 P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

4 Drawing the Arc (continued) Assume that a speed of 0 corresponds to 0° on the dial, and a speed of 100 corresponds to 270° on the dial. We can then use trigonometry to calculate the position of each point on the described arc. A B C +X-X +Y -Y Speed corresponds to dial angle X Y d a In this simplified example: Angle ‘a’ corresponds to the speed. ‘d’ is the (fixed) diameter of the circle. Y = ‘d’ x Sin ‘a’ X = ‘d’ x Cos ‘a’ There two complications, however: In quadrant A, X is negative. In quadrant C, Y is negative. This means the calculated values have to be adjusted accordingly. If we assume that varying angle ‘a’ for 0° to 89° takes the speed display through quadrant ‘A’, in quadrant ‘B’ the angle varies from 90° to 179° and in ‘C’ from 180° to 269°. The trigonometry formula assumes the angle is < 90° and so ‘a’ has to be adjusted accordingly. This is best understood by looking at the code below. This is the code that calculates X and Y for quadrant ‘C’ Angle is the value of ‘a’ used for calculation. Dial Point is the point on the dial for which we are calculating ‘X’ and ‘Y’. The code for quadrants ‘A’ and ‘B’ is similar. 4 P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

5 Draw Arc (continued) This (finally!) is the main code in ‘Draw Arc’. I.e. progressively draw an arc, starting from 0° until a point is reached that represents the current fader position. Decide which quadrant we are working in and calculate the corresponding value of ‘X’ and ‘Y’. Write the next segment of arc on the screen. 5 P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

6 6 Complete Program P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/

7 Sources of help General Scratch helphttp://scratch.mit.edu/help/ The Scratch website includes many examples of Scratch programs and tutorials. PicoBoard setuphttp://www.picocricket.com/picoboardsetupUSB.html References 6 Acknowledgements PicoBoard imagehttp://www.hobbytronics.co.uk/picoboard-scratch Sparkfun ProtoSnaphttps://www.sparkfun.com/products/10311 Create a more realistic display with speed numbers around the dial. Convert the slider % reading to mph. Do this by assuming a relationship between % and mph, e.g. 100% = 1200mph, and introduce some maths to calculate the mph value. When the slider is moved, the program typically displays the new speed twice. It does this because moving the slider immediately triggers the new speed value to be displayed, but it does so before the slider has finished moving causing the display to be updated again when it has stopped. Introduce a procedure to check that the slider has stopped moving before the display is updated. Suggestions for further work P roduced at Warwick University by Margaret Low (m.j.low@warwick.ac.uk), Simon Leigh (S.J.Leigh@warwick.ac.uk),m.j.low@warwick.ac.ukS.J.Leigh@warwick.ac.uk Bob Hodge Funded by Bloodhound SSC http://www.bloodhoundssc.com/ To download Scratch version 1.4http://scratch.mit.edu/scratch_1.4 Software


Download ppt "4 - 270° Speedometer Display 4 - 270° Speedometer Display The speedometer on a car is traditionally a circular dial using around 270° of the arc to indicate."

Similar presentations


Ads by Google