raspberry pi to stm32f303k8 for serial communication

Dependencies:   mbed Servo

Files at this revision

API Documentation at this revision

Comitter:
mukuyo
Date:
Sun May 02 15:01:33 2021 +0000
Commit message:
update;

Changed in this revision

Drive/Drive.cpp Show annotated file Show diff for this revision Revisions of this file
Drive/Drive.hpp Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
serial/serial.cpp Show annotated file Show diff for this revision Revisions of this file
serial/serial.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Drive/Drive.cpp	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,67 @@
+#include "Drive.hpp"
+#define LED_PIN PF_0
+
+Drive::Drive(PinName CAN_TX, PinName CAN_RX): Rasp(USBTX,USBRX), canMBED(CAN_TX, CAN_RX), LED(LED_PIN), servo(PA_8){
+    initCAN();
+    //can_Timer.attach(callback(this,&UDP::can_send), 16ms);
+    count = 0;
+}
+
+void Drive::setVelocity(){
+    //LED=0;
+    for(int i = 0; i < 4; i++)
+    {
+        M[i] = 0;
+        Rasp::get(M[i], i);
+    }
+    dribble_set();
+    calcWheelVelocity();
+    setMotVel();
+    can_send();
+}
+
+void Drive::dribble_set(){
+    if(dribble_power > 0){
+        dribble_power = 0.25;
+    }
+    servo = dribble_power;
+}
+
+void Drive::calcWheelVelocity(){
+    
+    float max;
+    double plus;
+   
+    order.Mot0Order.Vel_short = M[0];
+    order.Mot1Order.Vel_short = M[1];
+    order.Mot2Order.Vel_short = M[2];
+    order.Mot3Order.Vel_short = M[3];
+    /*order.Mot0Order.Vel_short = -100;
+    order.Mot1Order.Vel_short = -100;
+    order.Mot2Order.Vel_short = 100;
+    order.Mot3Order.Vel_short = 100;*/
+}
+
+void Drive::setMotVel(){
+    send_motvel_data[0] = (char)order.Mot0Order.Vel_char.L;
+    send_motvel_data[1] = (char)order.Mot0Order.Vel_char.H;
+    send_motvel_data[2] = (char)order.Mot1Order.Vel_char.L;
+    send_motvel_data[3] = (char)order.Mot1Order.Vel_char.H;
+    send_motvel_data[4] = (char)order.Mot2Order.Vel_char.L;
+    send_motvel_data[5] = (char)order.Mot2Order.Vel_char.H;
+    send_motvel_data[6] = (char)order.Mot3Order.Vel_char.L;
+    send_motvel_data[7] = (char)order.Mot3Order.Vel_char.H;
+}
+
+void Drive::initCAN(){
+    canMBED.frequency(100000);
+    
+}
+
+void Drive::can_send(){
+    canMBED.write(CANMessage(0x1AA,send_motvel_data,8)); 
+}
+
+int Drive::masuo_send(){
+    return 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Drive/Drive.hpp	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,67 @@
+#ifndef Drive_hpp
+#define Drive_hpp
+
+#include "mbed.h"
+#include "serial.h"
+#include "Servo.h"
+
+class Drive : public Rasp{
+    public:
+        Drive(PinName CAN_TX, PinName CAN_RX);
+        void setVelocity();
+        void calcWheelVelocity();
+        void dribble_set();
+        void setMotVel();
+        void initCAN();
+        void can_send();
+        int masuo_send();
+       
+    private:
+        typedef struct {
+            union{
+                signed short Vel_short;
+                struct{
+                    signed short L:8;
+                    signed short H:8;
+                }Vel_char;
+            }Mot0Order;
+    
+            union{
+                signed short Vel_short;
+                struct{
+                    signed short L:8;
+                    signed short H:8;
+                }Vel_char;
+            }Mot1Order;
+    
+            union{
+                signed short Vel_short;
+                struct{
+                    signed short L:8;
+                    signed short H:8;
+                }Vel_char;
+            }Mot2Order;
+    
+            union{
+                signed short Vel_short;
+                struct{
+                    signed short L:8;
+                    signed short H:8;
+                }Vel_char;
+            }Mot3Order;
+    
+        } OrderMotVel;
+
+        OrderMotVel order;
+        
+        Ticker can_Timer;
+        char send_motvel_data[8];
+        CAN canMBED;
+        float M[4];
+        float dribble_power;
+        DigitalOut LED;
+        Servo servo;
+        int count;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include "serial.h"
+#include "Drive.hpp"
+
+Rasp rasp(USBTX, USBRX); 
+
+Drive drive(PA_11, PA_12);
+
+void DR_setup(){
+}
+
+int main(void) {
+    DR_setup();
+    while (1) {
+        drive.setVelocity();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial/serial.cpp	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,34 @@
+#include "serial.h"
+Rasp::Rasp(PinName TX,PinName RX): device(USBTX,USBRX) {
+    device.baud(115200);//通信速度最速
+    device.attach(callback(this,&Rasp::dev_rx), RawSerial::RxIrq);//角度割り込み入力
+    u=0;
+    ID=0;
+    rx_val=0;
+    BUFFER_SIZE=6;
+}
+
+void Rasp::dev_rx(){
+  u++;
+  buffer[u-1]=device.getc();
+  rx_val=buffer[0];
+  if(buffer[0]!=0xFF)u--;
+  if(u==BUFFER_SIZE){
+     if(buffer[0]==0xFF){
+         u=0;
+         motor_limit = buffer[1]/100;
+         for(int i = 0; i < 4; i++){
+           Motor_pow[i] = buffer[i+2] - 100;
+           Motor_pow[i] *= 0.4;
+         }
+     }
+  }
+}
+void Rasp::put(int val){
+    device.putc(val);
+}
+void Rasp::get(float &a, int num){
+   
+       a= Motor_pow[num];
+   
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial/serial.h	Sun May 02 15:01:33 2021 +0000
@@ -0,0 +1,19 @@
+#pragma once
+#include "mbed.h"
+
+class Rasp {
+public:
+    Rasp(PinName TX,PinName RX);
+    void dev_rx();
+    void put(int val);
+    void get(float &a, int num);
+    
+    int rx_val,ID,u,vel_norm,vel_theta,flag,DKpow;
+    float omega;
+private:
+    int BUFFER_SIZE,val_1,val_2;
+    int buffer[100];
+    float Motor_pow[4];
+    float motor_limit;
+    RawSerial device;
+};