I’m modding a robot toy for the next Spork Factory experiment, the chassis provides twin motor driven wheels and I’m replacing it’s brains with a circuit based on the ATtiny85 for running the results of the genetic algorithm, and a pair of light dependant resistors for ‘seeing’ with.
Here’s the circuit (made in about 20 minutes after installing Fritzing for the first time). It’s quite simple – two LDR’s provide input, and some transistors are needed to provide enough power to the robot’s motors (it’s using PNP transistors as they were the first matching pair I could find, which means logical 0 is ‘on’ and 1 is ‘off’ in the code).
The robot needs to be emulated in software so the genetic algorithm can measure the fitness of hundreds of thousands of candidate programs. We need to simulate the effect of motor speeds on it’s position and velocity – here is a test running the right motor at a constant rate, and gradually increasing the speed of the left motor.
This is the code snippet that calculates the velocity from the 2 motor speeds – more work is needed to plug in the correct values for the distance between the wheels and the actual rotational speeds of the motor drives.
// update dir and pos from current motor speed float relative_speed_diff=m_left_motor-m_right_motor; float angle=atan(relative_speed_diff); // rotate the direction by the angle float nx=(m_dir.x*cos(angle))-(m_dir.y*sin(angle)); float ny=(m_dir.x*sin(angle))+(m_dir.y*cos(angle)); m_dir.x=nx; m_dir.y=ny; // update the position m_pos=m_pos.add(m_dir);