<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://wiki.zenerves.net/skins/common/feed.css?301"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
		<id>https://wiki.zenerves.net/index.php?action=history&amp;feed=atom&amp;title=Rex</id>
		<title>Rex - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.zenerves.net/index.php?action=history&amp;feed=atom&amp;title=Rex"/>
		<link rel="alternate" type="text/html" href="https://wiki.zenerves.net/index.php?title=Rex&amp;action=history"/>
		<updated>2026-06-25T13:49:24Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.17.0</generator>

	<entry>
		<id>https://wiki.zenerves.net/index.php?title=Rex&amp;diff=186&amp;oldid=prev</id>
		<title>Admin: 1 revision</title>
		<link rel="alternate" type="text/html" href="https://wiki.zenerves.net/index.php?title=Rex&amp;diff=186&amp;oldid=prev"/>
				<updated>2011-10-14T23:59:15Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 23:59, 14 October 2011&lt;/td&gt;
		&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>https://wiki.zenerves.net/index.php?title=Rex&amp;diff=185&amp;oldid=prev</id>
		<title>Lola at 23:36, 14 October 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.zenerves.net/index.php?title=Rex&amp;diff=185&amp;oldid=prev"/>
				<updated>2011-10-14T23:36:14Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;'''NXC program to run Alpha-rex from the base NXT set.'''&lt;br /&gt;
&lt;br /&gt;
 Instructions to build '''Rex''' are [http://pics.zenerves.net/index.php?gallery=linux/NXT/AlphaRex here]&lt;br /&gt;
&lt;br /&gt;
Graham Hawkins (29 Jan 2007)&lt;br /&gt;
&lt;br /&gt;
Performs the tasks set in the 'Test Guide' sections of the&lt;br /&gt;
construction pane of the Mindstorms NXT-G for Alpha-Rex:&lt;br /&gt;
&lt;br /&gt;
1. Walk from the start of the rest pad to the ball &amp;amp; back. Actually&lt;br /&gt;
it keeps walking in loops ad infinitum.&lt;br /&gt;
&lt;br /&gt;
2. Display beating heart.&lt;br /&gt;
&lt;br /&gt;
3. Say 'hello' when something comes near to the US sensor, and&lt;br /&gt;
'goodbye' when it goes away.&lt;br /&gt;
&lt;br /&gt;
4. Say 'Play music please' when touch sensor operated. Then dance if&lt;br /&gt;
loud noise detected.&lt;br /&gt;
&lt;br /&gt;
5. Go to sleep when the lights go out, wake up when they come on.&lt;br /&gt;
Movements are performed as an integer number of 'steps' to try and&lt;br /&gt;
keep the 'lift' and 'pace' actions in phase as per pages 46-49 of&lt;br /&gt;
the building guide.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 //Some Constants:&lt;br /&gt;
 &lt;br /&gt;
 #define MY_ONE_STEP       1080  //Motor has an 8 tooth gear. Leg drive has a 24 tooth gear. So 1 step is 360*24/8 degrees = 1080&lt;br /&gt;
 #define MY_STEPS_TO_BALL    11  //Each step is about 4.25cm. The ball is about 50cm up the test pad from the start position, so that's about 11 steps. &lt;br /&gt;
 &lt;br /&gt;
  // ...YMMV depending on friction...&lt;br /&gt;
 &lt;br /&gt;
 #define MY_STEPS_TO_TURN     8  // It takse about 8 steps from motor B to execute a 180 degree turn &lt;br /&gt;
 #define MY_FWD_PWR          30  // Define some motor power levels&lt;br /&gt;
 #define MY_BOOGIE_PWR       70  // Define some motor power levels&lt;br /&gt;
 &lt;br /&gt;
 // We need a mutex to stop the 'move' task when we want to dance instead of walk. &lt;br /&gt;
 // The shared resource consists of the drive motors B &amp;amp; C and the arm/head motor A. &lt;br /&gt;
 mutex motors_a_b_c;&lt;br /&gt;
 mutex the_display; //  And a mutex for the display to stop the heart 'beat' task when the 'sleep' task wants to display 'Zzzz'.&lt;br /&gt;
 &lt;br /&gt;
 task main() //task main just exits, and the 5 following tasks start.&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Move forever up to the ball position and back to the start position. This is likely to be very inaccurate since the distance&lt;br /&gt;
  walked per step varies depending on the coefficient of friction between the foot &amp;amp; the surface...&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 task move()&lt;br /&gt;
 {&lt;br /&gt;
    short i = 0;&lt;br /&gt;
   &lt;br /&gt;
    Follows (main);&lt;br /&gt;
    while (1)&lt;br /&gt;
    {&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Would have used function calls here, but the mutex interaction&lt;br /&gt;
  ith the dance task doesn't seem to work if the mutex&lt;br /&gt;
  operators are in a function.&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
        //move_steps(MY_STEPS_TO_BALL, MY_FWD_PWR);&lt;br /&gt;
        //turn_steps(MY_STEPS_TO_TURN, MY_FWD_PWR);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  After each step, release the motor mutex in case the dance task&lt;br /&gt;
  want the motors. The re-acquire the motors and carry on.&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
        i = 0;&lt;br /&gt;
        while (i &amp;lt; MY_STEPS_TO_BALL)&lt;br /&gt;
        {&lt;br /&gt;
            Acquire(motors_a_b_c);&lt;br /&gt;
            OnFwd(OUT_A, MY_FWD_PWR);&lt;br /&gt;
            /* Use synch API to try &amp;amp; keep drive motors in step */&lt;br /&gt;
            RotateMotorEx(OUT_BC, MY_FWD_PWR, MY_ONE_STEP, 0, true, true);&lt;br /&gt;
            Float(OUT_A);&lt;br /&gt;
            Release (motors_a_b_c);&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        i = 0;&lt;br /&gt;
        while ( i &amp;lt; MY_STEPS_TO_TURN)&lt;br /&gt;
        {   &lt;br /&gt;
            Acquire(motors_a_b_c);&lt;br /&gt;
            OnFwd(OUT_A, MY_FWD_PWR);&lt;br /&gt;
            RotateMotor(OUT_B, MY_FWD_PWR, MY_ONE_STEP);&lt;br /&gt;
            Float(OUT_A);&lt;br /&gt;
            Release (motors_a_b_c);&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  This task monitors the US sensor for things getting closer than&lt;br /&gt;
  12cm. If detected, plays Hello.rso. If something is near, monitors&lt;br /&gt;
  for it going away, and plays Goodbye.rso.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 task ultra_sonic ()&lt;br /&gt;
 {&lt;br /&gt;
    short ultra = 0;&lt;br /&gt;
    short hello_thr = 12;&lt;br /&gt;
    short bye_thr = 20;&lt;br /&gt;
    bool  near = false;&lt;br /&gt;
   &lt;br /&gt;
    Follows (main);&lt;br /&gt;
  &lt;br /&gt;
    SetSensorLowspeed(IN_4);&lt;br /&gt;
    while (1)&lt;br /&gt;
    {&lt;br /&gt;
        ultra = SensorUS(IN_4);&lt;br /&gt;
        //NumOut(10, LCD_LINE7, true, ultra);&lt;br /&gt;
      &lt;br /&gt;
        if (ultra &amp;lt;= hello_thr)&lt;br /&gt;
        {&lt;br /&gt;
            if(!near) &lt;br /&gt;
            {&lt;br /&gt;
                PlayFile(&amp;quot;Hello.rso&amp;quot;);&lt;br /&gt;
                near = true;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        else if ( ultra &amp;gt; bye_thr)&lt;br /&gt;
        {&lt;br /&gt;
            if(near) &lt;br /&gt;
            {&lt;br /&gt;
                PlayFile(&amp;quot;Goodbye.rso&amp;quot;);&lt;br /&gt;
                near = false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  No point in using up all the CPU whizzing round this loop.&lt;br /&gt;
 */      &lt;br /&gt;
  &lt;br /&gt;
       Wait(200);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 /*&lt;br /&gt;
  This task displays a beating heart.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 task beat()&lt;br /&gt;
 {&lt;br /&gt;
 &lt;br /&gt;
 /* &lt;br /&gt;
  Set up some co-ordinates so the heart appears to beat in place&lt;br /&gt;
  rather than moving to the right and back.&lt;br /&gt;
 */ &lt;br /&gt;
  &lt;br /&gt;
    short big_heart_x = 16;&lt;br /&gt;
    short big_heart_y = 1;&lt;br /&gt;
    short little_heart_x = 20;&lt;br /&gt;
    short little_heart_y = 4;&lt;br /&gt;
   &lt;br /&gt;
 /* &lt;br /&gt;
  Start the heart after main() exits.&lt;br /&gt;
 */ &lt;br /&gt;
 &lt;br /&gt;
    Follows (main);&lt;br /&gt;
    &lt;br /&gt;
    while (1)&lt;br /&gt;
    {&lt;br /&gt;
 &lt;br /&gt;
 /* &lt;br /&gt;
  Little heart&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
        Acquire(the_display);&lt;br /&gt;
        GraphicOut(little_heart_x, little_heart_y, &amp;quot;Heart 02.ric&amp;quot;,true);&lt;br /&gt;
        Release(the_display);&lt;br /&gt;
        Wait(1000);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Big heart&lt;br /&gt;
 */  &lt;br /&gt;
  &lt;br /&gt;
        Acquire(the_display);&lt;br /&gt;
        GraphicOut(big_heart_x, big_heart_y, &amp;quot;Heart 01.ric&amp;quot;, true);&lt;br /&gt;
        Release(the_display);&lt;br /&gt;
        Wait(1000);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Task to control dance movements in response to touch and sound&lt;br /&gt;
  sensors.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 task dance()&lt;br /&gt;
 {&lt;br /&gt;
 &lt;br /&gt;
    long tick = 0;&lt;br /&gt;
    long begin_tick = 0;&lt;br /&gt;
    long wait_time = 3000;&lt;br /&gt;
    bool wait_expired = false;&lt;br /&gt;
    short sound_level;&lt;br /&gt;
    short sound_threshold = 40;&lt;br /&gt;
   &lt;br /&gt;
    Follows (main);&lt;br /&gt;
    SetSensorTouch(IN_1);&lt;br /&gt;
    SetSensorSound(IN_2);&lt;br /&gt;
   &lt;br /&gt;
    while (1)&lt;br /&gt;
    {&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Wait for touch sensor to be pressed. Get the motor mutex, and&lt;br /&gt;
  play specified files.&lt;br /&gt;
 */&lt;br /&gt;
  &lt;br /&gt;
       while(!Sensor(IN_1))&lt;br /&gt;
        {&lt;br /&gt;
            ;&lt;br /&gt;
        }&lt;br /&gt;
      &lt;br /&gt;
        Acquire(motors_a_b_c);&lt;br /&gt;
        PlayFile(&amp;quot;Play.rso&amp;quot;);&lt;br /&gt;
        Wait(500);&lt;br /&gt;
        PlayFile(&amp;quot;Music.rso&amp;quot;);&lt;br /&gt;
        Wait(500);&lt;br /&gt;
        PlayFile(&amp;quot;Please.rso&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Wait 3 secs for a loud noise. Dance if noise detected.  Use&lt;br /&gt;
  system tick rather than Wait() because we want to test the sound&lt;br /&gt;
  sensor during the wait period.&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
        wait_expired = false;&lt;br /&gt;
        begin_tick = CurrentTick();&lt;br /&gt;
        while (!wait_expired)&lt;br /&gt;
        {&lt;br /&gt;
            tick = CurrentTick();&lt;br /&gt;
            if ((tick - begin_tick)&amp;gt;=wait_time)&lt;br /&gt;
            {&lt;br /&gt;
                wait_expired = true;&lt;br /&gt;
            }&lt;br /&gt;
 &lt;br /&gt;
            sound_level = Sensor(IN_2);&lt;br /&gt;
            if (sound_level &amp;gt;= sound_threshold)&lt;br /&gt;
            {&lt;br /&gt;
                RotateMotor(OUT_AC, MY_BOOGIE_PWR, 5*MY_ONE_STEP);&lt;br /&gt;
                wait_expired = false;&lt;br /&gt;
                begin_tick = CurrentTick();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  No more loud noises heard. Let move task have the motors back.&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
        Release (motors_a_b_c);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Sleep task grabs the motors &amp;amp; display if amient light drops by hysteresis value.&lt;br /&gt;
  Releases them if ambient goes up by hysteresis.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 task sleep()&lt;br /&gt;
 {&lt;br /&gt;
    short ambient;&lt;br /&gt;
    short hysteresis = 20;&lt;br /&gt;
    short light_value;   &lt;br /&gt;
    bool dark = false;&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
    Follows(main);&lt;br /&gt;
 &lt;br /&gt;
    SetSensorType(IN_3,IN_TYPE_LIGHT_INACTIVE);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  Assume it's daytime&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
   ambient = Sensor(IN_3);&lt;br /&gt;
    dark = false;&lt;br /&gt;
   &lt;br /&gt;
    while (1)&lt;br /&gt;
    {&lt;br /&gt;
        light_value =  Sensor(IN_3);&lt;br /&gt;
        //NumOut(10, LCD_LINE7, true, light_value);&lt;br /&gt;
        if (dark)&lt;br /&gt;
        {&lt;br /&gt;
            if( light_value &amp;gt; (ambient + hysteresis))&lt;br /&gt;
            {&lt;br /&gt;
                PlayFile(&amp;quot;Goodmorning.rso&amp;quot;);&lt;br /&gt;
                Wait(500);&lt;br /&gt;
                Release(the_display);&lt;br /&gt;
                Release (motors_a_b_c);&lt;br /&gt;
                dark = false;&lt;br /&gt;
                ambient = light_value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        if (!dark)&lt;br /&gt;
        {&lt;br /&gt;
            if( light_value &amp;lt;  (ambient - hysteresis))&lt;br /&gt;
            {&lt;br /&gt;
                Acquire(the_display);&lt;br /&gt;
                Acquire (motors_a_b_c);&lt;br /&gt;
                Wait(500);&lt;br /&gt;
                PlayFile(&amp;quot;Good.rso&amp;quot;);&lt;br /&gt;
                Wait(500);&lt;br /&gt;
                PlayFile(&amp;quot;Night.rso&amp;quot;);&lt;br /&gt;
                Wait(500);&lt;br /&gt;
                GraphicOut(10, 10, &amp;quot;Zzzz.ric&amp;quot;, true);&lt;br /&gt;
                dark = true;&lt;br /&gt;
                ambient = light_value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
  No point in using up all the CPU whizzing round this loop.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
        Wait(200);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Lola</name></author>	</entry>

	</feed>