Skip to content

Commit

Permalink
Add dac2ch and pwm2ch libraries & examples, update ActivityBot 360 IR…
Browse files Browse the repository at this point in the history
… following examples
  • Loading branch information
AndyLindsay committed Nov 6, 2018
1 parent b5a0b0c commit ab8e160
Show file tree
Hide file tree
Showing 91 changed files with 11,229 additions and 2,027 deletions.
40 changes: 40 additions & 0 deletions Learn/Examples/Devices/Convert/Test libdac2ch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Test libdac2ch.c
Test the dac2ch library.
*/

#include "simpletools.h"
#include "dac2ch.h"

dac2ch *dacA;
dac2ch *dacB;

int main()
{
// Start two DAC processes, one 8-bit and one 10 bit
dacA = dac2ch_start(8);
dacB = dac2ch_start(10);

// Set P26 and P27 to 1.65 V, and P5 and P6 to 0.825 V.
dac2ch_set(dacA, 26, 0, 128);
dac2ch_set(dacB, 27, 0, 512);
dac2ch_set(dacA, 5, 1, 64);
dac2ch_set(dacB, 6, 1, 256);
pause(3000);

// Stop signals on P26 and P5 -> inputs
dac2ch_set(dacA, 26, 0, -1);
dac2ch_set(dacA, 5, 1, -1);
pause(3000);

// Switch outputs that were on P27 and P6 to P26 and P5.
dac2ch_set(dacB, 26, 0, 768);
dac2ch_set(dacB, 5, 1, 192);
pause(3000);

// End the dac2ch processes
dac2ch_stop(dacA);
dac2ch_stop(dacB);
}

9 changes: 9 additions & 0 deletions Learn/Examples/Devices/Convert/Test libdac2ch.side
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Test libdac2ch.c
>compiler=C
>memtype=cmm main ram compact
>optimize=-Os
>-m32bit-doubles
>-fno-exceptions
>defs::-std=c99
>-lm
>BOARD::ACTIVITYBOARD
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
libpwm2ch.c
Test libpwm2ch.c
Drive a pair of DC motors at 6/10 of full speed.
http://learn.parallax.com/propeller-c-tutorials
Test the pwm2ch library.
*/

#include "simpletools.h"
Expand All @@ -14,12 +12,12 @@ pwm2ch *pwmB;

int main()
{
// Start PWM process. Frequency = 1 kHz -> pulse period = 1 ms.
// Start PWM process A. Frequency = 1 kHz -> pulse period = 1 ms.
// Start PWM process B. Frequency = 1.2 kHz -> pulse period = 0.8333.. ms.
pwmA = pwm2ch_start(1000);
pwmB = pwm2ch_start(1200);
//high(27);

// Turn motors counterclockwise for 3 s.
// Set all four available channels.
pwm2ch_set(pwmA, 3, 0, 300);
pwm2ch_set(pwmA, 4, 1, 600);
pwm2ch_set(pwmB, 5, 0, 100);
Expand All @@ -29,40 +27,38 @@ int main()
// Stop signals on P3 and P5 -> inputs
pwm2ch_set(pwmA, 3, 0, -1);
pwm2ch_set(pwmB, 5, 0, -1);
//low(3);
pause(1000);
//input(3);


// Re-start channels with different duty cycles.
pwm2ch_set(pwmA, 3, 0, 100);
pwm2ch_set(pwmB, 5, 0, 50);
pause(1000);

// Turn off channels again
pwm2ch_set(pwmA, 3, 0, -1);
pwm2ch_set(pwmB, 5, 0, -1);
pause(1000);

// Move signals from other channels to P3 and P5
pwm2ch_set(pwmA, 3, 1, 600);
pwm2ch_set(pwmB, 5, 1, 600);
pause(1000);

// Move channels that were disabled to the ones just
// vacated.
pwm2ch_set(pwmA, 4, 0, 300);
pwm2ch_set(pwmB, 6, 0, 300);
pause(1000);







// Stop for 1 second.
// Set A process channels low.
// Set B process channels to inputs.
pwm2ch_set(pwmA, 3, 0, 0);
pwm2ch_set(pwmA, 4, 1, 0);
pwm2ch_set(pwmB, 5, 0, -1);
pwm2ch_set(pwmB, 6, 1, -1);
pause(1000);

// Turn motors counterclockwise for 3 s.
// Restart A channels in B channels that were just vacated.
pwm2ch_set(pwmA, 5, 0, 600);
pwm2ch_set(pwmA, 6, 1, 600);
pause(3000);
Expand Down
File renamed without changes.
20 changes: 8 additions & 12 deletions Learn/Examples/Robots/ActivityBot360/IR Follow the Leader.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "simpletools.h" // Include simpletools
#include "abdrive360.h"
#include "abvolts.h"

int irLeft, irRight; // Global variables
int errorLeft, errorRight;
Expand All @@ -17,33 +18,28 @@ int kp = -12;

int main() // Main function
{
pwm_start(255);

low(26); // D/A0 & D/A1 to 0 V
low(27);

while(1) // Main loop
{
irLeft = 0; // Detect variables to 0
irRight = 0;

for(int da = 189; da >= 0; da -= 27) // Sweep da pins
for(int da = 112; da >= 0; da -= 16) // Sweep da pins
{
pwm_set(26, 0, da);
pwm_set(27, 1, da);
da_out(0, da); // Set IR LED cathode volts
da_out(1, da);
freqout(11, 1, 38000); // Left IR LED light
irLeft += input(10); // Check left IR detector
irLeft += input(10); // No detect? Add 1 to irLeft
freqout(1, 1, 38000); // Repeat for right detector
irRight += input(2);
}

errorLeft = setPoint - irLeft;
errorLeft = setPoint - irLeft; // Error = want - measured
errorRight = setPoint - irRight;

driveLeft = kp * errorLeft;
driveLeft = kp * errorLeft; // Output = k * error
driveRight = kp * errorRight;

drive_speed(driveLeft, driveRight);
drive_speed(driveLeft, driveRight); // Set outputs
}
}

18 changes: 9 additions & 9 deletions Learn/Examples/Robots/ActivityBot360/Test IR Distances.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@

#include "simpletools.h" // Include simpletools
#include "abdrive360.h"
#include "abvolts.h"

int irLeft, irRight; // Global variables

int main() // Main function
{
pwm_start(255);

low(26); // D/A0 & D/A1 to 0 V
low(27);

while(1) // Main loop
{
irLeft = 0; // Detect variables to 0
irRight = 0;

for(int da = 189; da >= 0; da -= 27) // Sweep da pins
// Test if an object is detected with 9 increasing levels of IR LED
// brightness. Keep track of the number of times an object is NOT
// detected. That's its distance value.

for(int da = 112; da >= 0; da -= 16) // Sweep da pins
{
pwm_set(26, 0, da);
pwm_set(27, 1, da);
da_out(0, da); // Set IR LED cathode volts
da_out(1, da);
freqout(11, 1, 38000); // Left IR LED light
irLeft += input(10); // Check left IR detector
irLeft += input(10); // No detect? Add 1 to irLeft
freqout(1, 1, 38000); // Repeat for right detector
irRight += input(2);
}
Expand Down
12 changes: 12 additions & 0 deletions Learn/Simple Libraries Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ <h3 style="font-family:verdana;">Convert</h3>
</p>
</blockquote>

<p>
<blockquote>
<a href="Simple%20Libraries/Convert/libdac2ch/Documentation%20dac2ch%20Library.html" target="_blank">dac2ch.h</a> - Up to 2 Channels per cog of D/A signals. Supports multiple cogs.
</p>
</blockquote>

<p>
<blockquote>
<a href="Simple%20Libraries/Convert/libpwm2ch/Documentation%20pwm2ch%20Library.html" target="_blank">pwm2ch.h</a> - Up to 2 Channels per cog of PWM signals. Supports multiple cogs.
</p>
</blockquote>



<h3 style="font-family:verdana;">Display</h3>
Expand Down
Loading

0 comments on commit ab8e160

Please sign in to comment.