summaryrefslogtreecommitdiffstats
path: root/main.py
blob: e281a8fa28cb1f3c59046700e6ce8ac4011e87ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from machine import Pin
from machine import PWM
from time import sleep
import gc
from rp2 import *

# Enable debug messages over serial (a bit slower)
DEBUG = 0
DEBUG2 = 0
# Enable canbus output
CANBUS = 1

CAN1 = CAN()
FAN1 = PWM(Pin(20))
FAN1.freq(25000)

LED1 = Pin(25, Pin.OUT)

IN1 = Pin(16,Pin.OUT)
IN2 = Pin(17,Pin.OUT)
IN3 = Pin(18,Pin.OUT)
IN4 = Pin(19,Pin.OUT)

PINS = [IN1, IN2, IN3, IN4]

sequence = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
sequence_reverse = [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]]
allpinsoff = [0,0,0,0]

FAN1.duty_u16(25000)
sleep(5)
FAN1.duty_u16(0)
sleep(2)

# CIRCLE = 1024
CURRENTSTEP=0
NEXTSTEP=0
FORWARD=0
REVERSE=0
TMIN=18
TMAX=33
# 18=0
# x=((1024/25)*(t-TMIN)):1
#x=((1024/25)*(t-TMIN)):1

while True:
    if CANBUS:
        frames=CAN1.recv()
        for frame in frames:
            mycid=frame.get_arbitration_id()
            if mycid == 48:            ######################################
                rawdata=frame.get_data()
                lefttemp=int(((rawdata[0])/2.4))
                righttemp=int(((rawdata[1])/2.4))
                combtemp=int(((rawdata[2])/2.4))
                #print(combtemp)
                #
                fanspeed=rawdata[4]

                # fan handling
                #print(fanspeed)
                # fanspeed is 0-255
                # transform into duty cycle
                # 0 is 5000
                # 255 is 25000
                # aka 20000 range
                if fanspeed == 0:
                    dutycycle=0
                    FAN1.duty_u16(dutycycle)
                else:
                    # approximation ... 19890 = 24890 ~ 25000	
                    dutycycle=( 5000 + ( 78 * int(fanspeed) ) )
                    FAN1.duty_u16(dutycycle)
                print(dutycycle)
                
                # temp handlingv
                NEXTSTEP=int(combtemp*1024/100)
                if NEXTSTEP > CURRENTSTEP:
                    FORWARD=1
                    count=int(NEXTSTEP-CURRENTSTEP)
                    while count > 0:
                        for step in sequence:
                            for i in range(len(PINS)):
                                PINS[i].value(step[i])
                                sleep(0.001)
                            count=count-1
                else:
                    FORWARD=0
                    count=int(CURRENTSTEP-NEXTSTEP)
                    while count > 0:
                        for step in sequence_reverse:
                            for i in range(len(PINS)):
                                PINS[i].value(step[i])
                                sleep(0.001)
                            count=count-1
                #
                for i in range(len(PINS)):
                    PINS[i].value(allpinsoff[i])
                CURRENTSTEP=NEXTSTEP
                
        LED1.toggle()
                    
    else:
        LED1.toggle()
        #print(frame)
        #x=frame.get_data()
        #int result = ((0xF0 - 0x10) / 15) * (value - 15) + 0x10;
        # x / (0xF0/0x10) = (y - 15) + 0x10
        #lefttemp=int((x[0]/(224/16))+15-1)
        #righttemp=int((x[1]/(224/16))+15-1)
        #fanspeed=x[4]
        count=512
        while count > 0:
            for step in sequence:
                for i in range(len(PINS)):
                    PINS[i].value(step[i])
                    sleep(0.001)
                count=count-1
        #         print(lefttemp)
        #        print(righttemp)
        #        print(fanspeed)
        for i in range(len(PINS)):
            PINS[i].value(allpinsoff[i])
        sleep(0.2)
        count=512
        while count > 0:
            for step in sequence_reverse:
                for i in range(len(PINS)):
                    PINS[i].value(step[i])
                    sleep(0.001)
                count=count-1
        for i in range(len(PINS)):
            PINS[i].value(allpinsoff[i])        
    sleep(1)
    if DEBUG:
        # debug output on serial is slower
        sleep(0.05)