Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added autonomous project

  • Loading branch information...
commit 1794863266c41d3946d7739b44e746efa7ca27a2 1 parent a1102c7
Sander van de Graaf authored
148 autonomous/autonomous/autonomous.ino
... ... @@ -0,0 +1,148 @@
  1 +#include <Servo.h>
  2 +
  3 +Servo head;
  4 +int distance = 0;
  5 +int headRotation = 0;
  6 +int pos = 0;
  7 +
  8 +//Arduino PWM Speed Control
  9 +int E1 = 6;
  10 +int M1 = 7;
  11 +int E2 = 5;
  12 +int M2 = 4;
  13 +
  14 +void setup()
  15 +{
  16 + Serial.begin(9600);
  17 + Serial.println("start");
  18 +
  19 + // mark forward
  20 + digitalWrite(M1, HIGH);
  21 + digitalWrite(M2, HIGH);
  22 +
  23 + // servo
  24 + head.attach(9);
  25 +
  26 + centerHead();
  27 +}
  28 +
  29 +void loop()
  30 +{
  31 +
  32 + distance = analogRead(0);
  33 + Serial.println(distance);
  34 +
  35 + // if close by an object, start rotating
  36 + if(distance > 100)
  37 + {
  38 + Serial.println("uhoh, possible collision detected, sweeping")
  39 + sweep();
  40 + }
  41 + else
  42 + {
  43 + forward();
  44 + }
  45 +}
  46 +
  47 +void forward()
  48 +{
  49 + digitalWrite(M1, HIGH);
  50 + digitalWrite(M2, HIGH);
  51 + analogWrite(E1, 128);
  52 + analogWrite(E2, 128 );
  53 +}
  54 +
  55 +void standstill()
  56 +{
  57 + digitalWrite(M1, HIGH);
  58 + digitalWrite(M2, HIGH);
  59 + analogWrite(E1, 0);
  60 + analogWrite(E2, 0);
  61 +}
  62 +
  63 +void sweep()
  64 +{
  65 + boolean foundNext = false;
  66 +
  67 + // stop any movement
  68 + standstill();
  69 +
  70 + // move the head, and measure the distance
  71 + for(pos=0;pos<180;pos+=5)
  72 + {
  73 + head.write(pos); // sweep start position
  74 + delay(15);
  75 + if(measure())
  76 + {
  77 + foundNext = true;
  78 + break;
  79 + }
  80 + }
  81 +
  82 + if(foundNext == false)
  83 + {
  84 + for(pos = 180; pos>=1; pos-=5) // goes from 180 degrees to 0 degrees
  85 + {
  86 + head.write(pos); // tell servo to go to position in variable 'pos'
  87 + delay(15);
  88 + if(measure())
  89 + {
  90 + foundNext = true;
  91 + break;
  92 + }
  93 + }
  94 + }
  95 +
  96 + centerHead();
  97 +
  98 + // next step found?
  99 + if(foundNext)
  100 + {
  101 + if(headRotation > 90)
  102 + {
  103 + // turn left
  104 + left();
  105 + delay(500);
  106 + }
  107 + else
  108 + {
  109 + right();
  110 + delay(500);
  111 + }
  112 + }
  113 +}
  114 +
  115 +void turnLeft()
  116 +{
  117 + digitalWrite(M1, HIGH);
  118 + digitalWrite(M2, LOW);
  119 + analogWrite(E1, 128);
  120 + analogWrite(E2, 128);
  121 +}
  122 +
  123 +void turnRight()
  124 +{
  125 + digitalWrite(M1, LOW);
  126 + digitalWrite(M2, HIGH);
  127 + analogWrite(E1, 128);
  128 + analogWrite(E2, 128 );
  129 +}
  130 +
  131 +void centerHead()
  132 +{
  133 + head.write(90);
  134 + delay(15);
  135 +}
  136 +
  137 +boolean measure()
  138 +{
  139 + currentReadout = analogRead(0);
  140 + Serial.print("sees: ");
  141 + Serial.println(currentReadout);
  142 +
  143 + // if more distant, set the direction
  144 + if(currentReadout < distance)
  145 + {
  146 + headRotation = head.read()
  147 + }
  148 +}
BIN  engine/engines/applet/core.a
Binary file not shown
152 engine/engines/applet/engines.cpp
... ... @@ -0,0 +1,152 @@
  1 +#include "Arduino.h"
  2 +void setup();
  3 +void loop();
  4 +//Arduino PWM Speed Control
  5 +int E1 = 6;
  6 +int M1 = 7;
  7 +int E2 = 5;
  8 +int M2 = 4;
  9 +
  10 +// used for detecting incoming bytes
  11 +int incomingByte = 0;
  12 +
  13 +bool w = false;
  14 +bool a = false;
  15 +bool s = false;
  16 +bool d = false;
  17 +
  18 +bool keyUp = false;
  19 +
  20 +void setup()
  21 +{
  22 + pinMode(M1, OUTPUT);
  23 + pinMode(M2, OUTPUT);
  24 +
  25 + Serial.begin(9600);
  26 + Serial.println("start");
  27 +}
  28 +
  29 +void loop()
  30 +{
  31 + // send data only when you receive data:
  32 + if (Serial.available() > 0) {
  33 + // read the incoming byte:
  34 + incomingByte = Serial.read();
  35 +
  36 + // say what you got:
  37 + Serial.print("I received: ");
  38 + Serial.println(incomingByte, DEC);
  39 + Serial.println(incomingByte);
  40 +
  41 + switch(incomingByte)
  42 + {
  43 + case 64:
  44 + keyUp = true;
  45 + break;
  46 + case 119:
  47 + // w
  48 + if(keyUp == true)
  49 + {
  50 + Serial.println("w = false, keyUp = true");
  51 + w = false;
  52 + keyUp = false;
  53 + }
  54 + else
  55 + {
  56 + Serial.println("w = true, keyUp = false");
  57 + w = true;
  58 + }
  59 + break;
  60 + case 97:
  61 + // a
  62 + if(keyUp == true)
  63 + {
  64 + Serial.println("a = false, keyUp = true");
  65 + a = false;
  66 + keyUp = false;
  67 + }
  68 + else
  69 + {
  70 + Serial.println("a = true, keyUp = false");
  71 + a = true;
  72 + }
  73 + break;
  74 + case 115:
  75 + // s
  76 + if(keyUp == true)
  77 + {
  78 + Serial.println("s = false, keyUp = true");
  79 + s = false;
  80 + keyUp = false;
  81 + }
  82 + else
  83 + {
  84 + Serial.println("s = true, keyUp = false");
  85 + s = true;
  86 + }
  87 + break;
  88 + case 100:
  89 + // d
  90 + if(keyUp == true)
  91 + {
  92 + Serial.println("d = false, keyUp = true");
  93 + d = false;
  94 + keyUp = false;
  95 + }
  96 + else
  97 + {
  98 + Serial.println("d = true, keyUp = false");
  99 + d = true;
  100 + }
  101 + break;
  102 + default:
  103 + Serial.println("got nothing, full stop");
  104 + // full stop
  105 + keyUp = false;
  106 + w = false;
  107 + a = false;
  108 + s = false;
  109 + d = false;
  110 + analogWrite(E1, 0);
  111 + analogWrite(E2, 0);
  112 + }
  113 + }
  114 +
  115 + // if forward
  116 + if(w)
  117 + {
  118 + // Serial.println("w active");
  119 + digitalWrite(M1, HIGH);
  120 + digitalWrite(M2, HIGH);
  121 + analogWrite(E1, 255);
  122 + analogWrite(E2, 255);
  123 + }
  124 + else if(s)
  125 + {
  126 + // Serial.println("s active");
  127 + digitalWrite(M1, LOW);
  128 + digitalWrite(M2, LOW);
  129 + analogWrite(E1, 255);
  130 + analogWrite(E2, 255);
  131 + }
  132 + else
  133 + {
  134 + // Serial.println("s/w not active");
  135 + analogWrite(E1, 0);
  136 + analogWrite(E2, 0);
  137 + }
  138 +
  139 + if(a)
  140 + {
  141 + // Serial.println("a active");
  142 + analogWrite(E1, 255);
  143 + analogWrite(E2, 64);
  144 + }
  145 + else if(d)
  146 + {
  147 + // Serial.println("d active");
  148 + analogWrite(E1, 64);
  149 + analogWrite(E2, 255);
  150 + }
  151 +
  152 +}
BIN  engine/engines/applet/engines.elf
Binary file not shown
239 engine/engines/applet/engines.hex
... ... @@ -0,0 +1,239 @@
  1 +:100000000C9462000C948A000C948A000C948A0070
  2 +:100010000C948A000C948A000C948A000C948A0038
  3 +:100020000C948A000C948A000C948A000C948A0028
  4 +:100030000C948A000C948A000C948A000C948A0018
  5 +:100040000C94EF050C948A000C94E1030C942804A2
  6 +:100050000C948A000C948A000C948A000C948A00F8
  7 +:100060000C948A000C948A000000000024002700F1
  8 +:100070002A0000000000250028002B0000000000DE
  9 +:1000800023002600290004040404040404040202DA
  10 +:100090000202020203030303030301020408102007
  11 +:1000A0004080010204081020010204081020000012
  12 +:1000B0000007000201000003040600000000000029
  13 +:1000C00000009F0511241FBECFEFD8E0DEBFCDBFDB
  14 +:1000D00012E0A0E0B1E0E4ECFDE002C005900D927A
  15 +:1000E000A230B107D9F712E0A2E0B2E001C01D9240
  16 +:1000F000A93BB107E1F710E0C4ECD0E004C02297BF
  17 +:10010000FE010E94DC06C23CD107C9F70E949A0298
  18 +:100110000C94E0060C94000081E992E00E94F70440
  19 +:10012000181619060CF0BBC081E992E00E94280560
  20 +:10013000909303028093020281E992E060E071E013
  21 +:100140000E94CA03609102027091030281E992E069
  22 +:100150004AE050E00E94AB036091020270910302FA
  23 +:1001600081E992E04AE050E00E94AB0380910202F4
  24 +:10017000909103028436910509F45EC0853691059D
  25 +:1001800044F48034910569F08136910509F06BC023
  26 +:1001900023C083379105C9F18737910509F063C002
  27 +:1001A00004C081E0809308027AC08091080288230D
  28 +:1001B00049F081E992E06DE071E00E94CD03109278
  29 +:1001C000040216C081E992E065E271E00E94CD036D
  30 +:1001D00081E08093040263C080910802882359F073
  31 +:1001E00081E992E06DE371E00E94CD031092050277
  32 +:1001F0001092080254C081E992E065E571E00E9426
  33 +:10020000CD0381E0809305024AC0809108028823D3
  34 +:1002100049F081E992E06DE671E00E94CD03109211
  35 +:100220000602E6CF81E992E065E871E00E94CD0325
  36 +:1002300081E08093060233C080910802882349F050
  37 +:1002400081E992E06DE971E00E94CD03109207020E
  38 +:10025000CFCF81E992E065EB71E00E94CD0381E0B0
  39 +:10026000809307021CC081E992E06DEC71E00E946E
  40 +:10027000CD0310920802109204021092050210920F
  41 +:100280000602109207028091EA0160E070E00E948D
  42 +:10029000C0018091EE0160E070E00E94C001809199
  43 +:1002A0000402882349F08091EC0161E00E9446023B
  44 +:1002B0008091F00161E00CC0809106028823A9F0D2
  45 +:1002C0008091EC0160E00E9446028091F00160E0C4
  46 +:1002D0000E9446028091EA016FEF70E00E94C00127
  47 +:1002E0008091EE016FEF70E00AC08091EA0160E05A
  48 +:1002F00070E00E94C0018091EE0160E070E00E9419
  49 +:10030000C00180910502882369F08091EA016FEFB6
  50 +:1003100070E00E94C0018091EE0160E470E00E94F4
  51 +:10032000C001089580910702882361F08091EA015D
  52 +:1003300060E470E00E94C0018091EE016FEF70E018
  53 +:100340000E94C00108950F931F938091EC0161E01A
  54 +:100350000E9420028091F00161E00E94200201E9E8
  55 +:1003600012E0C80140E855E260E070E00E947104CC
  56 +:10037000C80164EE71E00E94CD031F910F910895B2
  57 +:100380001F93CF93DF93182FEB0161E00E942002AF
  58 +:10039000209709F44AC0CF3FD10509F449C0E12FA5
  59 +:1003A000F0E0E255FF4F84918330C1F0843028F4AF
  60 +:1003B000813051F08230B1F50CC0863019F18730B0
  61 +:1003C00049F1843079F514C084B5806884BDC7BD17
  62 +:1003D00033C084B5806284BDC8BD2EC080918000CA
  63 +:1003E000806880938000D0938900C093880024C0E7
  64 +:1003F00080918000806280938000D0938B00C093B6
  65 +:100400008A001AC08091B00080688093B000C093C9
  66 +:10041000B30012C08091B00080628093B000C0939E
  67 +:10042000B4000AC0C038D1051CF4812F60E002C0BE
  68 +:10043000812F61E00E944602DF91CF911F910895C4
  69 +:10044000482F50E0CA0186569F4FFC0124914A571D
  70 +:100450005F4FFA0184918823C1F0E82FF0E0EE0F9E
  71 +:10046000FF1FE859FF4FA591B491662341F49FB750
  72 +:10047000F8948C91209582238C939FBF08959FB709
  73 +:10048000F8948C91822B8C939FBF0895482F50E055
  74 +:10049000CA0182559F4FFC012491CA0186569F4F85
  75 +:1004A000FC0194914A575F4FFA013491332309F4C8
  76 +:1004B00040C0222351F1233071F0243028F4213040
  77 +:1004C000A1F0223011F514C02630B1F02730C1F070
  78 +:1004D0002430D9F404C0809180008F7703C08091CC
  79 +:1004E00080008F7D8093800010C084B58F7702C01C
  80 +:1004F00084B58F7D84BD09C08091B0008F7703C023
  81 +:100500008091B0008F7D8093B000E32FF0E0EE0F7C
  82 +:10051000FF1FEE58FF4FA591B4912FB7F8946623B3
  83 +:1005200021F48C919095892302C08C91892B8C9316
  84 +:100530002FBF0895CF93DF930E9437060E94A30137
  85 +:10054000C5E1D4E00E948C002097E1F30E941504DD
  86 +:10055000F9CFCF92DF92EF92FF920F931F93CF9339
  87 +:10056000DF937C016B018A01C0E0D0E00FC0D601AF
  88 +:100570006D916D01D701ED91FC910190F081E02D1D
  89 +:10058000C7010995C80FD91F015010400115110569
  90 +:1005900071F7CE01DF91CF911F910F91FF90EF90F6
  91 +:1005A000DF90CF900895EF92FF920F931F938C01ED
  92 +:1005B000DC01ED91FC910190F081E02D6DE0099559
  93 +:1005C0007C01D801ED91FC910190F081E02DC801F2
  94 +:1005D0006AE009959C012E0D3F1DC9011F910F91E5
  95 +:1005E000FF90EF900895CF93DF93DB010D900020F3
  96 +:1005F000E9F71197A61BB70BEC01E881F98102809E
  97 +:10060000F381E02DAD010995DF91CF9108954F92CF
  98 +:100610005F927F928F929F92AF92BF92CF92DF9222
  99 +:10062000EF92FF920F931F93DF93CF93CDB7DEB777
  100 +:10063000A1970FB6F894DEBF0FBECDBF2C01742E6C
  101 +:10064000CB01223008F42AE019A231E2C32ED12CCA
  102 +:10065000CC0EDD1E822E9924AA24BB24672D752F73
  103 +:10066000A50194010E94A40679018A01C801B7017D
  104 +:10067000A50194010E947206472D461B0894C108EB
  105 +:10068000D1084A3014F4405D01C0495CF601408352
  106 +:10069000E114F1040105110521F07E2C5F2DC80144
  107 +:1006A000DDCFC201B6010E94F302A1960FB6F89405
  108 +:1006B000DEBF0FBECDBFCF91DF911F910F91FF9095
  109 +:1006C000EF90DF90CF90BF90AF909F908F907F90F2
  110 +:1006D0005F904F900895CF92DF92EF92FF920F9329
  111 +:1006E0001F93CF93DF93EC016A017B012115310544
  112 +:1006F00041F4E881F9810190F081E02D642F0995A2
  113 +:100700001FC02A303105D1F477FF17C0E881F98185
  114 +:100710000190F081E02D6DE209958C014427552769
  115 +:10072000BA014C195D096E097F09CE012AE00E94C9
  116 +:1007300007039801280F391F04C02AE00E9407030D
  117 +:100740009C01C901DF91CF911F910F91FF90EF9014
  118 +:10075000DF90CF900895CF92DF92EF92FF920F93A8
  119 +:100760001F936C017B019A010027F7FC0095102F65
  120 +:10077000B801A7010E946B038C01C6010E94D3023D
  121 +:10078000080F191FC8011F910F91FF90EF90DF9084
  122 +:10079000CF9008950E94F3020895EF92FF920F9375
  123 +:1007A0001F937C010E94F3028C01C7010E94D302B7
  124 +:1007B000080F191FC8011F910F91FF90EF90089526
  125 +:1007C00008951F920F920FB60F9211242F933F930B
  126 +:1007D0004F938F939F93EF93FF934091C600209187
  127 +:1007E000490230914A022F5F3F4F2F733070809142
  128 +:1007F0004B0290914C022817390759F0E0914902B9
  129 +:10080000F0914A02E75FFD4F408330934A02209304
  130 +:100810004902FF91EF919F918F914F913F912F91BD
  131 +:100820000F900FBE0F901F901895E0919D02F091D0
  132 +:100830009E02E05CFF4F8191919120813181821B6A
  133 +:10084000930B8F739070892B11F00E94E003089531
  134 +:100850001F920F920FB60F9211242F933F934F9335
  135 +:100860005F936F937F938F939F93AF93BF93EF9318
  136 +:10087000FF9320918D0230918E0280918F02909192
  137 +:1008800090022817390731F48091C1008F7D809341
  138 +:10089000C10016C0E0918F02F0919002E35BFD4F22
  139 +:1008A000408180918F0290919002019660E470E007
  140 +:1008B0000E9491069093900280938F024093C6000D
  141 +:1008C000FF91EF91BF91AF919F918F917F916F9128
  142 +:1008D0005F914F913F912F910F900FBE0F901F90FE
  143 +:1008E0001895AF92BF92DF92EF92FF920F931F93F2
  144 +:1008F000CF93DF93EC017A018B01DD24403081EE50
  145 +:10090000580780E0680780E0780711F0DD24D39471
  146 +:1009100091E0A92EB12CEC89FD89DD2069F0C5019B
  147 +:100920000E8C02C0880F991F0A94E2F7808360E062
  148 +:1009300079E08DE390E005C0108260E874E88EE114
  149 +:1009400090E0A80197010E94A40621503040404049
  150 +:100950005040569547953795279580E120303807C8
  151 +:1009600020F0DD2011F0DD24D6CFE889F98930832D
  152 +:10097000EA89FB892083EE89FF89408121E030E00C
  153 +:10098000C9010A8C02C0880F991F0A94E2F7482B0C
  154 +:100990004083EE89FF894081C9010B8C02C0880F1A
  155 +:1009A000991F0A94E2F7482B4083EE89FF89408122
  156 +:1009B000C9010C8C02C0880F991F0A94E2F7482BDA
  157 +:1009C0004083EE89FF8980810D8C02C0220F331F86
  158 +:1009D0000A94E2F7209528232083DF91CF911F917D
  159 +:1009E0000F91FF90EF90DF90BF90AF900895DC01E2
  160 +:1009F0001C96ED91FC911D97E05CFF4F2191319188
  161 +:100A000080819181281B390B2F733070C9010895A3
  162 +:100A1000DC011C96ED91FC911D97E05CFF4F20815D
  163 +:100A20003181E054F040DF01AE5BBF4F8D919C916E
  164 +:100A300011972817390719F42FEF3FEF07C08D9151
  165 +:100A40009C91E80FF91F8081282F30E0C90108959B
  166 +:100A5000DC011C96ED91FC911D97E05CFF4F20811D
  167 +:100A60003181E054F040DF01AE5BBF4F8D919C912E
  168 +:100A700011972817390719F42FEF3FEF10C08D9108
  169 +:100A80009C911197E80FF91F20818D919C911197EE
  170 +:100A900001968F73907011969C938E9330E0C901EC
  171 +:100AA0000895FC0186859785DC01A05CBF4FFC01A1
  172 +:100AB000EE5BFF4F2D913C9111978081918128171A
  173 +:100AC0003907C1F70895CF93DF93EC01462FEE85E8
  174 +:100AD000FF85E05CFF4F80819181E054F0400196FA
  175 +:100AE00060E470E00E9491069C01DF01AE5BBF4FA5
  176 +:100AF0008D919C91119728173907D1F3E05CFF4F36
  177 +:100B000080819181E054F040E80FF91F4083EE8529
  178 +:100B1000FF85E05CFF4F31832083EE89FF892081D0
  179 +:100B200081E090E00D8C02C0880F991F0A94E2F7D3
  180 +:100B3000282B208381E090E0DF91CF9108951092DF
  181 +:100B400094021092930288EE93E0A0E0B0E08093CC
  182 +:100B5000950290939602A0939702B093980286EF25
  183 +:100B600091E0909392028093910289E092E09093B9
  184 +:100B70009E0280939D028DE492E09093A002809368
  185 +:100B80009F0285EC90E09093A2028093A10284ECF6
  186 +:100B900090E09093A4028093A30280EC90E0909365
  187 +:100BA000A6028093A50281EC90E09093A802809326
  188 +:100BB000A70286EC90E09093AA028093A90284E0B9
  189 +:100BC0008093AB0283E08093AC0287E08093AD0218
  190 +:100BD00085E08093AE0281E08093AF0208951F927A
  191 +:100BE0000F920FB60F9211242F933F938F939F93E1
  192 +:100BF000AF93BF938091B4029091B502A091B602D9
  193 +:100C0000B091B7023091B8020196A11DB11D232FFA
  194 +:100C10002D5F2D3720F02D570196A11DB11D20937A
  195 +:100C2000B8028093B4029093B502A093B602B09339
  196 +:100C3000B7028091B0029091B102A091B202B0913E
  197 +:100C4000B3020196A11DB11D8093B0029093B10231
  198 +:100C5000A093B202B093B302BF91AF919F918F91D5
  199 +:100C60003F912F910F900FBE0F901F901895789481
  200 +:100C700084B5826084BD84B5816084BD85B58260A1
  201 +:100C800085BD85B5816085BDEEE6F0E0808181603F
  202 +:100C90008083E1E8F0E0108280818260808380813F
  203 +:100CA00081608083E0E8F0E0808181608083E1EB17
  204 +:100CB000F0E0808184608083E0EBF0E080818160FF
  205 +:100CC0008083EAE7F0E080818460808380818260B5
  206 +:100CD000808380818160808380818068808310929E
  207 +:100CE000C1000895629FD001739FF001829FE00DC3
  208 +:100CF000F11D649FE00DF11D929FF00D839FF00D9B
  209 +:100D0000749FF00D659FF00D9927729FB00DE11D46
  210 +:100D1000F91F639FB00DE11DF91FBD01CF01112423
  211 +:100D2000089597FB092E07260AD077FD04D02ED010
  212 +:100D300006D000201AF4709561957F4F0895F6F75C
  213 +:100D4000909581959F4F0895A1E21A2EAA1BBB1B77
  214 +:100D5000FD010DC0AA1FBB1FEE1FFF1FA217B30787
  215 +:100D6000E407F50720F0A21BB30BE40BF50B661F9D
  216 +:100D7000771F881F991F1A9469F760957095809561
  217 +:100D800090959B01AC01BD01CF010895AA1BBB1B2F
  218 +:100D900051E107C0AA1FBB1FA617B70710F0A61B7B
  219 +:100DA000B70B881F991F5A95A9F780959095BC019C
  220 +:100DB000CD010895EE0FFF1F0590F491E02D0994E9
  221 +:040DC000F894FFCFD5
  222 +:100DC400492072656365697665643A200077203D41
  223 +:100DD4002066616C73652C206B65795570203D200D
  224 +:100DE400747275650077203D20747275652C206BD4
  225 +:100DF40065795570203D2066616C73650061203D06
  226 +:100E04002066616C73652C206B65795570203D20DC
  227 +:100E1400747275650061203D20747275652C206BB9
  228 +:100E240065795570203D2066616C73650073203DC3
  229 +:100E34002066616C73652C206B65795570203D20AC
  230 +:100E4400747275650073203D20747275652C206B77
  231 +:100E540065795570203D2066616C73650064203DA2
  232 +:100E64002066616C73652C206B65795570203D207C
  233 +:100E7400747275650064203D20747275652C206B56
  234 +:100E840065795570203D2066616C736500676F74E9
  235 +:100E9400206E6F7468696E672C2066756C6C2073A5
  236 +:100EA400746F7000737461727400060007000500AB
  237 +:100EB4000400000000006305A902F70428050805E2
  238 +:020EC4005105D6
  239 +:00000001FF
BIN  engine/engines/applet/engines.o
Binary file not shown
419 mp3/mp3.ino
... ... @@ -1,85 +1,364 @@
1   -//Arduino PWM Speed ControlÔºö
2   -int E1 = 6;
3   -int M1 = 7;
4   -int E2 = 5;
5   -int M2 = 4;
6   -
7   -void setup()
8   -{
9   - pinMode(M1, OUTPUT);
10   - pinMode(M2, OUTPUT);
11   -
12   - Serial.begin(9600);
13   - Serial.println("start");
14   -
15   -}
16   -
17   -void loop()
18   -{
19   -// forward(5);
20   -// left(1);
21   -// right(1);
22   -// backward(5);
23   - delay(2000);
24   -}
  1 +/*
  2 + 4-28-2011
  3 + Spark Fun Electronics 2011
  4 + Nathan Seidle
  5 +
  6 + This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
  7 +
  8 + This example code plays a MP3 from the SD card called 'track001.mp3'. The theory is that you can load a
  9 + microSD card up with a bunch of MP3s and then play a given 'track' depending on some sort of input such
  10 + as which pin is pulled low.
  11 +
  12 + It relies on the sdfatlib from Bill Greiman:
  13 + http://code.google.com/p/sdfatlib/
  14 + You will need to download and install his library. To compile, you MUST change Sd2PinMap.h of the SDfatlib!
  15 + The default SS_PIN = 10;. You must change this line under the ATmega328/Arduino area of code to
  16 + uint8_t const SS_PIN = 9;. This will cause the sdfatlib to use pin 9 as the 'chip select' for the
  17 + microSD card on pin 9 of the Arduino so that the layout of the shield works.
  18 +
  19 + Attach the shield to an Arduino. Load code (after editing Sd2PinMap.h) then open the terminal at 57600bps. This
  20 + example shows that it takes ~30ms to load up the VS1053 buffer. We can then do whatever we want for ~100ms
  21 + before we need to return to filling the buffer (for another 30ms).
  22 +
  23 + This code is heavily based on the example code I wrote to control the MP3 shield found here:
  24 + http://www.sparkfun.com/products/9736
  25 + This example code extends the previous example by reading the MP3 from an SD card and file rather than from internal
  26 + memory of the ATmega. Because the current MP3 shield does not have a microSD socket, you will need to add the microSD
  27 + shield to your Arduino stack.
  28 +
  29 + The main gotcha from all of this is that you have to make sure your CS pins for each device on an SPI bus is carefully
  30 + declared. For the SS pin (aka CS) on the SD FAT libaray, you need to correctly set it within Sd2PinMap.h. The default
  31 + pin in Sd2PinMap.h is 10. If you're using the SparkFun microSD shield with the SparkFun MP3 shield, the SD CS pin
  32 + is pin 9.
  33 +
  34 + Four pins are needed to control the VS1503:
  35 + DREQ
  36 + CS
  37 + DCS
  38 + Reset (optional but good to have access to)
  39 + Plus the SPI bus
  40 +
  41 + Only the SPI bus pins and another CS pin are needed to control the microSD card.
  42 +
  43 + What surprised me is the fact that with a normal MP3 we can do other things for up to 100ms while the MP3 IC crunches
  44 + through it's fairly large buffer of 2048 bytes. As long as you keep your sensor checks or serial reporting to under
  45 + 100ms and leave ~30ms to then replenish the MP3 buffer, you can do quite a lot while the MP3 is playing glitch free.
  46 +
  47 + */
25 48
26   -void forward(int sec)
27   -{
28   - Serial.println("forward");
29   - // set the engines forward
30   - digitalWrite(M1, LOW);
31   - digitalWrite(M2, LOW);
32   -
33   - // full power to the thrusters!
34   - analogWrite(E1, 255);
35   - analogWrite(E2, 255);
  49 +#include <SPI.h>
  50 +
  51 +//Add the SdFat Libraries
  52 +#include <SdFat.h>
  53 +#include <SdFatUtil.h>
  54 +
  55 +//Create the variables to be used by SdFat Library
  56 +Sd2Card card;
  57 +SdVolume volume;
  58 +SdFile root;
  59 +SdFile track;
  60 +
  61 +//This is the name of the file on the microSD card you would like to play
  62 +//Stick with normal 8.3 nomeclature. All lower-case works well.
  63 +//Note: you must name the tracks on the SD card with 001, 002, 003, etc.
  64 +//For example, the code is expecting to play 'track002.mp3', not track2.mp3.
  65 +char trackName[] = "track001.mp3";
  66 +int trackNumber = 1;
  67 +
  68 +char errorMsg[100]; //This is a generic array used for sprintf of error messages
  69 +
  70 +#define TRUE 0
  71 +#define FALSE 1
  72 +
  73 +//MP3 Player Shield pin mapping. See the schematic
  74 +#define MP3_XCS 6 //Control Chip Select Pin (for accessing SPI Control/Status registers)
  75 +#define MP3_XDCS 7 //Data Chip Select / BSYNC Pin
  76 +#define MP3_DREQ 2 //Data Request Pin: Player asks for more data
  77 +#define MP3_RESET 8 //Reset is active low
  78 +//Remember you have to edit the Sd2PinMap.h of the sdfatlib library to correct control the SD card.
  79 +
  80 +//VS10xx SCI Registers
  81 +#define SCI_MODE 0x00
  82 +#define SCI_STATUS 0x01
  83 +#define SCI_BASS 0x02
  84 +#define SCI_CLOCKF 0x03
  85 +#define SCI_DECODE_TIME 0x04
  86 +#define SCI_AUDATA 0x05
  87 +#define SCI_WRAM 0x06
  88 +#define SCI_WRAMADDR 0x07
  89 +#define SCI_HDAT0 0x08
  90 +#define SCI_HDAT1 0x09
  91 +#define SCI_AIADDR 0x0A
  92 +#define SCI_VOL 0x0B
  93 +#define SCI_AICTRL0 0x0C
  94 +#define SCI_AICTRL1 0x0D
  95 +#define SCI_AICTRL2 0x0E
  96 +#define SCI_AICTRL3 0x0F
  97 +
  98 +const int buttonPin = 5; // the number of the pushbutton pin
  99 +const int ledPin = 13; // the number of the LED pin
  100 +
  101 +// Variables will change:
  102 +int ledState = HIGH; // the current state of the output pin
  103 +int buttonState; // the current reading from the input pin
  104 +int lastButtonState = LOW; // the previous reading from the input pin
  105 +int lastButtonReading = 0;
  106 +long lastDebounceTime = 0; // the last time the output pin was toggled
  107 +long debounceDelay = 1500; // the debounce time; increase if the output flickers
  108 +
  109 +
  110 +void setup() {
  111 + pinMode(MP3_DREQ, INPUT);
  112 + pinMode(MP3_XCS, OUTPUT);
  113 + pinMode(MP3_XDCS, OUTPUT);
  114 + pinMode(MP3_RESET, OUTPUT);
36 115
37   - delay(1000*sec);
  116 + // initialize the LED pin as an output:
  117 + pinMode(ledPin, OUTPUT);
  118 + // initialize the pushbutton pin as an input:
  119 + pinMode(buttonPin, INPUT);
  120 +
  121 +
  122 + digitalWrite(MP3_XCS, HIGH); //Deselect Control
  123 + digitalWrite(MP3_XDCS, HIGH); //Deselect Data
  124 + digitalWrite(MP3_RESET, LOW); //Put VS1053 into hardware reset
  125 +
  126 + Serial.begin(57600); //Use serial for debugging
  127 + Serial.println("MP3 Testing");
  128 +
  129 + //Setup SD card interface
  130 + pinMode(10, OUTPUT); //Pin 10 must be set as an output for the SD communication to work.
  131 + if (!card.init(SPI_FULL_SPEED,9)) Serial.println("Error: Card init"); //Initialize the SD card and configure the I/O pins.
  132 + if (!volume.init(&card)) Serial.println("Error: Volume ini"); //Initialize a volume on the SD card.
  133 + if (!root.openRoot(&volume)) Serial.println("Error: Opening root"); //Open the root directory in the volume.
  134 +
  135 + //We have no need to setup SPI for VS1053 because this has already been done by the SDfatlib
  136 +
  137 + //From page 12 of datasheet, max SCI reads are CLKI/7. Input clock is 12.288MHz.
  138 + //Internal clock multiplier is 1.0x after power up.
  139 + //Therefore, max SPI speed is 1.75MHz. We will use 1MHz to be safe.
  140 + SPI.setClockDivider(SPI_CLOCK_DIV16); //Set SPI bus speed to 1MHz (16MHz / 16 = 1MHz)
  141 + SPI.transfer(0xFF); //Throw a dummy byte at the bus
  142 + //Initialize VS1053 chip
  143 + delay(10);
  144 + digitalWrite(MP3_RESET, HIGH); //Bring up VS1053
  145 + //delay(10); //We don't need this delay because any register changes will check for a high DREQ
  146 +
  147 + Mp3SetVolume(20, 20); //Set initial volume (20 = -10dB) LOUD
  148 +// Mp3SetVolume(40, 40); //Set initial volume (20 = -10dB) Manageable
  149 + //Mp3SetVolume(80, 80); //Set initial volume (20 = -10dB) More quiet
  150 +
  151 + //Let's check the status of the VS1053
  152 + int MP3Mode = Mp3ReadRegister(SCI_MODE);
  153 + int MP3Status = Mp3ReadRegister(SCI_STATUS);
  154 + int MP3Clock = Mp3ReadRegister(SCI_CLOCKF);
  155 +
  156 + Serial.print("SCI_Mode (0x4800) = 0x");
  157 + Serial.println(MP3Mode, HEX);
  158 +
  159 + Serial.print("SCI_Status (0x48) = 0x");
  160 + Serial.println(MP3Status, HEX);
  161 +
  162 + int vsVersion = (MP3Status >> 4) & 0x000F; //Mask out only the four version bits
  163 + Serial.print("VS Version (VS1053 is 4) = ");
  164 + Serial.println(vsVersion, DEC); //The 1053B should respond with 4. VS1001 = 0, VS1011 = 1, VS1002 = 2, VS1003 = 3
  165 +
  166 + Serial.print("SCI_ClockF = 0x");
  167 + Serial.println(MP3Clock, HEX);
  168 +
  169 + //Now that we have the VS1053 up and running, increase the internal clock multiplier and up our SPI rate
  170 + Mp3WriteRegister(SCI_CLOCKF, 0x60, 0x00); //Set multiplier to 3.0x
  171 +
  172 + //From page 12 of datasheet, max SCI reads are CLKI/7. Input clock is 12.288MHz.
  173 + //Internal clock multiplier is now 3x.
  174 + //Therefore, max SPI speed is 5MHz. 4MHz will be safe.
  175 + SPI.setClockDivider(SPI_CLOCK_DIV4); //Set SPI bus speed to 4MHz (16MHz / 4 = 4MHz)
  176 +
  177 + MP3Clock = Mp3ReadRegister(SCI_CLOCKF);
  178 + Serial.print("SCI_ClockF = 0x");
  179 + Serial.println(MP3Clock, HEX);
  180 +
  181 + //MP3 IC setup complete
  182 +}
  183 +
  184 +void loop(){
  185 + // read the state of the pushbutton value:
  186 + int reading = digitalRead(buttonPin);
  187 +// if(reading > 1022)
  188 +// {
  189 + Serial.println(reading);
  190 +// }
  191 +// if(reading > 1022 && lastButtonReading < 1000)
  192 +// {
  193 +// Serial.println(reading);
  194 +// }
  195 +// if(lastButtonReading > 1000 && reading > 1022)
  196 +// {
  197 +// Serial.println("no change");
  198 +// }
38 199
39   - analogWrite(E1, 0);
40   - analogWrite(E2, 0);
  200 + lastButtonReading = reading;
  201 +
  202 +//
  203 +// if (reading != lastButtonState) {
  204 +// // reset the debouncing timer
  205 +// lastDebounceTime = millis();
  206 +// }
  207 +//
  208 +// if ((millis() - lastDebounceTime) > debounceDelay) {
  209 +// // whatever the reading is at, it's been there for longer
  210 +// // than the debounce delay, so take it as the actual current state:
  211 +// buttonState = reading;
  212 +//
  213 +//// playMP3("track001.mp3");
  214 +// }
  215 +// digitalWrite(ledPin, buttonState);
  216 +// if(buttonState)
  217 +// {
  218 +// Serial.println(buttonState);
  219 +// Serial.println(lastButtonState);
  220 +// Serial.println("click");
  221 +//// reading = LOW;
  222 +// }
  223 +//
  224 +// // save the reading. Next time through the loop,
  225 +// // it'll be the lastButtonState:
  226 +// lastButtonState = reading;
41 227 }
42 228
43   -void left(int sec)
44   -{
45   - Serial.println("left");
46   - digitalWrite(M1, LOW);
  229 +//PlayMP3 pulls 32 byte chunks from the SD card and throws them at the VS1053
  230 +//We monitor the DREQ (data request pin). If it goes low then we determine if
  231 +//we need new data or not. If yes, pull new from SD card. Then throw the data
  232 +//at the VS1053 until it is full.
  233 +void playMP3(char* fileName) {
  234 + if (!track.open(&root, fileName, O_READ)) { //Open the file in read mode.
  235 + sprintf(errorMsg, "Failed to open %s", fileName);
  236 + Serial.println(errorMsg);
  237 + return;
  238 + }
47 239
48   - // full power to the thrusters!
49   - analogWrite(E1, 255);
50   -
51   - delay(1000*sec);
  240 + Serial.println("Track open");
  241 +
  242 + uint8_t mp3DataBuffer[32]; //Buffer of 32 bytes. VS1053 can take 32 bytes at a go.
  243 + //track.read(mp3DataBuffer, sizeof(mp3DataBuffer)); //Read the first 32 bytes of the song
  244 + int need_data = TRUE;
  245 + long replenish_time = millis();
  246 +
  247 + Serial.println("Start MP3 decoding");
  248 +
  249 + while(1) {
  250 + while(!digitalRead(MP3_DREQ)) {
  251 + //DREQ is low while the receive buffer is full
  252 + //You can do something else here, the buffer of the MP3 is full and happy.
  253 + //Maybe set the or test to see how much we can delay before we hear audible glitches
  254 +
  255 + //If the MP3 IC is happy, but we need to read new data from the SD, now is a great time to do so
  256 + if(need_data == TRUE) {
  257 + if(!track.read(mp3DataBuffer, sizeof(mp3DataBuffer))) { //Try reading 32 new bytes of the song
  258 + //Oh no! There is no data left to read!
  259 + //Time to exit
  260 + break;
  261 + }
  262 + need_data = FALSE;
  263 + }
  264 +
  265 + //Serial.println("."); //Print a character to show we are doing nothing
  266 +
  267 + //This is here to show how much time is spent transferring new bytes to the VS1053 buffer. Relies on replenish_time below.
  268 + Serial.print("Time to replenish buffer: ");
  269 + Serial.print(millis() - replenish_time, DEC);
  270 + Serial.print("ms");
  271 +
  272 + //Test to see just how much we can do before the audio starts to glitch
  273 + long start_time = millis();
  274 + //delay(150); //Do NOTHING - audible glitches
  275 + //delay(135); //Do NOTHING - audible glitches
  276 + //delay(120); //Do NOTHING - barely audible glitches
  277 + delay(100); //Do NOTHING - sounds fine
  278 + Serial.print(" Idle time: ");
  279 + Serial.print(millis() - start_time, DEC);
  280 + Serial.println("ms");
  281 + //Look at that! We can actually do quite a lot without the audio glitching
  282 +
  283 + //Now that we've completely emptied the VS1053 buffer (2048 bytes) let's see how much
  284 + //time the VS1053 keeps the DREQ line high, indicating it needs to be fed
  285 + replenish_time = millis();
  286 + }
  287 +
  288 +
  289 + if(need_data == TRUE){ //This is here in case we haven't had any free time to load new data
  290 + if(!track.read(mp3DataBuffer, sizeof(mp3DataBuffer))) { //Go out to SD card and try reading 32 new bytes of the song
  291 + //Oh no! There is no data left to read!
  292 + //Time to exit
  293 + break;
  294 + }
  295 + need_data = FALSE;
  296 + }
  297 +
  298 + //Once DREQ is released (high) we now feed 32 bytes of data to the VS1053 from our SD read buffer
  299 + digitalWrite(MP3_XDCS, LOW); //Select Data
  300 + for(int y = 0 ; y < sizeof(mp3DataBuffer) ; y++) {
  301 + SPI.transfer(mp3DataBuffer[y]); // Send SPI byte
  302 + }
  303 +
  304 + digitalWrite(MP3_XDCS, HIGH); //Deselect Data
  305 + need_data = TRUE; //We've just dumped 32 bytes into VS1053 so our SD read buffer is empty. Set flag so we go get more data
  306 + }
  307 +
  308 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating transfer is complete
  309 + digitalWrite(MP3_XDCS, HIGH); //Deselect Data
52 310
53   - analogWrite(E1, 0);
  311 + track.close(); //Close out this track
  312 +
  313 + sprintf(errorMsg, "Track %s done!", fileName);
  314 + Serial.println(errorMsg);
54 315 }
55 316
56   -void right(int sec)
57   -{
58   - Serial.println("right");
59   - digitalWrite(M2, LOW);
  317 +//Write to VS10xx register
  318 +//SCI: Data transfers are always 16bit. When a new SCI operation comes in
  319 +//DREQ goes low. We then have to wait for DREQ to go high again.
  320 +//XCS should be low for the full duration of operation.
  321 +void Mp3WriteRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte){
  322 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating IC is available
  323 + digitalWrite(MP3_XCS, LOW); //Select control
60 324
61   - // full power to the thrusters!
62   - analogWrite(E2, 255);
63   -
64   - delay(1000*sec);
65   -
66   - analogWrite(E2, 0);
  325 + //SCI consists of instruction byte, address byte, and 16-bit data word.
  326 + SPI.transfer(0x02); //Write instruction
  327 + SPI.transfer(addressbyte);
  328 + SPI.transfer(highbyte);
  329 + SPI.transfer(lowbyte);
  330 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
  331 + digitalWrite(MP3_XCS, HIGH); //Deselect Control
67 332 }
68 333
69   -void backward(int sec)
70   -{
71   - Serial.println("backward");
72   - digitalWrite(M1, HIGH);
73   - digitalWrite(M2, HIGH);
74   -
75   - // full power to the thrusters!
76   - analogWrite(E1, 255);
77   - analogWrite(E2, 255);
78   -
79   - delay(1000*sec);
80   -
81   - analogWrite(E1, 0);
82   - analogWrite(E2, 0);
  334 +//Read the 16-bit value of a VS10xx register
  335 +unsigned int Mp3ReadRegister (unsigned char addressbyte){
  336 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating IC is available
  337 + digitalWrite(MP3_XCS, LOW); //Select control
  338 +
  339 + //SCI consists of instruction byte, address byte, and 16-bit data word.
  340 + SPI.transfer(0x03); //Read instruction
  341 + SPI.transfer(addressbyte);
  342 +
  343 + char response1 = SPI.transfer(0xFF); //Read the first byte
  344 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
  345 + char response2 = SPI.transfer(0xFF); //Read the second byte
  346 + while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
  347 +
  348 + digitalWrite(MP3_XCS, HIGH); //Deselect Control
  349 +
  350 + int resultvalue = response1 << 8;
  351 + resultvalue |= response2;
  352 + return resultvalue;
  353 +}
  354 +
  355 +//Set VS10xx Volume Register
  356 +void Mp3SetVolume(unsigned char leftchannel, unsigned char rightchannel){
  357 + Mp3WriteRegister(SCI_VOL, leftchannel, rightchannel);
83 358 }
84 359
85 360
  361 +
  362 +
  363 +
  364 +
49 sweep/Capfile/Capfile.ino
... ... @@ -0,0 +1,49 @@
  1 +#include <Servo.h>
  2 +
  3 +Servo myservo; // create servo object to control a servo
  4 + // a maximum of eight servo objects can be created
  5 +
  6 +int pos = 0; // variable to store the servo position
  7 +int near = 0;
  8 +
  9 +void setup()
  10 +{
  11 + Serial.begin(9600);
  12 + Serial.println("start");
  13 + myservo.attach(9); // attaches the servo on pin 9 to the servo object
  14 +}
  15 +
  16 +
  17 +void loop()
  18 +{
  19 + Serial.println("start");
  20 + near = analogRead(0);
  21 + Serial.print("distance: ");
  22 + Serial.println(near);
  23 + sweep();
  24 +}
  25 +
  26 +void sweep()
  27 +{
  28 + for(pos=0;pos<180;pos+=5)
  29 + {
  30 + myservo.write(pos); // sweep start position
  31 + delay(15);
  32 + measure();
  33 + }
  34 +
  35 + for(pos = 180; pos>=1; pos-=5) // goes from 180 degrees to 0 degrees
  36 + {
  37 + myservo.write(pos); // tell servo to go to position in variable 'pos'
  38 + delay(15);
  39 + measure();
  40 + }
  41 +
  42 +}
  43 +
  44 +void measure()
  45 +{
  46 + near = analogRead(0);
  47 + Serial.print("distance: ");
  48 + Serial.println(near);
  49 +}

0 comments on commit 1794863

Please sign in to comment.
Something went wrong with that request. Please try again.