Wishful Coding

Didn't you ever wish your
computer understood you?

Explorer robot without sensors

Usually the first robot you make when you get the NXT is the wheelbase with a bumper, you know, make it run into a wall, turn back and repeat.

To really get my point about saving sensors across, I made a robot like that without any sensors.

It works by turning the motors in regulated mode(constant speed, varying power) and measuring the actual power applied. If the robot runs into a wall, the firmware will apply extra power to the motors to keep them turning. With some tweaking, you can even detect which wheel hit the wall.

The commented code:

// define 2 variables for containing the actual speed
dseg segment
  aaspeed byte
  caspeed byte
dseg ends

thread main
Start:
  // turn the motors on, regulated
  OnFwdReg(OUT_AC, 50, OUT_REGMODE_SPEED)
  // wait for the robot to accelerate
  // it will apply full power here
  wait 1000
Forever:
  // get the actual power used
  getout aaspeed OUT_A ActualSpeedField
  getout caspeed OUT_C ActualSpeedField

  // print the power to the screen
  NumOut(0, LCD_LINE1, aaspeed)
  NumOut(0, LCD_LINE2, caspeed)

  // if one of the motors uses more than 75 power
  // jump to either LResistance or RResistance
  brcmp GT LResistance aaspeed 75
  brcmp GT RResistance caspeed 75

  // repeat forever
  jmp Forever

LResistance:
  // reverse, turn right, jump to start
  OnRevReg(OUT_AC, 50, OUT_REGMODE_SPEED)
  wait 2000
  OnFwdReg(OUT_A, 50, OUT_REGMODE_SPEED)
  wait 500
  jmp Start

RResistance:
  // reverse, turn left, jump to start
  OnRevReg(OUT_AC, 50, OUT_REGMODE_SPEED)
  wait 2000
  OnFwdReg(OUT_C, 50, OUT_REGMODE_SPEED)
  wait 500
  jmp Start
endt

Lat/Lon to Meter

Easy, 1 Latitude is the circumference of the earth divided by 360: 111 km

1 Longitude depends on your Latitude, so… copy-paste

math.radians(6378137.0 * math.cos(math.radians(lat)))

Now, I know this all breaks down horribly when you consider large distances or require high precision(it also upsets mathematicians), but I don’t care(much).

All I1 am interested in is, if I stand at 42° Latitude and I walk North 10 meter, about what is my new Latitude?

  1. And probably half the googlers and questioners on Stack Overflow. 

Published on

Saving sensors with structural limits

When building a robot with some sort of back-and-forth motion, such as a steering car or a robotic arm, you commonly see touch sensors at the end or center to easily move to that point.

However, the NXT motors have built-in rotation sensors, so with a bit more fiddling, you can get rid of most touch sensors in your system by using the structural limits of the model.

The basic idea is that you move the motor slowly forwards until it doesn’t go any further, record the tacho count, rotate backwards slowly until it stops, record the tacho count. Now you know the center point(the average of the two), and you can move to any point within the limits real quick.

In NXT-G this can very easily be done using the PID block by HiTechnic, but it does not give you the endpoints, which you can notice in the video.

In NXC there is a more powerful absolute position regulation, implemented at firmware level. Flexible, fast, precise, awesome.