Files
NODEDC_MISSION_CORE/plugins/vesc/frontend/src/VescLimits.tsx
T

36 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {useRef,useState} from 'react';
import {Button,ResourceList,ResourceRow,SettingsCard} from '@nodedc/ui-react';
import {perform,type Sensor,type SensorTransport} from '../../../../packages/sensor-ui/src/contracts';
import {vescStatus} from './model';
interface Limits {observed_at:string;identity?:{uuid:string};parameters:Record<string,number>}
const number=(value:number|undefined)=>value===undefined?'—':value.toLocaleString('ru-RU',{maximumFractionDigits:2});
export function VescLimits({controllers,transport,enabled,failure}:{controllers:Sensor[];transport:SensorTransport;enabled:boolean;failure:(error:unknown)=>void}){
const [values,setValues]=useState<Record<string,Limits>>({});
const [busy,setBusy]=useState(false);const running=useRef(false);
const readable=controllers.filter(device=>device.online&&device.verified&&vescStatus(device).readable&&vescStatus(device).board_settings_supported);
async function read(){
if(!enabled||running.current||!readable.length)return;
running.current=true;setBusy(true);failure(null);setValues({});
try{for(const device of readable){const value=await perform<Limits>(transport,device,'vesc.limits.read');setValues(current=>({...current,[device.id]:value}));}}
catch(error){failure(error);}finally{running.current=false;setBusy(false);}
}
return <SettingsCard title="Ограничения контроллеров" description="Текущие настройки VESC. Чтение не запускает моторы и не меняет конфигурацию." actions={<Button disabled={!enabled||busy||!readable.length} loading={busy} onClick={()=>void read()}>Прочитать ограничения</Button>}>
<p>Ток мотора задаёт тягу; ток батареи ограничивает потребление. Эти настройки действуют и при управлении с пульта. Паспортные пределы моторов, контроллеров и батареи проверяются отдельно.</p>
{controllers.map(device=>{
const value=values[device.id];if(!value)return null;
const p=value.parameters;
const fields=[
['Ток мотора · разгон / торможение',`${number(p.l_current_max)} / ${number(p.l_current_min)} А`],
['Масштаб тока · разгон / торможение',`${number(p.l_current_max_scale*100)} / ${number(p.l_current_min_scale*100)} %`],
['Ток батареи · потребление / рекуперация',`${number(p.l_in_current_max)} / ${number(p.l_in_current_min)} А`],
['Диапазон электрических оборотов',`${number(p.l_min_erpm)} … ${number(p.l_max_erpm)} ERPM`],
['Максимальная мощность',p.l_watt_max>=1500000?'Отдельный предел не задан':`${number(p.l_watt_max)} Вт`],
['Максимальный duty cycle',`${number(p.l_max_duty*100)} %`],
];
return <SettingsCard key={device.id} title={device.name} description={`Прочитано ${new Date(value.observed_at).toLocaleString('ru-RU')}`}><ResourceList aria-label={`Ограничения ${device.name}`}>{fields.map(([title,description])=><li key={title}><ResourceRow title={title} description={description}/></li>)}</ResourceList></SettingsCard>;
})}
{!Object.keys(values).length&&<p>Прочитайте значения для подключённых контроллеров.</p>}
</SettingsCard>;
}