|
| 1 | +/* |
| 2 | + * Neurons - singleNeuronNetwork |
| 3 | + * |
| 4 | + * This example illustrates the use of a network to drive motor outputs. This particular |
| 5 | + * network contains only one neuron, and serves as an example to make the transition from single |
| 6 | + * neurons to connected networks easier. |
| 7 | + * |
| 8 | + * The one neuron is both sensory and motor. It takes input from the default input pin for |
| 9 | + * neuron 0 (A0, set for Pin Change Interrupt); it sends output spikes from the default output |
| 10 | + * pin for neuron 0 (pin 7). For a complete list of these default pins, see lines 11(default |
| 11 | + * output) and 12 (default input) of Network.cpp. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +#include <Neurons.h> |
| 16 | +#include <Network.h> |
| 17 | + |
| 18 | + |
| 19 | +//Network variables |
| 20 | +Network testNetwork(1); // Network constructor. |
| 21 | +int sensoryNeuronIDs[1] = {0}; // The array should contain the indexes of the sensory neurons. Useful later, when we 'setSensoryNeurons'. |
| 22 | + |
| 23 | + |
| 24 | +//Interrupt variables |
| 25 | +/* Sensory input is best taken in asynchronously (all other neurons' membrane |
| 26 | + * potentials and spikes are updated synchronously). |
| 27 | + * Since the sensory neurons expect inputs at the PinChange Interrupt Pins, we |
| 28 | + * need the following boolean array to check the state of the sensory pins. |
| 29 | + */ |
| 30 | +static volatile bool pins[1] = {0}; |
| 31 | + |
| 32 | + |
| 33 | +//Output Control Variables |
| 34 | +/* It helps to specify the indices of the neurons whose spikes are supposed to cause motor |
| 35 | + * output. For example, the zeroeth neuron (the only neuron in this network), is |
| 36 | + * supposed to cause the robot to move forward: |
| 37 | + * The boolean 'commandSent' variable is required to ensure that the command is |
| 38 | + * sent only once per spike. |
| 39 | + */ |
| 40 | +int forwardNeuron = 0; |
| 41 | +volatile boolean commandSent=1; |
| 42 | + |
| 43 | + |
| 44 | +void setup(){ |
| 45 | + Serial.begin(9600); |
| 46 | + |
| 47 | + //Network stuff |
| 48 | + testNetwork.dt = 5; //Update rate of the network (in milliseconds) |
| 49 | + testNetwork.setSensoryNeurons(sensoryNeuronIDs, 1); // Using the array from above to know which pins are sensory |
| 50 | + |
| 51 | + /* Motor output will be sent from thes pins. In this case, the pins were |
| 52 | + * connected to an L293D motor driver IC, and the pins and values are set |
| 53 | + * accordingly. You may need to change this according to your setup. |
| 54 | + */ |
| 55 | + //Left wheel |
| 56 | + pinMode(8,OUTPUT); digitalWrite(8,LOW); |
| 57 | + pinMode(9,OUTPUT); digitalWrite(9,LOW); |
| 58 | + //Right Wheel |
| 59 | + pinMode(10,OUTPUT); digitalWrite(10,HIGH); |
| 60 | + pinMode(11,OUTPUT); digitalWrite(11,HIGH); |
| 61 | +} |
| 62 | + |
| 63 | +void loop() { |
| 64 | + /* |
| 65 | + * Every iteration of the loop indicates one time-step. Within each time-step, |
| 66 | + * a number of tasks occur, each with it's own dedicated code block in the code |
| 67 | + * below: |
| 68 | + * |
| 69 | + * 1. The neurons' membrane potentials are updated, and if spikes occur |
| 70 | + * then they are relayed throughout the network. |
| 71 | + * 2. The requisite instructions are issued, according to the spiking of the motor |
| 72 | + * neurons. |
| 73 | + * 3. The outputs of the neurons are sent though the output pins, from where they can |
| 74 | + * be visualized on LEDs. This is an optional step. |
| 75 | + * |
| 76 | + * The neuron's input pins are continuously monitored in the background, triggering the interrupt |
| 77 | + * routine whenever a change occurs on any one of them. |
| 78 | + * |
| 79 | + */ |
| 80 | + |
| 81 | + |
| 82 | + /* This code block is where the synchronous update of the neurons' membrane potentials |
| 83 | + * and spikes takes place. This part of the code remains pretty much constant. |
| 84 | + */ |
| 85 | + testNetwork.updateNeurons(); |
| 86 | + testNetwork.issueSpikes(); |
| 87 | + |
| 88 | + /* This block ensures that a command sent as a response to a motor neuron spike issued in the previous |
| 89 | + * time-step doesn't persist into the next time-step. |
| 90 | + * The last if-statement is is where the commands are actually sent, according to the spiking |
| 91 | + * of the motor neurons. |
| 92 | + */ |
| 93 | + if(commandSent){ |
| 94 | + digitalWrite(8,LOW); |
| 95 | + digitalWrite(9,LOW); |
| 96 | + digitalWrite(10,LOW); |
| 97 | + digitalWrite(11,LOW); |
| 98 | + commandSent=0; |
| 99 | + } |
| 100 | + if(testNetwork.Neurons[forwardNeuron].spike) moveForward(); |
| 101 | + |
| 102 | + /* The 'Visualize' function is used to send the required outputs to the default output pins |
| 103 | + * of the neurons (these default pins are assigned according to the neuron index, see line |
| 104 | + * 11 of Network.cpp) |
| 105 | + */ |
| 106 | + testNetwork.Visualize(); |
| 107 | + |
| 108 | + /* This delay is actually very important. It allows for the sensory neurons to 'accumulate' |
| 109 | + * input current from their input pins before the next calculation of the nest time-step commence. |
| 110 | + */ |
| 111 | + delay(testNetwork.dt); |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/* This interrupt routine relays asynchronous spikes coming in at the analogPins to the sensoryNeurons. |
| 116 | + * Notice the input pin is A0 - the default for neuron index 0. This is another part of the code that |
| 117 | + * remains pretty much constant. The same block repeats with more neurons. |
| 118 | + */ |
| 119 | +ISR(PCINT1_vect){ |
| 120 | + if(digitalRead(A0)!=pins[0]){ |
| 121 | + testNetwork.Neurons[0].inputCurrent += 2000; |
| 122 | + pins[0] = digitalRead(A0); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/* This function issues the instructions required by the associated motor neuron. |
| 127 | + * In this case, that would be to make the robot turn right. |
| 128 | + */ |
| 129 | +void moveForward(){ |
| 130 | + digitalWrite(8,LOW); |
| 131 | + digitalWrite(9,HIGH); |
| 132 | + digitalWrite(10,LOW); |
| 133 | + digitalWrite(11,HIGH); |
| 134 | + commandSent=1; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | + |
0 commit comments