collab/pb-hal

git clone https://git.julia.blue/collab/pb-hal

Rahul ButaniDRV8871s work!1dbe808

control
1.3 KiB78 linesraw
1#include <RASLib/inc/common.h>
2#include <RASLib/inc/gpio.h>
3#include <RASLib/inc/time.h>
4
5#include <RASLib/inc/pwm.h>
6
7#include <stdint.h>
8
9// Blink the LED to show we're on
10tBoolean blink_on = true;
11
12#define IN1 PIN_D1
13#define IN2 PIN_D2
14
15#define min(a, b) ((a) <= (b) ? (a) : (b))
16#define max(a, b) ((a) >= (b) ? (a) : (b))
17
18tPWM *uno, *dos;
19int8_t speed;
20
21#define SCALE_LEN 5
22
23static const char scale[2*SCALE_LEN + 1] = "asdfghjkl;'";
24
25
26void blink(void)
27{
28    SetPin(PIN_F3, blink_on);
29    blink_on = !blink_on;
30}
31
32void setOutputs(const int8_t set)
33{
34    // If set is below 0, go in direction of IN1:
35    SetPWM(uno, -min(set/128.0, 0), 0);
36    SetPWM(dos,  max(set/128.0, 0), 0);
37}
38
39int8_t getSpeed(char c)
40{
41    int8_t step  = 127 / (SCALE_LEN),
42           speed = -(SCALE_LEN);
43
44    while (speed <= SCALE_LEN)
45    {
46        if (scale[speed + SCALE_LEN] == c)
47            break;
48
49        speed++;
50    }
51
52    return speed * step;
53}
54
55int main(void)
56{
57    CallEvery(blink, 0, 0.5);
58
59    InitializeUART(115200);
60
61    uno = InitializePWM(IN1, 200);
62    dos = InitializePWM(IN2, 200);
63
64    setOutputs(0);
65    int8_t set;
66
67
68    while (true) {
69
70        Printf("Speed? ");
71
72        set = getSpeed(Getc());
73        Printf("%d\n", set, set);
74        Printf("%f & %f\n\n", -min(set/128.0, 0), max(set/128.0, 0));
75
76        setOutputs(set);
77    }
78}